36

How is it possible to send message in slack directly to the user, by user.id as application. enter image description here

this application has scope: bot,channels:write,emoji:read,users:read,users:read.email

I find how to send message only as DM or by webhooks, but there is no scope for that. Any one has idea?

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
Koloritnij
  • 1,167
  • 1
  • 8
  • 15

5 Answers5

56

If I understand your question correctly, you want to send direct messages to users in the app channel instead of the standard slackbot channel.

In order to do that you need to

  1. Your app needs the bot scope and a bot user
  2. Open a direct message channel from your app with the user with conversations.open. You get back a direct message ID.
  3. Send a message with chat.postMessage to the the direct message channel ID

Make sure to use your bot access token (not the user access token) from your Slack app.

The bot scope gives you all permissions needed to open and send DMs to users from your bot channel. No other scopes are required.

You can also use the new conversations methods, which work for all kind of channel types to do the same.

See also this question on the same topic.

knutole
  • 1,709
  • 2
  • 22
  • 41
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • as you can see example application hasn't scopes for that. Also I've tried like you showed and I wrote to the DM as user who added app to the workspace(I mean that in his DM channels it will be written) – Koloritnij Dec 13 '17 at 10:14
  • You can see the scopes on the documentation pages I linked, but I am happy to add them to my answer. – Erik Kalkoken Dec 13 '17 at 13:48
  • 1
    You are correct. I did some testing with scopes and it turns out you only need the `bot` scope, which is included in your example app. I have updated my answer accordingly. – Erik Kalkoken Dec 13 '17 at 15:30
  • 1
    You are my superhero!) thanks a lot :) btw: I can't understand why I used user token and didn't try it with bot token.. – Koloritnij Dec 14 '17 at 07:06
  • @ErikKalkoken Can you please post a sample code for im.open in python? – Mithilesh_Kunal Nov 30 '18 at 11:49
  • 3
    Just found out that `im:open` is deprecated, "It will stop functioning in February 2021 and will not work with newly created apps after June 10th, 2020" per https://api.slack.com/methods/im.open. `conversations.open` is to be used. – Jochem Schulenklopper Jun 03 '20 at 08:50
  • i used chat.postMessage & as_user: true. with specific user's token. But how can i get those user tokens dynamically. for sending direct message from user2 to user3, user3 to user2, user4 to user6 etc. – Nikhil Kadam Jul 07 '20 at 06:34
  • how can i change sender of chat.postMessage dynamically. – Nikhil Kadam Jul 07 '20 at 06:37
28

There is an alternative way to solve this, which can be more suitable if your app uses a bot to operate with Slack API.

You need to call chat.postMessage API method and specify channel argument equal to the user ID (e.g. U0G9QF9C6) you want to message and as_user argument is true. Important detail - ensure you are using bot access token (learn here how to obtain it).

Example:

curl -X POST "https://slack.com/api/chat.postMessage" -H  "accept: application/json" -d token=BOT_ACCESS_TOKEN -d channel=U0G9QF3C6 -d text=Hello -d as_user=true

In this way, your message will be always sent on behalf (name and icon) of your bot and will be display like a direct message in the app channel (YourAppChannel in the Slack sidebar).

Compared to the approach of @ErikKalkoken you have no need to create a channel in advance and as a result, keep track of its ID (it may be good or bad depending on your needs).

Ruslan Isay
  • 933
  • 7
  • 10
  • I like this approach, because you only need one instead of two calls to the Slack API for this. – Erik Kalkoken Feb 28 '19 at 16:47
  • You also need chat:write:user permission. – quantoid Oct 25 '19 at 18:35
  • I keep getting ```{"ok":false,"error":"not_in_channel"}``` do i need to manually add the bot to a channel?? – Abhijeet Bajracharya Feb 04 '20 at 08:04
  • 3
    Not sure since when, but new Slack apps don't use the `as_user` parameter anymore, per https://api.slack.com/methods/chat.postMessage#authorship. – Jochem Schulenklopper Jun 03 '20 at 08:30
  • How if I want to post the message to the direct message of the user instead of the app channel in the Slack sidebar? I tried the following command `curl -X POST "https://slack.com/api/chat.postMessage" -H "accept: application/json" -d token=BOT-TOKEN -d channel=MEMBER-ID -d text=Hello`, where MEMBER-ID is the ID of the user. Instead of posting to the direct message of the user, it's posting to the app channel in the Slack sidebar. – Jag Sep 15 '21 at 09:11
1

For those who is still searching for detailed answer:

  • First of all you need to make call to this endpoint. You need to make call with bot token and provide into users param value of user you want to send message. Also you need set prevent_creation and return_im to true. Example:
Authorization: Bearer {your_bot_token}
{
  "users": "U12345679",
  "prevent_creation": true,
  "return_im": true
}
  • After that you will have your channel id to which you want to send message. Example response:
{
    "ok": true,
    "no_op": true,
    "already_open": true,
    "channel": {
        "id": "D123456789", <-- this is your id
        ...
        "unread_count": 0,
        "unread_count_display": 0,
        "is_open": true,
        "priority": 0
    }
}
  • and then with same bot token and user id send message with help of this one

Blockquote I keep getting {"ok":false,"error":"not_in_channel"} do i need to manually add the bot to a channel?? – Abhijeet Bajracharya Feb 4 '20 at 8:04

you need to get scope that allow to send messages like this

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 15 '21 at 17:43
0

There is no need to use the conversation.info, you can post message (DM), by using the users.list endpoint and fetch the user id, which then you can use in chat.postMessage

Akos
  • 161
  • 1
  • 5
0

If possible, please refrain from using chatPostMessage to deliver the message. You should initiate a conversation and send the message accordingly.

The correct code for sending the message is as follows:

https://github.com/password123456/slack_api_example/tree/main/bot_sending_direct_message_to_user

  • 1
    It is a very good Idea to share the link, however since links might be unavailable in the future, you'd better to explain about what the user is going to see on that page/link. – Shila Mosammami Aug 30 '23 at 14:02