channelmembertwilio-programmable-chat

In Twilio Programmable Chat, in Laravel backend, how to get members of a channel, given the channel code


How can I get the list of Twilio members of a channel, on my Laravel backend? I am using Programmable Chat API of Twilio. I tried using ChannelInstance members method or property, but I am not getting the list of members correctly. Is there a way to do this, from inside Laravel? I need it to be consumed by the iOS app on the other side (iOS app being the client). Ios app asks from Laravel the list of users that the user has chatted to, so I first get all channels:

$params = $request->all();
$username = $params["username"];
$unl = strlen($username);

$twilio = new Client($this->sid, $this->authToken);

// Get all user's channels
$ucs = $twilio->chat->v2->services($this->serviceId)
    ->users($username)
    ->userChannels
    ->read(50);

// Try to find members of each channel
foreach ($ucs as $uc) {

    $channel = $twilio->chat->v2->services($this->serviceId)
        ->channels($uc->channelSid)
        ->fetch();

    print_r($channel->toArray());


}

in the print_r - ed array, I have uniqueName and friendlyName, which are concatenated two participants of the channel. But I would prefer to just have two objects, or two strings that would tell me which are the members participants of that channel. Is there a way to do this?


Solution

  • To get members, I needed to do this:

    $ucs = $twilio->chat->v2->services($this->serviceId)
        ->users($username)
        ->userChannels
        ->read(50);
    
    foreach ($ucs as $uc) {
        $channel_members = $twilio->chat->v2->services($this->serviceId)
            ->channels($uc->channelSid)
            ->members
            ->read([], 20);
    
        foreach ($channel_members as $record) {
            print($record->sid . "\n");
        }
    }