Send Slack messages from anywhere using Webhook Integration

I started working at a new company called Mercury FX International. We specialize in foreign exchange and I help maintain and innovate the software that we use. This can range from menial tasks such as fixing minor bugs to major projects like...

I started working at a new company called Mercury FX International.  We specialize in foreign exchange and I help maintain and innovate the software that we use.  This can range from menial tasks such as fixing minor bugs to major projects like integrations into financial systems and working with new cryptocurrencies.

While working on a tool that fed information into our system, I needed a way to notify our team if the process ever failed.  At first we sent an email out but I stumbled upon the Slack API and was hooked.  I ended up configuring a Slack bot which would send messages to a dedicated channel if the process ever failed.  That way it would not interfere with other communication and we would easily be able to see new notifications because it had its own channel.

This integration was so easy I wanted to write a short article on how to achieve it.  I will show you how to setup the Slack bot and then demonstrate using the API in Postman to send a simple message.

Setting up a Slack bot

  1. Navigate to https://api.slack.com/apps. You will need to be authenticated.
  2. Click on Create New App on the following screen:
    slack_apps
  3. Input a name for your new bot and select the workspace that the bot should send messages in (note that this cannot be changed. If you're worried about spamming your colleagues with test messages fret not - you can send test messages to yourself).
  4. Click Create App when you're done.
  5. Open the newly created bot and select Incoming Webhooks on the left menu.
    bot_info
  6. Turn Incoming Webhooks to On
    activate_webhooks
  7. Scroll to the bottom of the page and click on Add New Webhook to Workspace
    add_webhook
  8. Here you will add the Slack bot to a particular channel in the workspace (for testing purposes I added the bot to my own channel so as to not disrupt anyone else).
  9. Click Allow to add the bot to the channel.

Using our new Slack bot

Now that we have created a Slack bot and granted them access to a particular channel in our Slack workspace, we can use a very simple REST API to send messages from the bot.

In this example, I will simple use Postman to make the requests.  These REST requests can be made in almost any programming language though.

  1. Open Postman and create a new POST request using the specified Webhook URL
    postman_request
  2. Send the following raw body text as type application/json
    request_body
  3. Click Send and you should receive an HTTP code 200 in Postman. If you open Slack and you should see your new message:
    new_message

That's all there is to it.  Slack has made their API so simple that it can be integrated into almost any existing system.  This is a great way to modernize software that you might be maintaining where notifications are required.  Just don't get too excited and spam your users with status updates.