This action wraps the Slack chat.postMessage API method for posting to channels, private groups, and DMs. This action sends messages using Slack bot tokens, which have two main advantages compared to user tokens and incoming webhooks: (1) Bots can't be disabled inadvertently when a Slack user is disabled or removed. Slack has written about this in a recent announcement, and (2) Bots offer a powerful range of capabilities that can be leveraged to perform more functions.
- name: Notify slack
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
uses: pullreminders/slack-action@master
with:
args: '{\"channel\":\"C1234567890\",\"text\":\"Hello world\"}'
Here's what the Slack message would look like:
To use this GitHub Action you'll first need to create a Slack App and install it to your Slack workspace.
To use this GitHub Action, you'll need to set a SLACK_BOT_TOKEN
secret on GitHub. To get your Slack bot token, browse to the "OAuth & Permissions" page listed in Slack and copy the "Bot User OAuth Access Token" beginning in xoxb-
.
Slack's chat.postMessage method accepts a JSON payload containing options — this JSON payload should be supplied as the argument in your GitHub Action. At a bare minimum, your payload must include a channel ID and the message. Here's what a basic message might look like:
- name: Notify slack
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
uses: pullreminders/slack-action@master
with:
args: '{\"channel\":\"C1234567890\",\"text\":\"Hello world\"}'
Please note that if you are using the visual editor you should not escape quotes because GitHub will automatically escape them for you.
A "channel ID" can be the ID of a channel, private group, or user you would like to post a message to. Your bot can message any user in your Slack workspace but needs to be invited into channels and private groups before it can post to them.
If you open Slack in your web browser, you can find channel IDs at the end of the URL when viewing channels and private groups. Note that this doesn't work for direct messages.
https://myworkspace.slack.com/messages/CHANNEL_ID/
You can also find channel IDs using the Slack API. Get a list of channels that your bot is a member of via Slack's users.conversations endpoint. Get user IDs for direct messages using Slack's users.lookupByEmail endpoint
If the channel is private, you'll need to install the App in that channel.
Please refer to Slack's documentation on message formatting. They also have a message builder that's great for playing around and previewing messages. Your messages can contain attachments, markdown, buttons, and more.
The Dockerfile and associated scripts and documentation in this project are released under the MIT License.