Quick Links

With the help of a few webhooks, you can turn Slack into a notification center for your command line. There's a lot you can do with this, from simple message feeds to custom Slack integrations for your app.

This kind of integration works particularly well for notification-based events, like when something updates, or new content goes live somewhere, similar to how RSS feeds operate. You can also make custom integrations for your own apps that work over the same API, even including interactive dialogs and menus to obtain user input.

Use a Slack Bot With Webhooks

Slack has an API that allows you to automate a lot of interactions with your workspace. You can access this API from anywhere that allows you to make HTTP requests like

        POST
    

 and

        GET
    

, which includes bash scripts using the curl utility, as well as most scripting languages. The curl utility is installed by default on nearly all Unix distros, and it's available for download on Windows.

Part of this API includes webhooks, a way for two applications to send each other messages. You'll be given a webhook URL which you can make a

        POST
    

 request to, with some data in the form of a JSON object. For example, you could send a quick message with:

{"text":"Hello, World!"}

Your bash script will send this bit of data off to Slack, which will read it and send "Hello, World!" to the channel you specify when you set up the webhook.

You'll need to use your own Slack App, so the message will come from a bot (which you can customize). This is actually a good thing; webhooks are primarily used for notifications, and since a message sent via a webhook won't be sent from your own account, you will get push notifications for messages sent this way.

You can also send automated messages from your own Slack account in a similar manner, except you'll need to use the chat.postMessage API with the as_user argument set to true. This will require an OAuth 2.0 token with the chat:write:user permission scope, which you will need to generate yourself prior to using the API. We suggest sticking with webhooks, as they are far easier to manage.

Set Up A Slack App to Accept Webhooks

Navigate to Slack's API portal and create a new Slack app. Give this app a username, select the workspace it will belong to, and hit "Create App."

You'll be brought to a homepage where you can add features to your bot. There's a lot of cool stuff here, like custom /commands and interactive messages, but all you need right now is "Incoming Webhooks." Click this button, and turn it on.

You'll need to register a new webhook for your application to send messages to. Click "Add New Webhook to Workspace" at the bottom of this screen.

You will be asked to authorize the application, and select the channel for the messages. These can be public channels or direct messages with other users; you both will see the bot in your DMs and receive notifications.

You can register multiple webhooks for different channels, but each hook will have a different URL.

Copy your webhook's URL, and open up your terminal. Paste in:

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' YOUR_WEBHOOK_URL

Replacing YOUR_WEBHOOK_URL with the one Slack gave you. Hit enter and you should receive a "Hello, World!" notification and see a new message in the channel you chose.

You can use this curl command from inside a bash script, and you can configure the JSON object to send whatever data you'd like, including from bash variables.

Using Formatted Messages and Attachments

The message API can do more than just send simple text. If you'd like your messages to be more stylized, you'll have to add some extra parameters to the JSON object that curl sends out.

You can use markdown and other simple formatting in the same way as you would from Slack's message box. One thing to note though is that for linking to channels and @ing users you will need to specify the channel ID and user ID rather than the plaintext names, or else it will not work.

For everything else, you'll want to use Slack's block kit builder, a WYSIWYG editor for Slack messages that allows you to make interactive and richly embedded content, and even handles creating the JSON for you. Elements like buttons and menus can be configured to send POST requests to a URL you specify, letting your users talk back to the application sending the message.

The block kit builder isn't entirely necessary though, since the old methods aren't being deprecated. If you'd just like to use features like Attachments, you can use the old Message Builder to build and preview the JSON.