4

When creating a slack app, it creates a new "channel" in the left hand menu. I want to be able to send a message to specific users and not to all users in a workspace who have integrated with the app.

For example, if I make the following request:

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/ABxxx/CDxxx/EFxxxxxx

It will send a message to all users who have integrated with my app with the text "Hello World".

But I only want to send a message to user A without User B being notified.

I don't want to message a user directly and it to appear to come from slack bot. I want the message to appear to come from my bot / app.

How can this be achieved via slack API?

I found this quite hard to explain so please let me know if you'd like me to clarify anything.

enter image description here

Stretch0
  • 8,362
  • 13
  • 71
  • 133
  • 1
    Possible duplicate of [Using Slack, how do you send direct message to user based on their "Member ID?](https://stackoverflow.com/questions/48347073/using-slack-how-do-you-send-direct-message-to-user-based-on-their-member-id) – Erik Kalkoken Feb 12 '19 at 21:55
  • Unfortunately not. That post is asking how to message users directly where the message appears to the user to come from slack bot. I want my message to appear to come from my bot / app. – Stretch0 Feb 13 '19 at 16:25
  • The message will come from the app, but will appear in the Slack bot channel. If you want your message to the user to appear in the app channel check out this answer: https://stackoverflow.com/questions/47753834/how-to-send-direct-messages-to-a-user-as-app-in-app-channel/47781750#47781750 – Erik Kalkoken Feb 13 '19 at 22:01

1 Answers1

3

The problem of your request that you are using a hook URL which is bound to a particular channel (you pick it during Slack App installation).

To send a direct message to the user on behalf of your bot, you need to consider the following things (this is not the single way to achieve it, but works for me):

  1. Ensure you have a bot registered for your Slack App.
  2. Ask for bot and chat:write:bot permissions during App installation process (example for Slack Install button and here).
  3. Store the bot access token on successful installation (see for details).
  4. Now using the bot access token you can send Slack API requests.
  5. To achieve what you need, use chat.postMessage API method. channel argument can be user ID (e.g. U0G9QF9C6). By setting up as_user argument to true, your message will be always sent on behalf (name and icon) of your bot (seems for bot tokens it's always like this, but it's recommend it to specify it explicitly).

Hope it helps. Feel free to ask for details.

Ruslan Isay
  • 933
  • 7
  • 10
  • Thanks Ruslan. That makes sense but how can I get a channel ID for an apps channel? As seen in the picture I have added to my OP, apps channels seem to exist in their own area. Can I get the ID's for these channels some how? It didn't seem to return under the https://slack.com/api/channels.list call – Stretch0 Feb 13 '19 at 17:13
  • If you want to send a message from bot to the user - you don't need to know any channel ID. User ID should be used as `channel` argument of API method. – Ruslan Isay Feb 13 '19 at 17:17
  • What is illustrated on your screenshot is how the user sees the bot. It's like direct messaging with the bot. So when you sending API request using bot token, it's like your bot sending the direct message to the user and this user will see a notification in the area on your screenshot. – Ruslan Isay Feb 13 '19 at 17:21
  • So when a bot sends a message using the we hook, will all users who have integrated with my bot see a message? How can I limit which users get a notification? – Stretch0 Feb 13 '19 at 17:57
  • Yes. hook will send a message to a channel, so all channel participants will see the message. If you want to target a particular set of users, you need to loop over the list of users and send multiple direct messages. – Ruslan Isay Feb 14 '19 at 02:29
  • Is there anyway to get the message to show up in the "Direct Messages" column instead of the "Recent Apps" column? Like, make the app look like a user? Would I need to create a user and use OAuth to have the bot work on behalf of the user? – chrisallick Apr 02 '20 at 23:19