【问题标题】:In Twilio Programmable Chat, in Laravel backend, how to get members of a channel, given the channel code在 Twilio Programmable Chat 中,在 Laravel 后端,如何在给定频道代码的情况下获取频道成员
【发布时间】:2022-01-14 19:32:27
【问题描述】:

如何在我的 Laravel 后端获取频道的 Twilio 成员列表?我正在使用 Twilio 的可编程聊天 API。我尝试使用 ChannelInstance 成员方法或属性,但我没有正确获取成员列表。有没有办法从 Laravel 内部做到这一点?我需要它被另一端的 iOS 应用程序使用(iOS 应用程序是客户端)。 Ios 应用向 Laravel 询问用户与之聊天的用户列表,所以我首先获取所有频道:

$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());


}

在 print_r - ed 数组中,我有 uniqueName 和friendlyName,它们连接了频道的两个参与者。但我更喜欢只有两个对象,或者两个字符串来告诉我哪些是该频道的成员参与者。有没有办法做到这一点?

【问题讨论】:

    标签: channel member twilio-programmable-chat


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    在这种情况下,您只是在阅读有关频道的信息。相反,您应该fetch the members from the channel

    foreach ($ucs as $uc) {
        $channel_members = $twilio->chat->v2->services($this->serviceId)
            ->channels($uc->channelSid)
            ->members
            ->read([], 20);
        print_r($channel_members);
    }
    

    【讨论】:

    • 我试过了,但我收到错误消息“在数组上使用 toArray()”。所以我删除了 toArray() 部分,但我只是用许多私有方法获得了对象的整个打印输出,所以我无法获取成员的身份。
    • 啊,抱歉,$channel_members 当然已经是一个数组了。我看到你自己设法得到了答案。我将更新这个以删除 toArray 部分。
    • 如果你愿意,我给你一两天时间让你给出正确答案。如果没有,两天后我会接受我的回答
    • 你做对了!接受你的答案!
    • 好吧...还需要 5 个小时才能完成。
    【解决方案2】:

    要获得会员,我需要这样做:

    $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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2019-07-08
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 2021-10-06
      • 2022-09-25
      相关资源
      最近更新 更多