Javascript 无法使用Hubot将消息发送到松弛的特定通道-SlackRTMError:无通道id

Javascript 无法使用Hubot将消息发送到松弛的特定通道-SlackRTMError:无通道id,javascript,slack,hubot,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);

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

代码:

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应用程序[web.1]:未处理的拒绝 SlackRTMError:无通道id

2016-09-01T18:46:36.836677+00:00应用程序[网站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应用程序[网站1]:在 RTMClient.\u 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应用程序[网站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应用程序[网站1]:位于WebSocket.emit (events.js:98:17)2016-09-01T18:46:36.836684+00:00应用程序[网站1]:在 接收文本 (/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应用程序[网站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应用程序[网站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应用程序[网站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应用程序[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应用程序[网站1]:在 WritableState.onwrite(_stream_writable.js:97:5) 2016-09-01T18:46:36.836689+00:00 app[web.1]:在转换后 (_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 app[web.1]:在Zlib.callback(Zlib.js:456:5) 2016-09-01T18:46:36.836691+00:00应用程序[网站1]: 2016-09-01T18:46:36.836681+00:00应用程序[网站1]:位于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。为此,请使用方法获取所有通道,然后使用
Array#find
按其名称查找适当的通道:

bot.api.channels.list((error, response) => {
  const targetChannel = response.data.find(channel => channel.name === message.match[1]);

  robot.send(targetChannel.id, sentence);
});
只需对其进行优化,使其不会在每次bot接收到消息时调用此API,它应该可以工作


PS您甚至可以看到类似问题的讨论。

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

for(x in msg.envelope) {
    console.log(x + " : " + msg.envelope[x]);
} 
这是检查msg对象变量的最佳方法。另外,我必须使用msg.messageRoom(roomID,msg)而不是robot.send发送它


再次感谢

建议
频道
未定义,您是否尝试记录它以确保它符合您的预期?在这段代码中,
机器人
是否与
机器人
对象相同?正如我无法确定
机器人
来自何处一样,我无法确定
机器人
机器人
是否相同。我使用的是官方文件中广泛使用的术语。但它们应该是一样的。