Skip to content
bell

GitHub Action

SlackBot postMessage

v1.2.0 Latest version

SlackBot postMessage

bell

SlackBot postMessage

Send Slack Messages by a Bot

Installation

Copy and paste the following snippet into your .yml file.

              

- name: SlackBot postMessage

uses: parkingboss/[email protected]

Learn more about this action in parkingboss/slackbot-postmessage

Choose a version

Post Slack messages

This action is roughly the same as all the others, but it exposes the full chat.postMessage api and (critically in our opinion) exposes the response from Slack. This document in particular is heavily cribbed from Slack-Action, as their instructions were excellent, readable, and included some great screenshots which we also borrowed. Any section of this document taken wholesale from Slack Action will be labeled like so:

From Slack-Action

The code for this action differs significantly from Slack Action in that it uses the javascript runtime which is cross platform and faster to boot.

Usage:

- name: Send Slack Message
  uses: parkingboss/slackbot-postmessage@v1
  with:
    token: ${{ secrets.SLACK_BOT_TOKEN }}
    request: '{\"channel\":\"CHANNEL ID\",\"text\":\"Hello world\"}'

Setup

From: Slack-Action

To use this GitHub Action you'll first need to create a Slack App and install it to your Slack workspace.

Creating a Slack App

From: Slack-Action

  1. Create a Slack App. Go to Slack's developer site then click "Create an app". Name the app "GitHub Action" (you can change this later) and make sure your team's Slack workspace is selected under "Development Slack Workspace" (see screenshot).
  2. Add a Bot user. Browse to the "Bot users" page listed in the sidebar. Name your bot "GitHub Action" (you can change this later) and leave the other default settings as-is (see screenshot).
  3. Set an icon for your bot. Browse to the "Basic information" page listed in the sidebar. Scroll down to the section titled "Display information" to set an icon. Feel free to use one of the icons in this repository.
  4. Install your app to your workspace. At the top of the "Basic information" page you can find a section titled "Install your app to your workspace". Click on it, then use the button to complete the installation (see screenshot).

Using the action

From Slack-Action

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-.

Posting messages

From Slack-Action

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.

Channel IDs

From Slack-Action

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.

Formatting messages

From Slack-Action

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.

Inputs

This action requires two parameters:

token (required)

See above, should be provided by a secure method, like Github secrets.

request (required)

The request body as stringified JSON, see Slack's documentation.

verbose (optional)

Pass anything to verbose to make the action "chatty" while it sends your message. Great for debugging.

Outputs

This action exposes Slack's response in several fields.

ts

If successful, the message's timestamp is exposed in the ts output variable. It can be used by future calls to reply.

message

If successful, the message details are returned as JSON in the message output variable.

error

If not successful, the slack error message is exposed in the error output variable.

License

From Slack-Action, MIT license also further granted.

The Dockerfile and associated scripts and documentation in this project are released under the MIT License.