如何在Twilio可编程聊天中的单个频道上收听消息

如何在Twilio可编程聊天中的单个频道上收听消息

本文介绍了如何在Twilio可编程聊天中的单个频道上收听消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 twilio-chat.js 如何在一个听筒上收听消息渠道?我发现了这个问题询问如何在多个频道上收听,但找不到任何描述如何在单个频道上收听的内容.

Using twilio-chat.js how can I listen for messages on a single channel? I found this question which asks how to listen on multiple channels, but I can't find anything describing how to do this on a single channel.

(其中token访问令牌).目前,我有:

(Where token is an Access Token).Currently I have:

let client = await Twilio.Chat.Client.create(token);
client.on('messageAdded', function(message){...})

推荐答案

将消息添加到频道时将触发messageAdded事件.客户在所有订阅的频道上获取所有这些事件.

The messageAdded event is fired when messages are added to the channel. The client picks up on all of these events on all subscribed channels.

您需要在通道本身而不是客户端上处理messageAdded事件.为此,您首先需要获取频道-在这种情况下,通过 SID ,然后处理该事件:

You need to handle the messageAdded event on the channel itself, rather than the client. To do this, you first need to get the channel - in this case by SID, then handle the event:

let client = await Twilio.Chat.Client.create(token);
let channel = await client.getChannelBySid(sid);
channel.on('messageAdded', function(message){...})

这篇关于如何在Twilio可编程聊天中的单个频道上收听消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:08