首页 > 解决方案 > Twilio 可编程聊天 getChannelByUniqueName 引发“禁止”错误

问题描述

在我的后端服务中创建一个频道后,我试图在我的反应前端中加入这个频道,像这样的 useEffect 钩子:

const initChat = async () => {

    const chatClient = await Client.create(props.token);
    const channel = await chatClient.getChannelByUniqueName(props.room);

    if (channel) {
      if (channel.status !== "joined") {
        chatClient.on("channelJoined", () => {
          registerChannelListeners(channel);
        });

        await channel.join();
      } else {
        registerChannelListeners(channel);
      }

      setChannel(channel);
      setMessages((await channel?.getMessages()).items);
    }
  };
}

最初导航到页面时,错误

upstream.js?a850:136 Uncaught (in promise) Error: Forbidden
    at Upstream.actualSend (upstream.js?a850:136)

偶尔被抛出。

重新加载页面时,一切正常。

有问题的行似乎是:

const channel = await chatClient.getChannelByUniqueName(props.room);

因为没有进一步的代码被执行。token并且room都分配有有效值。

在解码的套接字消息中,此错误消息是从 twilio 发送的:

{"method":"reply"...,"http_status":{"code":403,"status":"Forbidden"}}
{"status":403,"message":"User not member of channel","code":50400}

尽管两个参与者都是通过后端通过此功能邀请的:

inviteParticipantToChannel(participant: Participant, channelSid: string) {
    this.logger.log(
      `Inviting paricipant ${participant.getIdentifier()} to channel ${channelSid}`,
    );

    return this.twilioClient.chat
      .services(process.env.TWILIO_CHAT_SERVICE_ID)
      .channels(channelSid)
      .invites.create({ identity: participant.getIdentifier() });
  }

为了让参与者能够找到/加入频道,我还需要做更多的事情吗?

标签: node.jsreactjstwiliouse-effecttwilio-programmable-chat

解决方案


您是否尝试过设置短暂的超时?twilio 的频道资源可能需要一些时间。


推荐阅读