# XendBridge Chat SDK

For a third-party application to be able to get the history of messages and also send one, this SDK has to be implemented. Here are the steps to follow;

### Installation

Using npm :

```shell
$ npm install xendbridgechat-sdk
```

Using yarn :

```shell
$ yarn add xendbridgechat-sdk
```

{% embed url="<https://www.npmjs.com/package/xendbridgechat-sdk>" %}
XendBridge Chat SDK
{% endembed %}

### Usage

```javascript
# using require
const { getMessageHistory, sendMessage } = require('xendbridgechat-sdk');

# using import
import { getMessageHistory, sendMessage } from 'xendbridgechat-sdk';
```

### Examples

XendBridge Chat SDK provides only two distinct functions. GetMessageHistory and SendMessage.

The **getMessageHistory** takes the three arguments which are a public key(string), dispute reference(string), and a function called **getAllChats**.

**getAllChats** is a function that must be called as an argument in both **getMessageHistory** and **sendMessage**. It automatically fetches the history for you after a message is being sent or when an initial connection to the hub is made.

```javascript
//implement a better and safer way for getting the following values
const disputeRef = "AdhdfoGjfo8jorp";
const publicKey = "XP_34984hfhsdksofhs";

const getHistory = async () => {
    await getMessageHistory(disputeRef, publicKey, getAllChats);
};

//call when connected to application
getHistory();
```

The **sendMessage** takes three arguments as well, being an object called ChatHubAddCommentModel which has props of email address, comment, sent by, recipient, base64Attachment, dispute reference, and sent at. The other arguments are the public key and the function **getAllChats**.

```javascript
//implement a better and safer way for getting the following values
const disputeRef = "AdhdfoGjfo8jorp";
const publicKey = "XP_34984hfhsdksofhs";

let ChatHubAddCommentModel = {
    EmailAddress: "youruseremailaddress@domain.com",
    Comment: comment, //the user's message from the client side
    SentBy: 2, //this is always 2 representing the third party user
    Recipient: 1, //this is always 1 representing all entities involved
    Base64Attachment: "", //this is the image in base64 format
    DisputeReference: disputeRef, //the unique dispute reference
    SentAt: new Date().toISOString() //this is the time in timestamp
}

//call when user hits the send button on the UI
sendMessage(ChatHubAddCommentModel, publicKey, getAllChats);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.xendbridge.com/api-method-calls/dispute/xendbridge-chat-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
