简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM