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

有些话、适合烂在心里 提交于 2019-11-27 05:07:07

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!