Search code examples
javascriptslackhubot

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-slack/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


Solution

  • 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