Messaging

If you need translation feature, see below.

1. Get Messages

const resp = await client.getMessages({
    channelId: 'my_channel',
    order: 'latest', // default: 'latest'. Use 'oldest' to order by oldest messages first
    limit: 10, // how many messages to fetch, default: 20, max: 50
});

if (resp.hasNext) {
    const lastMessageId = resp.messages[resp.messages.length - 1].id;
    
    const moreResp = await client.getMessages({
        channelId: 'my_channel',
        lastMessageId: lastMessageId,
        limit: 10,
    });
}

Response

{
  "messages": [
    {
      "id": "5f7d5e407d138500010000ce",
      "channelId": "my_channel",
      "userId": "user456",
      "username": "user456",
      "profileImageUrl": "http://cdn.test.com/456.jpg",
      "type": "text",
      "text": "Hello",
      "data": {},
      "fileUrl": "http://cdn.test.com/123.jpg",
      "mentions": [],
      "parentMessage": {}, // message to reply to
      "parentMessageId": "",
      "reactions": {"happy": ["user1"]},
      "createdAt": 1583921400
    }
  ],
  "hasNext": false
}

2. View Messages With File Upload

View messages with file attachment.

const resp = await client.getFileMessages({
    channelId: 'my_channel',
    limit: 10,
});

3. Send Message

await client.sendMessage({
    channelId, 
    type: 'text', 
    text: 'hello?', 
    data: {imageUrl: 'http://image.cdn.com/123.jpg'},
    mentions: ['user1', 'user2'] // optional. mentioning another user
});

4. View Message

View a single message.

const resp = await client.getMessage({
    channelId, 
    messageId: 'someMessageId',
});

5. Reply To Message

Reply to another message.

await client.sendMessage({
    channelId, 
    type: 'text', 
    text: 'hello?', 
    parentMessageId: 'anotherMessageIdThatIamReplyingTo',
    data: {imageUrl: 'http://image.cdn.com/123.jpg'},
});

6. Mark As Read

You can mark a channel as read.

await client.markAsRead({channelId: 'my_channel'}); 

7. React To Message

You can add/remove reaction to a message.

// add reaction to message
await client.addMessageReaction({
    channelId: 'my_channel', 
    messageId: 'my_message',
    reaction : 'happy',
});

// remove reaction
await client.removeMessageReaction({
    channelId: 'my_channel', 
    messageId: 'my_message',
    reaction : 'happy',
});

// reactions field is visible in message object
const msgListResp = await client.getMessages({channelId: 'someChannelId'});
console.log(msgListResp.messages[0].reactions);
/*
{
    "happy": [
        "user1",
        "user2"
    ],
    "sad": [
        "user3"
    ]
}
*/

8. Message Translation

await client.translateMessage({
    channelId: 'my_channel', 
    messageId: 'target_messageId',
    targetLanguages: ['en', 'de'], // translate to en, de
}); 

9. Get Unread Message Count

You can find out how many channel members have not read a particular message.

const channelGetResp = await client.getChannel({channelId: 'demo channel'});
const channelObject = channelGetResp.channel;

const msgResp = await client.getMessages({channelId: 'my channel'});
const messageObject = msgResp.messages[0]; // assume that message array is not empty in this example

const msgUnreadCount = client.getMessageUnreadCount({channel: channelObject, message: messageObject});
console.log(`msgUnreadCount: ${msgUnreadCount}`); // integer result

10. Delete Message

You can delete your own message.

await client.deleteMessage({channelId: 'my_channel', messageId: 'messageId_to_delete'}); 

Last updated