Reputation: 431
I am trying to call Twilio services into my node application.
According to docs I am calling list of messages service like bellow
var accountSid = 'ACe622fda3d3cd03b3b975d8d92f7c794b';
var authToken = "your_auth_token";
var client = require('twilio')(accountSid, authToken);
client.messages.list(function(err, data) {
data.messages.forEach(function(message) {
console.log(message.body);
});
});
As a result I am getting first 50 messages with complete details.
Now my issue is how to get previous messages(pagination), conversations between two numbers and using further filters like date.
Upvotes: 0
Views: 615
Reputation: 73029
Twilio developer evangelist here.
List resources return pagination information, including the next and previous pages' URLs. You can also set the page size.
So, for a first pass you can get more than 50 messages by setting the PageSize to the maximum 1000.
client.messages.list({ PageSize: 1000 }, function(err, data) {
data.messages.forEach(function(message) {
console.log(message.body);
});
});
If you need to go beyond that, then you can use the next page url to get the next page:
var url = require("url");
client.messages.list(function(err, data) {
if (data.next_page_uri) {
// deal with page 1
var query = url.parse(data.next_page_uri, true).query;
client.messages.list(query, function(err, data) {
// and so on
}
}
});
Adam Varga shared a solution he was using on GitHub (it's for phone numbers, but lists all act the same on Twilio). Also, look out for the release of version 3 of the Node.js library, which will include pagination helpers.
Upvotes: 1