Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send message to Slack listening channels?

i am working on a slack app (scope including bot and incoming webhooks). I can send message to a defined channel but i don't know how to stop using the "&channel=" parameter and just send messages to listening channels. By listening i mean, when the app is installed, user is asked where to post (channel or dm has to be chosen).

String postUrl = "https://slack.com/api/chat.postMessage?token=" + botAccessToken + "&as_user=false&channel=%23community&text=testing&username=CommunityQ";

Any hints would be useful.

like image 868
Gabi Radu Avatar asked Jan 27 '16 18:01

Gabi Radu


4 Answers

I think you can't: According to the official Slack API documentation it is not possible to send messages to all/multiple listening channels:

Incoming webhooks output to a default channel and can only send messages to a single channel at a time. You can override a custom integration's configured channel by specifying the channel field in your JSON payload.

I interpret this as "there is always exactly one channel your message is sent to"

Furthermore, Slack restricts this channel override feature for Slack apps:

You cannot override the default username, icon, or channel for incoming webhooks attached to Slack apps.

like image 108
Michael Schaefers Avatar answered Sep 26 '22 11:09

Michael Schaefers


I think there is a slight confusion here. I am not sure what the "incoming webhook" scope does that the "bot" scope cannot do. Here's how I see things

  • Either you want a lightweight, low-permission app that posts in one channel, and you'd use the incoming webhook scope
  • Or you want an app that can ask questions to the users, like which channels they'd like to have updated posted to, process answers, etc. Then you'd use the bot scope, and the bot can post in any public channel

If you give a bit more details in what you want to achieve we can perhaps help you better

like image 21
Matthieu Avatar answered Sep 22 '22 11:09

Matthieu


If you want a more dynamic approach on sending messages into any channel I would suggest to user the API method chat.postMessage instead of an incoming webhook.

The API method can post messages into any channel (including private channels, DMs) as long as your token as access to it.

like image 36
Erik Kalkoken Avatar answered Sep 24 '22 11:09

Erik Kalkoken


As Michael mentioned in his answer, it is not possible to post to multiple channels at the same time. You have to look through them and do multiple requests.

like image 40
Roman Avatar answered Sep 24 '22 11:09

Roman