Search code examples
phptwiliotwilio-conversations

Twilio PHP conversation->message->read , could not sort using provided syntax


Here is the snippet of twilio doc for php to order messages

<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';

use Twilio\Rest\Client;

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$messages = $twilio->conversations->v1->conversations("CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                                      ->messages
                                      ->read(["order" => "desc"], 20);

foreach ($messages as $record) {
    print($record->sid);
}

enter image description here

Here is my implementation

$messages = $twilio_client->conversations->v1->conversations($conversation_channel_sid)
    ->messages->read(["order" => "desc"], 20);

but it was not allowed. it ran into following error:

Fatal error: Uncaught TypeError: Argument 1 passed to Twilio\Rest\Conversations\V1\Conversation\MessageList::read() must be of the type int or null, array given, called in D:\xampp\htdocs\teleupachar2.0\P127_TeliUpachar\chat-module\conversation_test.php on line 67 and defined in D:\xampp\htdocs\teleupachar2.0\P127_TeliUpachar\vendor\twilio\sdk\src\Twilio\Rest\Conversations\V1\Conversation\MessageList.php:103 Stack trace: #0 D:\xampp\htdocs\teleupachar2.0\P127_TeliUpachar\chat-module\conversation_test.php(67): Twilio\Rest\Conversations\V1\Conversation\MessageList->read(Array, 20) #1 {main} thrown in D:\xampp\htdocs\teleupachar2.0\P127_TeliUpachar\vendor\twilio\sdk\src\Twilio\Rest\Conversations\V1\Conversation\MessageList.php on line 103

it says it must be of the type int or null. Is there any other ways to filter or sort those messages?

Thank you for your time.


Solution

  • Twilio developer evangelist here.

    It turns out that the order parameter is new. In fact it was only released a week ago. The helper libraries are generated every 2 weeks and the next time it will be generated with the latest API definitions is 25th August. In the meantime, the documentation got ahead of the library.

    So, look out for the next version of the Twilio PHP library, which should bring the order parameter to the messages resource list and make that example code work for you.