arun kumar
arun kumar

Reputation: 735

Twilio How to do pagination with messages?

I am able to get the messages from the new php client. How do I do pagination with messages? How to get next_uri, first_uri, page_size parameters ?

 <?php 
 require_once '/Twilio/autoload.php'; // Loads the library

 use Twilio\Rest\Client;

 // Your Account Sid and Auth Token from twilio.com/user/account
 $sid = "xxx"; 
 $token = "xxx";
 $client = new Client($sid, $token);

 // Loop over the list of messages and echo a property for each one
 foreach ($client->messages->read() as $message) {
 echo $message->body;
 }
 ?>

Upvotes: 3

Views: 4395

Answers (4)

Charbel B.G.
Charbel B.G.

Reputation: 301

For anyone seeing this late in 2025, you can use the .nextPage() to get the following page data.

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 14520

I wasted hours on this. In case it saves some future person some time, here's what I did. I'm using Laravel but you get the idea:

In your controller:

// If no pagination info has been specified, get the first page of data
// using page().  If there is pagination info in the request, use it with
// getPage()

if (! $request->page) {
    $messages = $client->messages->page([], 30);
} else {
    $messages = $client->messages->getPage($request->page);
}

Then, in your view (Laravel/blade pseudo-code):

@foreach ($messages as $message)
    $message->body
    // ... etc
@endforeach

// Next page link
?page={{ urlencode($messages->getNextPageUrl()) }}

// Prev page link
?page={{ urlencode($messages->getPreviousPageUrl()) }}

Docs for page() and getPage().

Upvotes: 2

ARUNBALAN NV
ARUNBALAN NV

Reputation: 1662

Here is the Node.js code for fetching message history using paging. You can specify how many items should be in a single page by using the parameter pageSize and use limit parameter to limit the number of pages to display

              client.messages.each({
                dateSent: new Date(date.toDateString()),
                from: event.To,
                to: event.From,
                pageSize: 1,
                limit:1,
                done: function(done) {//this is the call back for the for each loop. this will get fired even if no messages found.
                    console.log('completed for each');
                }
            }, (messages) => {//each message can handle here.
                console.log("message body:", messages.body);
            }, (Error) => {
                console.log('err');
     });

Upvotes: 0

philnash
philnash

Reputation: 73047

Twilio developer evangelist here.

Instead of using read() you can use stream() which will return an iterator for your messages. You can give stream() a limit, but by default it has no limit and will iterate over all of your messages.

 <?php 
 require_once '/Twilio/autoload.php'; // Loads the library

 use Twilio\Rest\Client;

 // Your Account Sid and Auth Token from twilio.com/user/account
 $sid = "xxx"; 
 $token = "xxx";
 $client = new Client($sid, $token);

 // Loop over the list of messages and echo a property for each one
 foreach ($client->messages->stream() as $message) {
 echo $message->body;
 }
 ?>

The pagination information itself is returned in each request. You can see an example of a call to the Calls resource in the documentation and the pagination information will be the same for Messages.

Upvotes: 2

Related Questions