'How to send direct messages to a user as app in app channel

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?



Solution 1:[1]

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).

Solution 2:[2]

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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ruslan Isay
Solution 2
Solution 3 Akos