0
    privatechannel.getMessages().then(function (messages) {
    const totalMessages = messages.items.length;
    for (let i = 0; i < totalMessages; i++) {
      const message = messages.items[i];
      console.log('Author:' + messages.author);
      printMessage(message.author, message.body);
    }
    console.log('Total Messages:' + totalMessages);
    deleteNotifs()

  });

This is listed as the way in Twilio docs to retrieve the most recent messages. I tried it and the maximum number of messages it displays are not even like 100, it just shows me the last 30 messages in the conversation.

Is there a way to retrieve and display all the messages in a Twilio Programmable Chat channel ( private or otherwise )?

philnash
  • 70,667
  • 10
  • 60
  • 88
Roast Biter
  • 651
  • 1
  • 6
  • 22

2 Answers2

1

Twilio developer evangelist here.

When you make a request to channel.getMessages() there are a few things you can do to get more than the default previous 30 messages.

First, you can pass a pageSize to get more messages from each request. pageSize is 30 by default and can be up to (I think) 100.

privatechannel.getMessages(100).then(function(messagePaginator) { 
  // deal with the messages
});

If you want to go beyond 100 messages then you will notice from the docs that getMessages returns a Promise that resolves to a Paginator<Messages>. The Paginator object has an items property that you've already used to access the messages. It also has hasNextPage and hasPrevPage properties, that tell you whether there are more messages available.

There are also nextPage and prevPage functions that return the next or previous page of the Paginator you are using.

So, in your example, you can fetch and print all the messages in a channel like so:

const messages = [];
let totalMessages = 0;

function receivedMessagePaginator(messagePaginator) {
  totalMessages += messagePaginator.items.length;
  messagePaginator.items.forEach(function (message) {
    printMessage(message.author, message.body);
    messages.push(message);
  });
  if (messagePaginator.hasNextPage) {
    messagePaginator.nextPage().then(receivedMessagePaginator);
  } else {
    console.log("Total Messages:" + totalMessages);
  }
}

privatechannel.getMessages().then(receivedMessagePaginator);

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
0

As there is no Syntax error in your code, then it's highly likely a rate limit API related. Consider checking Twilio docs, type of APIs limits (free vs paid subscriptions)

charmful0x
  • 155
  • 9
  • I did find there's way to achieve this. The user can fetch all the message resources, send them to an api endpoint from whatever backend they are using ( Django in my case ) and then retrieve the data from that API endpoint in JS for the chat channel. Now, this works, but is an awful lot for literally just accessing the messages, especially for someone who needs just the last 100 messages or so. Hence, I was hoping someone on here might have a better way to achieve that. – Roast Biter May 01 '21 at 18:46
  • 2
    Good news, there's neither a syntax error or a rate limit issue. It's just that 30 is the default page size. There is a way to paginate through messages too, check out my answer, hope it helps! – philnash May 03 '21 at 01:51