Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending message to different channel via slack webhook fails

I'm not sure if I am understanding the way how to use webhooks quite right, but: I want to send messages in different channels or to different users (not at once), without being involved in that conversation.

My problem: I can only create webhooks for specific users, so I end up having a unique URL for every user / channel? If I use something like the "channel" parameter or even the "setReceipent" method from the library I use, it doensn't have any effect and only the channel / user the webhook was created for, receives the message.

Do I need to use API access or can I accomplish my needs using webhooks?

like image 715
mrvnklm Avatar asked Jul 22 '18 16:07

mrvnklm


People also ask

Can you send a Slack message to multiple channels?

Option 1 - Slack Scheduler Slack scheduler is a quick tool that is designed to free up time and let people use Slack at the most efficient time. It comes with a built in advanced scheduler that allows you to schedule messages to multiple channels, users or conversations.

Is Slack webhook URL sensitive?

The unique webhook URL is secret. The webhook only accepts data, and thus alone cannot expose sensitive data to third parties.


2 Answers

In general incoming webhooks are fixed to the configured channel. So if you want to send messages to users and/or multiple channels you need to create multiple webhooks or send messages though the API (e.g. chat.PostMessage).

However, there is a another way to create webhooks, that allows you to send messages to every channel with the same webhook by adding a channel override property ('channel') to your message. Its how incoming webhooks used to work in the past and part of legacy custom integrations.

To create such a webhook you need to install an app called "Incoming webhooks" from the Slack App Directory (app is made by the Slack team).

Syntax:

POST https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Content-type: application/json
{
    "text": "Hello, world.",
    "channel": "U12345678"
}

Where U12345678 is the Slack ID of the user you want to send a direct message to.

Note that the channel property is optional and the message will be send to the default channel if omitted.

See here fore the full documentation.

like image 150
Erik Kalkoken Avatar answered Oct 21 '22 15:10

Erik Kalkoken


Hooks only allow you to send to the channel defined in the hook. If you want to send to any channel you need to create a bot user who can post to any channel. To create a bot user you need to do the following:

  1. Add an API app Once done create a bot user (or create and delete a webhook )
  2. Create a bot user or create and delete a webhook (which will create the bot user for you)
  3. Add chat:write and possibly chat:write.public to the OAuth & Permissions of the API App.
  4. Grab the Bot User OAuth Access Token it should start xoxb
  5. Post to https://slack.com/api/chat.postMessage eg
curl -X POST \ 
  -H 'Authorization: Bearer xoxb-###-###-***' \
  -H 'Content-type: application/json' \
  --data '{"channel": "#general","text":"Hello, World!"}' \
  https://slack.com/api/chat.postMessage

https://api.slack.com/messaging/sending#publishing provides some details

like image 28
Timothy c Avatar answered Oct 21 '22 15:10

Timothy c