Twilio 如何对消息进行分页?

3

我能够从新的PHP客户端获取消息。如何对消息进行分页?如何获取next_uri、first_uri和page_size参数?

 <?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;
 }
 ?>
3个回答

2

我在这上面浪费了好几个小时。为了节省未来的时间,这是我所做的。我使用的是Laravel框架,但你可以得到相同的想法:

在你的控制器中:

// 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);
}

然后,在你的视图(Laravel/blade 伪代码)中:

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

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

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

page()getPage()文档


回顾6年前发布的这个问题,我仍然无法在整个Twilio文档中找到一个合适的示例来展示如何实现这一点,我浪费了很多时间。 - try_catch

2
Twilio的开发者布道师在此。
您可以使用 stream()来代替read(),它将返回一个消息的迭代器。您可以为stream()设置限制,但默认情况下它没有限制,会遍历所有消息。
 <?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;
 }
 ?>

“分页信息”本身会在每个请求中返回。您可以在文档中看到对“Calls”资源的调用示例,分页信息对于“Messages”也是相同的。其中pagination informationexample of a call to the Calls resource in the documentation是链接,可点击跳转至相关内容。

我在哪里可以找到 read()、stream() 方法的文档? - arun kumar
@philnash 提供的 Twilio 页面已经不存在了。404 - ryantxr
1
抱歉404错误。文档现在在这里:https://www.twilio.com/docs/libraries/reference/twilio-php/5.16.2/class-Twilio.Rest.Api.V2010.Account.MessageList.html - philnash
1
@philnash,我能够通过流式函数获取记录,并且也获得了下一页的信息。但是我不确定如何调用下一页?你能帮我吗? - Zedd Index
1
有没有办法在流函数中传递页面编号。 - Zedd Index
显示剩余2条评论

0

以下是使用分页获取消息历史记录的Node.js代码。您可以使用参数pageSize指定单个页面中应有多少项,并使用limit参数限制要显示的页面数。

              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');
     });


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接