9

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?

mrvnklm
  • 1,348
  • 2
  • 10
  • 19

3 Answers3

22

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.

Gregoire
  • 3,735
  • 3
  • 25
  • 37
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • I already tried and tried again now. `{ "text":"Hello, World!", "channel": "CHANNEL_ID" }` I also used #CHANNEL_NAME, #CHANNEL_ID, CHANNEL_ID – mrvnklm Jul 22 '18 at 17:40
  • It works in principle, so the problem must lie somewhere else. e.g. a bug in your code or your library does not support this. Add your code to the question if you want more help – Erik Kalkoken Jul 22 '18 at 18:01
  • 1
    I dont even use code, I send a plain request: `curl --request POST \ --url https://hooks.slack.com/services/XXXXXXXX \ --header 'content-type: application/json' \ --data '{ "text":"Hello, World!", "channel": "XXXXXXxX" }'` – mrvnklm Jul 22 '18 at 18:31
  • yes. that exact curl command works for me. what error / reply do you get? and please put those in your question – Erik Kalkoken Jul 22 '18 at 19:03
  • well, I would if I would get an error. the only thing that happens is that the person I chose for the webhook receives the message or is there a different way to create the webhook? – mrvnklm Jul 23 '18 at 08:30
  • 1
    ok, I think I might have found your problem. there are two ways to create webhooks and if you create one as part of a Slack app the channel override does not seam to work. I will update my answer – Erik Kalkoken Jul 23 '18 at 12:22
  • Working now! Thanks a lot. Besides that, whats the matter of using the API anymore? – mrvnklm Jul 24 '18 at 08:25
  • experiencing the same issue on incoming webhooks created by both ways(legacy slack app, part of the slack app). @mrvnklm did you try anything special to get that work correctly? – Anoop Philip Sep 05 '18 at 02:54
  • 1
    Did you really use "Incoming Webhooks" App from Slack and added this to your Slack? @AnoopPhilip – mrvnklm Sep 05 '18 at 09:49
  • I can confirm it works with the Incoming Webhooks app from the App Store. If you have problems, maybe your user has no access to the channel you are trying to send to? Try testing it with a public channel. – Erik Kalkoken Sep 05 '18 at 10:57
  • 5
    @Erik Kalkoken, that was it. if default channel is a private one then that webhook can't send the message to other channels. Thanks!! – Anoop Philip Sep 05 '18 at 20:25
  • @M.Abulsoud Hey, you may have a different problem. Would suggest opening a new question for it. – Erik Kalkoken Jul 01 '20 at 10:21
  • @ErikKalkoken I look at the docs of the new slack apis it's not possible to link a webtoken with multi channel. – M.Abulsoud Jul 01 '20 at 14:09
  • @M.Abulsoud I see. There is another approach. Not 100% sure if that still works, but worth a try: https://stackoverflow.com/questions/51878825/slack-incoming-webhook-sends-as-my-user/51884185#51884185 – Erik Kalkoken Jul 01 '20 at 14:36
3

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

Timothy c
  • 751
  • 7
  • 8
0

As others mentioned, in legacy integrations it was possible to send msg to other channels using webhooks but in their replacement(Slack apps) which you are probably using, it's not possible anymore, you should call slack api(using the provided auth token) instead of using webhooks for this purpose.

From the docs:

The majority of your legacy code for sending messages using incoming webhooks should continue to work within a Slack app without much modification; the only thing you can no longer do is customize the destination channel and author identity at runtime.

https://api.slack.com/legacy/custom-integrations/messaging/webhooks

You can still use legacy integration but it'll be deprecated in the future and is not recommended