使用Slack RTM api发布消息

我正在按照这里的教程https://slackapi.github.io/node-slack-sdk/bots#posting-a-message和我'm flummoxed why I can' t使这部分教程代码工作 . 我复制并粘贴了本节中的代码,如下所示

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)

我真的很感激任何帮助 .

回答(3)

2 years ago

可能你的机器人还没有加入 #general Channels . 首先邀请他加入 Channels .

2 years ago

这篇文章可能很旧,但我想分享一下这个错误的经验 . 我也在测试这段代码而且我正在使用私人 Channels . 即使僵尸程序已经是该 Channels 的成员,它也会抛出此错误 . 然后我尝试使用公共 Channels 然后它经历了 . 我希望这有帮助 .

2 years ago

您不能使用 Channels 名称,用户名或用户ID . 请改用 Channels /群组/ DM ID .

更改:

var channel = "#general";

至:

var channel = "C--------";

您可以从 Channels 的网址中获取此 Channels ID:

https://yourworkspace.slack.com/messages/C-------/details/

并且您的机器人必须添加到目标渠道,详情如下:

  • 在应用程序的设置页面上,单击导航菜单中的OAuth&Permissions设置项 .

  • 在“范围”部分中,添加聊天:写入权限范围,然后单击“保存更改” .

  • 既然您需要再次安装它 - 您应该会在屏幕顶部附近看到一个黄色 Banner ,告诉您单击此处重新安装您的应用 . 单击它,然后按照权限授权页面进行操作 .

  • 您将被重定向回OAuth&Permissions页面,您可以在其中查看页面顶部列出的工作区令牌 - 将其存储以供日后使用 .

SLACK API REFERENCE

此代码将按预期工作:

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

var rtm = new RtmClient('.....'); // your token
rtm.start();

let channel = 'C--------' ; //your channel

rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function () {
  rtm.sendMessage("Hello stack!", channel);
});