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

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]: 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)

知道为什么它不起作用吗?我已经使用 #general 和 general 来测试它的功能,但它没有用

在 Slack API 中,渠道不是他们的名字,而是某种形式的 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

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

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

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

来发送它

再次感谢