Message History

Fetch messages in reverse chronological order

You can use the fetchHistory method to fetch messages in reverse chronological order (from newest to oldest). fetchHistory also makes it easy to paginate through history in the direction of your choice.

chat.fetchHistory({
    channelId: "alpha",
    limit: 20
}).then(({ status, response }) => {
    // status = { error: (true|false), errorData: { statusCode<Integer>, message<String> } }
    if (status.error) {
        console.log(`Fetch History Failed:
            Reason: ${status.errorData.message}
            (Status Code: ${status.errorData.statusCode})
        `);
        return;
    }
    // response = { messages: [Message], newerPageToken: string, olderPageToken: string }

    // Now let's fetch the next 20 (older) messages:
    chat.fetchHistory({
        channelId: "alpha",
        limit: 20,
        olderPageToken: response.olderPageToken
    });
});

fetchHistory({ channelId: string, ... })

Name

Type

Required

Description

channelId

string

yes

The channel to fetch messages from.

limit

integer

no

The maximum number of messages to fetch (defaults to 100).

olderPageToken

string

no

The olderPageToken will be populated in the response offetchHistory if there exist messages sent before the current batch of retrieved messages (older messages).

newerPageToken

string

no

The newerPageToken will be populated in the response of fetchHistory if there exist messages sent after the current batch of retrieved messages (newer messages).

Fetch messages centered around a specific message

The fetchHistoryAround method gives you the ability to jump back into history to a specific message and retrieve the messages that were sent immediately before and after it.

chat.fetchHistoryAround({
   channelId: "alpha",
   messageId: "31570e17-0459-4fcb-bb57-a793f35c6689",
   limit: 10 
}).then((res) => {
   const { status, response } = res;
   // status = { error: (true|false), errorData: { statusCode<Integer>, message<String> } }
   if (status.error) {
      console.log(`Fetch History Failed:
         Reason: ${status.errorData.message}
         (Status Code: ${status.errorData.statusCode})
      `);
      return;
   }   
   const { messages, newerPageToken, olderPageToken } = response; 
});

fetchHistoryAround({ channelId: string, messageId: string, ... })

Name

Type

Required

Description

channelId

string

yes

The id of the channel for which you want to fetch history.

messageId

string

yes

The message for which you want to fetch history around.

limit

integer

no

The number of messages in both directions you want to fetch. For example, if you set limit to 5, you will receive a maximum of 11 messages: 5 messages before, the messageId message, and 5 message after. Defaults to 100.

Last updated