Search code examples
curlslackslack-api

Incoming WebHook and dynamic user-name/channel


I was using Legacy token and I was able to send any channel with any user name. But when I switch to use incoming webhook feature then its only works for only one channel that we selected during creating webhook. Here is what I am using,

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!","channel":"#my-channel" }' https://hooks.slack.com/services/XX/XX/XX

Here message is always sent to default channel instead of channel that I am specifying in CURL.

What is easiest way to send slack message to any channel with any user-name (other than legacy token)? I am also not seeing any client credentials(server to server) flow


Solution

  • If you want to send message to any channel with a custom username your best option is to use the Slack API with chat.postMessage.

    Note that you will need an Oauth token with the necessary scopes (e.g. chat:write:user), which requires you to create a Slack app and auth it to your workspace.

    If you are working within your own workspace, you can get your auth done directly within the Slack App management page (See internal integrations). Otherwise you will need to implement an Oauth flow for your app (see Add To Slack).

    Here is an example curl:

    curl https://slack.com/api/chat.postMessage -X POST --data "token=TOKEN&channel=test&username=Luke Skywalker&icon_emoji=:dog:&text=The force is with me"