简体   繁体   中英

Unable to send a message to specific channel in Slack with Hubot - SlackRTMError: no channel id

Simple script but it's not working properly.. I'm trying to send a message to a specific channel determined by the user's input.

Code:

module.exports = function(robot) {

    robot.respond(/in (.*) say (.*)/i, function(msg) {

        var channel = msg.match[1];
        var sentence = msg.match[2];

        robot.send(channel, sentence);
    });
}

When I run it, hubot throws the following error:

2016-09-01T18:46:36.836661+00:00 app[web.1]: Unhandled rejection SlackRTMError: no channel id

2016-09-01T18:46:36.836677+00:00 app[web.1]: at RTMClient.handleMessageAck [as _handleMessageAck] (/app/node_modules/hubot-slack/node_modules/@slack/client/lib/clients/rtm/client.js:497:40) 2016-09-01T18:46:36.836679+00:00 app[web.1]: at RTMClient._handleWsMessageViaEventHandler (/app/node_modules/hubot-slack/node_modules/@slack/client/lib/clients/rtm/client.js:460:12) 2016-09-01T18:46:36.836680+00:00 app[web.1]: at RTMClient.handleWsMessage (/app/node_modules/hubot-slack/node_modules/@slack/client/lib/clients/rtm/client.js:420:10) 2016-09-01T18:46:36.836683+00:00 app[web.1]: at WebSocket.emit (events.js:98:17) 2016-09-01T18:46:36.836684+00:00 app[web.1]: at Receiver.ontext (/app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/ws/lib/WebSocket.js:841:10) 2016-09-01T18:46:36.836685+00:00 app[web.1]: at /app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/ws/lib/Receiver.js:536:18 2016-09-01T18:46:36.836686+00:00 app[web.1]: at /app/node_modules/hubot-sl ack/node_modules/@slack/client/node_modules/ws/lib/Receiver.js:368:7 2016-09-01T18:46:36.836687+00:00 app[web.1]: at /app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/ws/lib/PerMessageDeflate.js:249:5 2016-09-01T18:46:36.836687+00:00 app[web.1]: at afterWrite (_stream_writable.js:278:3) 2016-09-01T18:46:36.836688+00:00 app[web.1]: at onwrite (_stream_writable.js:270:7) 2016-09-01T18:46:36.836689+00:00 app[web.1]: at WritableState.onwrite (_stream_writable.js:97:5) 2016-09-01T18:46:36.836689+00:00 app[web.1]: at afterTransform (_stream_transform.js:99:5) 2016-09-01T18:46:36.836690+00:00 app[web.1]: at TransformState.afterTransform (_stream_transform.js:74:12) 2016-09-01T18:46:36.836691+00:00 app[web.1]: at Zlib.callback (zlib.js:456:5) 2016-09-01T18:46:36.836691+00:00 app[web.1]: 2016-09-01T18:46:36.836681+00:00 app[web.1]: at WebSocket.wrapper (/app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/lodash/lodash.js:4762:19)

Any ideas why it is not working? I've used #general and general to test the functionality of it but it hasn't worked

In Slack API, channels are not their names, but instead some form of IDs. You can tell by an ID that it is channel ID when its first letter is "C", like in C024BE91L .

I believe what you need is to get channel ID by its name. To do that, use channels.list method to fetch all the channels, and then Array#find to find a proper channel by its name:

bot.api.channels.list((error, response) => {
  const targetChannel = response.data.find(channel => channel.name === message.match[1]);

  robot.send(targetChannel.id, sentence);
});

Just optimize it to not call this API every time your bot receives a message, and it should work.

PS You can even see discussion of similar problem in this issue on Github .

Thanks! I found the id using msg.envelope.channel. I didn't know where it was stored so I couldn't address it.

for(x in msg.envelope) {
    console.log(x + " : " + msg.envelope[x]);
} 

That was the best way to check the variables of the msg object. Plus I had to send it using msg.messageRoom(roomID, msg) instead of robot.send.

Thanks again

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