Use Slack as notification service in your workflow

This article intend to give some pointers to developers new to Slack on how to integrate their services / workflow with it. You can add a message to Slack and get notified as simple as a command like this:

My Use case

So my main purpose of using Slack it to get notified with certain events happen and implement it with minimum effort. For example, I often need to build web/apps before deploy, sometimes it could take a few minutes, I could just sit here waiting for the build to finish, or switch to do other tasks. It would be nice to have a notification on the Desktop, or even better, on my phone that the build is finished. I chose Slack because the mobile app push notification is very nice.

Potential use case:

  1. Get notified when a long-running command/build is finished
  2. Get notified when a cronjob failed (daily backup tasks, data sync task)
  3. Get notified when exception occur (recommend use Sentry instead, but Slack could be a simple solution)
  4. Get notified when a new version of a tool/framework is released
  5. Get notified when teammates push/tagged on Gitlab (Gitlab already has its webhook integration)

You don’t need to create an App

When I first started, one of the first thing I look for is the Web API of Slack, but the doc leads me to create an App to begin with. In fact it is not necessary, there is already a mechanism in Slack for simple integration, which is WebHook, you can think of it as an official App that handle simple integration.

How WebHook actually works

So basically on Slack, WebHook is like an App, which you can create a WebHook configuration, then Slack will give you a unique URL that looks something like this:

You can then POST a simple API call, no API authentication required!

Example CURL command:

Then you will see a new feed in the #general.

Getting started

1. You can go to the WebHooks page in the Custom Integrations page:

2. Create a configuration

3. Select which channel this wekbook will post to by default

4. Config the WebHook, you will get the Webhook URL here, copy it for later use

Now you can just post to this Webhook URL in your language of choice, I chose BASH script because it is the most flexible, I can integrate with any existing tools.

Additional Tips

In the following steps you can create a simple script for posting to slack with a command like this:

Create a slack.sh (gist here), this is a parameterized example

make it globally available in the system:

Install the Slack app on your phone, and try out the command

Have fun! For more advanced message format and integration, please read the Slack Docs.

Share your thoughts