How to listen for messages on a single channel in Twilio Programmable Chat

时光毁灭记忆、已成空白 提交于 2020-03-05 00:27:35

问题


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.

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

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

回答1:


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.

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){...})


来源:https://stackoverflow.com/questions/60175918/how-to-listen-for-messages-on-a-single-channel-in-twilio-programmable-chat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!