Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Send a message with the RTM API #50

Closed
gazpachu opened this issue Dec 5, 2016 · 5 comments
Closed

Send a message with the RTM API #50

gazpachu opened this issue Dec 5, 2016 · 5 comments

Comments

@gazpachu
Copy link

gazpachu commented Dec 5, 2016

Sounds silly but I'm struggling with this! I have a connection, can see the channels, users, etc but I cannot send a message. What am I doing wrong?

this.bot.message({
    type: 'message',
    channel: this.state.currentChannel.id,
    text: text
});

When I do that, nothing happens :( I've also tried with a properly formatted JSON (using quotes)

@brianleroux
Copy link
Member

brianleroux commented Dec 6, 2016

np! there's no this nor message

something like this would work tho

var slack = require('slack')
var bot = slack.rtm.client()

var token = process.env.SLACK_BOT_TOKEN

// define a hello handler
bot.hello(msg=> {
     // NOTE you need to set text and channel here
    slack.chat.postMessage({text, channel, token}, console.log)
})

bot.listen({token})

@gazpachu
Copy link
Author

gazpachu commented Dec 6, 2016

Thanks Brian. The postMessage method is part of the Web API. I'm trying to send a message with the RTM API.

@brianleroux
Copy link
Member

Ah! Yes you can do that too. (=

Fwiw: we only use the Web API for replies. Sockets can manifest tricky state issues especially when you double duty them for send and receipt logic (even though they are duplex in nature). Anyhow: still possible and totally appropriate for many cases!

To send a msg via the socket you can use bot.ws.send

var slack = require('slack')
var bot = slack.rtm.client()

var token = process.env.SLACK_BOT_TOKEN

// define a hello handler
bot.hello(msg=> {
     // NOTE you need to set text and channel here
    bot.ws.send({
      "id": 1,
      "type": "message",
      "channel": "C024BE91L",
      "text": "Hello world"
    })
})

bot.listen({token})

More info about the JSON payloads look for "Sending messages" on this page:

https://api.slack.com/rtm

@brianleroux
Copy link
Member

Also! slack.rtm.client above really is a super simple thin wrapper around ws:

https://github.com/smallwins/slack/blob/master/src/rtm.client.js

@brianleroux
Copy link
Member

LMK if this answers your questions / happy to help more if its still not working but closing this issue for now. Thx!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants