Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slack webhook and direct/private messages

Tags:

slack-api

I created a websocket-based Slack bot (that plays chess). In order to return a graphical representation of the board (so PNG instead of simple ASCII) I must use a webhook since normal messages cannot have attachments.

The interaction with the bot is through direct messages and I have 1 webhook. If I set the channel in the wehook to '@username' the message gets posted in that user 'slackbot' DM. But I want it to be posted in my bot's DM with that user.

How do I do that?

Or is there an alternative instead of a webhook?

Thanks.

Henry

like image 564
Henry Wilson Avatar asked Jun 11 '17 20:06

Henry Wilson


People also ask

Can Slack bot read private messages?

A user (or bot user) still needs to be member of a private / direct message channel in order to have access to it. It does also not matter what permission or role you have. Even the primary owner has no access to private channels he is not a member of.

Can Slack bot send direct message to user?

Bot users can't post to a direct message conversation between two users using chat. postMessage . If your app was involved in the conversation, then it would be a multi-person direct message instead.

How do I send a private message on Slack?

Open the channel or DM you'd like to send a message to. Click the message field. Type your message and add any attachments, emoji, mentions, or formatting you'd like. Press Enter to send it.

What can you do with a Slack webhook?

Message options Incoming webhooks allow you to add context to your messages by attaching helpful content and links. Things like images, websites, or other pieces of data make these messages more useful for your team.


1 Answers

Direct messages between bot and user

If you want to use a bot-specific direct message channel instead of the general slackbot channel you need to open a direct message channel just as you would between any two users.

Open the direct message channel from your bot to a user with im.open (which will provide you with the channel ID). Then send the message to that channel ID, e.g. with chat.postMessage. Important: Make sure you use the bot access token and not the general access token for all API calls.

Method for sending messages

I would recommend using the API method chat.postMessage instead of the webhook. It gives you more options than a webhook and of course also supports attachments.

like image 117
Erik Kalkoken Avatar answered Sep 27 '22 18:09

Erik Kalkoken