3

I'm following the tutorial here https://slackapi.github.io/node-slack-sdk/bots#posting-a-message and I'm flummoxed why I can't get this portion of the tutorial code to work. I copied and pasted the code from this section, which is below

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

Since the first section of the tutorial code worked, the problem is definitely coming from the last 3 lines of code. Presumably its an issue with the event. My error message is

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)

I would really appreciate any help.

Gwater17
  • 2,018
  • 2
  • 19
  • 38
  • 1
    Realized I'm facing the issue brought up in these links https://github.com/slackapi/node-slack-sdk/issues/251 https://github.com/slackapi/node-slack-sdk/issues/300#issuecomment-266804306 – Gwater17 Mar 10 '17 at 06:11

3 Answers3

0

Possibly your bot did not join the #general channel yet. Invite him to the channel first.

dux2
  • 1,770
  • 1
  • 21
  • 27
0

This post might be old but I'd like to share my experience with this error. I was testing this code too and I was using a private channel. Even if the bot is already a member of the channel it throws this error. Then I tried to use a public channel then it went through. I hope this helps.

Þaw
  • 2,047
  • 4
  • 22
  • 39
0

You cannot use channel names, usernames, or user ids. Use channel/group/DM ids instead.

Change:

var channel = "#general";

To:

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

You can grab this channel ID from your channel's URL:

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

And your bot must be added to the target channel, as detailed here:

  • On your app's settings page, click the OAuth & Permissions settings item in the navigation menu.
  • In the Scopes section, add the chat:write permission scope, then click Save Changes.
  • Now that you've changed the scopes for your app, you'll need to install it again - you should see a yellow banner near the top of the screen telling you to click here to reinstall your app. Click it, and follow through the permissions authorization page.
  • You'll be redirected back to the OAuth & Permissions page, where you can see your workspace token listed at the top of the page - store this for use later on.

SLACK API REFERENCE

This code will work as expected:

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);
});
Daniel Vukasovich
  • 1,692
  • 1
  • 18
  • 26