【问题标题】:Posting message with Slack RTM api使用 Slack RTM api 发布消息
【发布时间】:2017-03-10 05:29:18
【问题描述】:

我正在关注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)

非常感谢任何帮助。

【问题讨论】:

标签: slack-api slack chatbot rtm


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这篇文章可能很旧,但我想分享一下我对这个错误的经验。我也在测试这段代码,我使用的是私人频道。即使机器人已经是频道的成员,它也会抛出此错误。然后我尝试使用公共频道然后它通过了。我希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      您不能使用频道名称、用户名或用户 ID。请改用频道/组/DM ID。

      变化:

      var channel = "#general";
      

      收件人:

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

      您可以从您的频道 URL 中获取此频道 ID:

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

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

      • 在您应用的设置页面上,点击导航菜单中的 OAuth & Permissions 设置项。
      • 在 Scopes 部分中,添加 chat:write 权限范围,然后单击 Save Changes。
      • 现在您已经更改了应用的范围,您需要重新安装它 - 您应该会在屏幕顶部附近看到一个黄色横幅,告诉您单击此处重新安装您的应用。点击它,进入权限授权页面。
      • 您将被重定向回 OAuth 和权限页面,您可以在该页面顶部看到您的工作区令牌 - 将其存储起来以备日后使用。

      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);
      });
      

      【讨论】:

        猜你喜欢
        • 2015-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多