无法使用Hubot - SlackRTMError向Slack中的特定频道发送消息:无频道ID

时间:2016-09-01 20:37:40

标签: javascript slack hubot

简单的脚本,但它无法正常工作..我正在尝试将消息发送到由用户输入确定的特定频道。

代码:

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]: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]:在afterWrite   (_stream_writable.js:278:3)2016-09-01T18:46:36.836688 + 00:00   app [web.1]:atwrite(_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]:在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]:at WebSocket.wrapper   (/app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/lodash/lodash.js:4762:19)

为什么它不起作用的任何想法?我已经使用#general和general来测试它的功能但它没有用

2 个答案:

答案 0 :(得分:1)

在Slack API中,通道不是它们的名称,而是某种形式的ID。当ID的第一个字母为“C”时,您可以通过ID告知它是通道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,它应该可以正常工作。

PS您甚至可以看到类似问题in this issue on Github的讨论。

答案 1 :(得分:1)

谢谢!我使用msg.envelope.channel找到了id。我不知道它存储在哪里,所以我无法解决它。

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

这是检查msg对象变量的最佳方法。另外,我必须使用 msg.messageRoom(roomID,msg)而不是robot.send发送它。

再次感谢

相关问题