Search code examples
slackslack-api

Slack Bot send DM message to the User


I want to send message as DM to the users, so I've come with some questions;

1- There are two ways to send DM to the user;

  • the first one open a conversation with user by using conversation.open, and send message. Then if I want to send the message to the same user, I use conversation.list and find the conversationId by userId, then send message to the same channel again.
  • Second one is just basically using userId as channelId parameter in chatPostMessageRequest.

I've tried both ways, and both of them are sending message as DM. So, what is the purpose of using conversation.open? I'm asking this because nearly all the answers say you should do this etc.

2- Seems like there is no way to get multiple users by emails except users.list?

3- Also seems like there is no way to send messages to the multiple user at the same time (the content of the messages are different, those are depends on user data).


Solution

  • Sending a direct message by user ID works with chat.postMessage as a convenience, a shortcut. But it's the only method/API that allows you to use a user ID as a channel ID, so Slack warns developers away from relying on it in totality -- if you use conversations.open and the resultant conversation ID, it'll work for other methods like chat.update should you need to edit the message after sending it.

    Slack doesn't offer many "lookup" APIs -- most things you'll want to look up require fetching an entire data set (like a list of users with all pages of users.list) and then filtering the results yourself. There are no APIs to look up multiple users at once via email address.

    There is also no way to send the same direct message content to multiple users with a single request. In most cases, when an app is sending the same message to multiple users the best practice would be to use a channel to broadcast the message a single time, with the intended recipients as members of the channel.