gpt4 book ai didi

javascript - 无法使用 Hubot 将消息发送到 Slack 中的特定 channel - SlackRTMError : no channel id

转载 作者:行者123 更新时间:2023-11-30 15:51:21 25 4
gpt4 key购买 nike

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

代码:

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 中, channel 不是它们的名称,而是某种形式的 ID。首字母为“C”的ID可以判断为 channel ID,如C024BE91L

我相信您需要的是通过名称获取 channel ID。为此,请使用 channels.list方法来获取所有 channel ,然后 Array#find 通过名称找到合适的 channel :

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 .

关于javascript - 无法使用 Hubot 将消息发送到 Slack 中的特定 channel - SlackRTMError : no channel id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39280490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com