When calling rtm.sendMessage('test', ...) with a username like @test, or userid provided from message.user (like U3B4XADP9) it does not work. Instead a SlackRTMError is returned with 'invalid channel id'.
The only way I've found to DM a user or send a message to a chat is to hardcode the channel found from message.channel (like G3BBUJ7HE).
The docs make it sound like this should work: https://slackapi.github.io/node-slack-sdk/bots#posting-a-message
However the docs here: https://api.slack.com/methods/chat.postMessage discuss Channels and explain some of these issues.
So what's broken? Is it the API, or are the docs inconsistent?
A fair question, thank you for raising it! At the lowest level, the RTM API only allows you to specify a channel/group/DM id. You cannot use a channel name, a user name, or a user id. This is clearly a problem with the documentation for this project.
Please note that chat.postMessage is a _different_ API from the RTM API鈥攁nd it is much more permissive about what it will accept.
ahh cool, docs problem then.
how do you go about messaging a private user then? is there a way to DM them without having to check if you already have a conversation with them?
You need to know the DM id. Each DM conversation has a DM id that is separate from the participants' user ids. To participate in a DM using the RTM API, you'll need to start a DM using the im.open web API endpoint, and then cache the resulting DM id for future use.
The docs are broken.
Put this in the README.md example:
let channel;
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
for (const c of rtmStartData.channels) {
if (c.is_member && c.name ==='general') { channel = c.id }
}
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
});
Then the example will work with a copy and paste.
So if I understand this correctly, in order to send a DM to a user, I need to use the im.open web API endpoint. But a custom bot user (internal integration) which does not have the authorization token required to use the web API endpoint, how does it send a DM? Or in order to send a DM, I have to attach the bot user to a Slack app and do the OAuth dance? Or is xoxb- token enough for using the web API?
So, I got around this issue by using the Web API im.open to get the DM ID and then use the RTM API to send a DM to a user. Apparently, it's possible to use the Web API with a custom bot user's auth token.
closed in #347
I want to reopen this case.
I've spent days to find this information.
It's still not clear how limited the RTM API is from the regular documentation.
@BerndErnst thanks for the feedback. Your experience is valuable, since as maintainers it鈥檚 harder for us to unlearn ideas to document them in the best way.
Which information did this issue have that you thought should have been more clear in the documentation? Any suggestions on changes?
It's been a little while here. I hope that you've gotten past any issues you were having. Closing now, but feel free to open a new issue if there's still a problem we can solve.
Most helpful comment
The docs are broken.
Put this in the README.md example:
Then the example will work with a copy and paste.