问题
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;
}
?>
回答1:
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
.
回答2:
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().
回答3:
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');
});
来源:https://stackoverflow.com/questions/41542942/twilio-how-to-do-pagination-with-messages