Henry Wilson
Henry Wilson

Reputation: 141

Slack webhook and direct/private messages

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

Upvotes: 7

Views: 14377

Answers (1)

Erik Kalkoken
Erik Kalkoken

Reputation: 32698

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.

Upvotes: 8

Related Questions