[英]Unable to send a message to specific channel in Slack with Hubot - SlackRTMError: no channel id
简单的脚本,但不能正常工作。我正在尝试将消息发送到由用户输入确定的特定通道。
码:
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);
});
}
当我运行它时,hubot引发以下错误:
2016-09-01T18:46:36.836661 + 00:00 app [web.1]: 未处理的拒绝SlackRTMError:无通道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-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]:在/ 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]:在写入时(_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应用程序[web.1]:位于TransformState.afterTransform(_stream_transform.js:74:12)2016-09-01T18:46:36.836691 + 00:00应用程序[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)
任何想法为什么它不起作用? 我已经使用#general和general来测试它的功能,但是没有用
在Slack API中,通道不是其名称,而是某种形式的ID。 您可以通过ID知道它的第一个字母为“ C”时,它就是通道ID,就像C024BE91L
。
我相信您需要的是通过其名称获取频道ID。 为此,请使用channels.list
方法获取所有通道,然后使用Array#find
通过其名称查找合适的通道:
bot.api.channels.list((error, response) => {
const targetChannel = response.data.find(channel => channel.name === message.match[1]);
robot.send(targetChannel.id, sentence);
});
只需对其进行优化,以使每次您的机器人收到一条消息时都不会调用此API,它就可以正常工作。
谢谢! 我使用msg.envelope.channel找到了ID。 我不知道它的存储位置,所以无法解决。
for(x in msg.envelope) {
console.log(x + " : " + msg.envelope[x]);
}
那是检查msg对象变量的最佳方法。 另外,我必须使用msg.messageRoom(roomID,msg)而不是robot.send发送它。
再次感谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.