javascript - Unable to send a message to specific channel in Slack with Hubot - SlackRTMError: no channel id -
simple script it's not working properly.. i'm trying send message specific channel determined user's input.
code:
module.exports = function(robot) { robot.respond(/in (.*) (.*)/i, function(msg) { var channel = msg.match[1]; var sentence = msg.match[2]; robot.send(channel, sentence); }); }
when run it, hubot throws 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]: @ 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]: @ 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]: @ 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]: @ websocket.emit (events.js:98:17) 2016-09-01t18:46:36.836684+00:00 app[web.1]: @ 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]: @ /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]: @ /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]: @ /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]: @ afterwrite (_stream_writable.js:278:3) 2016-09-01t18:46:36.836688+00:00 app[web.1]: @ onwrite (_stream_writable.js:270:7) 2016-09-01t18:46:36.836689+00:00 app[web.1]: @ writablestate.onwrite (_stream_writable.js:97:5) 2016-09-01t18:46:36.836689+00:00 app[web.1]: @ aftertransform (_stream_transform.js:99:5) 2016-09-01t18:46:36.836690+00:00 app[web.1]: @ transformstate.aftertransform (_stream_transform.js:74:12) 2016-09-01t18:46:36.836691+00:00 app[web.1]: @ 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]: @ websocket.wrapper (/app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/lodash/lodash.js:4762:19)
any ideas why not working? i've used #general , general test functionality of hasn't worked
in slack api, channels not names, instead form of ids. can tell id channel id when first letter "c", in c024be91l
.
i believe need channel id name. that, use channels.list
method fetch channels, , array#find
find proper channel 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 not call api every time bot receives message, , should work.
ps can see discussion of similar problem in issue on github.
Comments
Post a Comment