# Conversation (Chat)

A conversation(chat) object lets you have conversations with 1 or more [Users](https://docs.minchat.io/reference/user).&#x20;

You can view how a chat object can be created for a [one-to-one conversation](https://docs.minchat.io/overview#start-one-to-one-conversation) or a [group chat](https://docs.minchat.io/overview#start-group-chat) with multiple members.

## Event Listeners

### Listen for typing started <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever a participant of the conversation starts typing.

```jsx
chat.onTypingStarted((user) => {
       //do something when a user starts typing
 })
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>user</code></td><td><a href="../reference/user"><code>User</code></a></td><td>the user that started typing</td></tr></tbody></table>

### Listen for typing stopped <a href="#listen-for-messages" id="listen-for-messages"></a>

This is called whenever a participant of the conversation stops typing.

```jsx
chat.onTypingStopped((user) => {
       //do something when a user stops typing
 })
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="149">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>user</code></td><td><a href="../reference/user"><code>User</code></a></td><td>the user that stopped typing</td></tr></tbody></table>

### Listen for user status changes (online/offline)

This is called whenever the status of a user in a conversation changes from online to offline or vice versa.

```javascript
chat.onMemberStatusChanged((memberId,status) => {
       //do something when a user status changes
 })
```

**Response**

<table><thead><tr><th width="190.33333333333331">Field</th><th width="109">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>memberId</code></td><td><code>string</code></td><td>the id of the user who's status has changed</td></tr><tr><td>status</td><td><code>enum</code> </td><td>the status of the user. an enum of either <code>ONLINE</code> or <code>OFFLINE</code></td></tr></tbody></table>

### Listen for AI Actions <a href="#listen-for-messages" id="listen-for-messages"></a>

Is triggered when an action configured on an AI agent is executed.

```javascript
chat.onAiAction(({event, chatbotUsername}) => {
    //do something with
})
```

<table><thead><tr><th width="190.33333333333331">Field</th><th width="105">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>event</code></td><td><code>string</code></td><td>The event name, this is defined on the minchat dashboard when creating/updating an AI Agent</td></tr><tr><td><code>chatbotUsername</code></td><td><code>string</code></td><td>the username of the AI agent that triggered the event</td></tr></tbody></table>

## Functions

### Get ID

Get the id of the conversation, returns a `string`

```javascript
const id = chat.getId()
```

### Get Title

Get the title of the conversation, returns a `string`

```javascript
const title = chat.getTitle()
```

### Get Conversation Avatar

Get the avatar of the chat, returns a `string`  which is the url to the avatar

```javascript
const avatar = chat.getChatAvatar()
```

### Get recent message

Get the most recent message sent in the conversation. Returns a [`Message`](https://docs.minchat.io/reference/message) object.

```javascript
const message = chat.getLastMessage()
```

### Get Members

Get an array of all the [users](https://docs.minchat.io/reference/user) of this conversation

```javascript
const users = await chat.getMembers()
```

### Get MemberIds

Get an array of ids of all the members of this conversation

```javascript
const userIds = await chat.getMemberIds()
```

### Get Metadata

get an optional key value pair for any additional chat information.

```javascript
const metadata = chat.getMetadata()
```

### Set Metadata

Add any additional chat information.

```javascript
await chat.setMetaData(metadata)
```

**Parameters**

<table><thead><tr><th width="151">Field</th><th width="148">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>metadata</code></td><td><code>json object</code></td><td><em>required</em></td><td>an optional key value pair for any additional chat information. Accepts string, number and boolean values</td></tr></tbody></table>

### Add member to group chat

Add a member to a group chat

```typescript
await chat.addMember(username)
```

### Add member to group chat by ID

add a member to a group chat using a member ID

```typescript
await chat.addMemberById(userId)
```

### Remove member from group chat

Remove a member from a group chat

<pre class="language-typescript"><code class="lang-typescript"><strong>await chat.removeMember(username)
</strong></code></pre>

### Remove member from group chat by ID

Remove a member from a group chat using a member ID

<pre class="language-typescript"><code class="lang-typescript"><strong>await chat.removeMemberById(userId)
</strong></code></pre>

### Notify everyone typing started

Call this function to notify everyone who is a participant of the conversation that the current user has started typing

```javascript
chat.startTyping()
```

### Notify everyone typing stopped

Call this function to notify everyone who is a participant of the conversation that the current user has stopped typing

```javascript
chat.stopTyping()
```

### Set Message Seen

Call this function to notify participants of the conversation that a message has been seen. (useful when handling message seen states).&#x20;

```javascript
chat.setSeen(messageId)
```

**Parameters**

<table><thead><tr><th width="151">Field</th><th width="113">Type</th><th width="98">State</th><th>Description</th></tr></thead><tbody><tr><td><code>messageId</code></td><td><code>string</code> </td><td><em>optional</em></td><td>the id of the message. If left null or undefined then the last message in the conversation is set as seen by the connected user.</td></tr></tbody></table>
