slack-api - 使用 Slack RTM api 发布消息

标签 slack-api slack chatbot rtm

我正在关注这里的教程 https://slackapi.github.io/node-slack-sdk/bots#posting-a-message我很困惑为什么我无法让这部分教程代码工作。我复制并粘贴了本节中的代码,如下

var RtmClient = require('@slack/client').RtmClient;
var RTM_CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS.RTM;

var bot_token = process.env.SLACK_BOT_TOKEN || ''; //I know the problem is not here.

var rtm = new RtmClient(bot_token);
rtm.start();

var channel = "#general"; //could also be a channel, group, DM, or user ID (C1234), or a username (@don)

// you need to wait for the client to fully connect before you can send messages
rtm.on(RTM_CLIENT_EVENTS.RTM_CONNECTION_OPENED, function () {
  rtm.sendMessage("Hello!", channel);
});

由于教程代码的第一部分有效,所以问题肯定来自最后 3 行代码。大概是事件的问题。我的错误消息是

Unhandled rejection Error
    at RTMClient.handleMessageAck [as _handleMessageAck] (/Users/mg/projects/slack_projects/games/s
lack_connect_four/node_modules/@slack/client/lib/clients/rtm/client.js:496:40)
    at RTMClient._handleWsMessageViaEventHandler (/Users/mg/projects/slack_projects/games/slack_con
nect_four/node_modules/@slack/client/lib/clients/rtm/client.js:459:12)
    at RTMClient.handleWsMessage (/Users/mg/projects/slack_projects/games/slack_connect_four/node_m
odules/@slack/client/lib/clients/rtm/client.js:419:10)
    at WebSocket.wrapper (/Users/mg/projects/slack_projects/games/slack_connect_four/node_modules/l
odash/lodash.js:4968:19)
    at emitTwo (events.js:106:13)
    at WebSocket.emit (events.js:191:7)
    at Receiver.ontext (/Users/mg/projects/slack_projects/games/slack_connect_four/node_modules/ws/
lib/WebSocket.js:841:10)
    at /Users/mg/projects/slack_projects/games/slack_connect_four/node_modules/ws/lib/Receiver.js:5
36:18
    at Receiver.applyExtensions (/Users/mg/projects/slack_projects/games/slack_connect_four/node_mo
dules/ws/lib/Receiver.js:371:5)
    at /Users/mg/projects/slack_projects/games/slack_connect_four/node_modules/ws/lib/Receiver.js:5
08:14
    at Receiver.flush (/Users/mg/projects/slack_projects/games/slack_connect_four/node_modules/ws/l
ib/Receiver.js:347:3) at Receiver.finish (/Users/mg/projects/slack_projects/games/slack_connect_four/node_modules/ws/
lib/Receiver.js:541:12)
    at Receiver.expectHandler (/Users/mg/projects/slack_projects/games/slack_connect_four/node_modu
les/ws/lib/Receiver.js:499:31)
    at Receiver.add (/Users/mg/projects/slack_projects/games/slack_connect_four/node_modules/ws/lib
/Receiver.js:103:24)
    at TLSSocket.realHandler (/Users/mg/projects/slack_projects/games/slack_connect_four/node_modul
es/ws/lib/WebSocket.js:825:20)
    at emitOne (events.js:96:13)

我非常感谢任何帮助。

最佳答案

可能您的机器人尚未加入#general channel 。先邀请他加入 channel 。

关于slack-api - 使用 Slack RTM api 发布消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42710920/

相关文章:

python - 如何以编程方式链接 slack 消息

slack-api - 显示 Slack channel 中的所有在线用户

ruby-on-rails - Slack 传入 Webhook API

slack - 将元数据/自定义数据附加到通过 API 发送的 slack 消息

node.js - 如何在 Azure Bot 服务上同时使用英语和法语 QnaMaker?

java - 如何更正无效协议(protocol): null sending mail and Could not to SMTP host: using javax.邮件?

Slack Events API 不适用于普通用户

javascript - 如果消息仅以标签开头,如何让 slackbot 回复

javascript - Botium-Core - 只需要断言 Alexa 响应的某些部分

node.js - 缺少脚本 : start & App crashed when deploying app to Heroku