问题
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