How to Configure Real-time Alerting with IoT Hub and Slack

Real-time Messaging and Alerting

Nowadays, most companies use real time messaging systems to have their teams communicate together from any location. Slack is one of the most popular such systems.

An interesting use case for Slack is alerting. You can set up “Slack applications” to send messages to team members and notify them about ongoing problems. As team members tend to keep a constant eye on their Slack workspace, they can be very quickly informed of ongoing problems.

In this article, we are going to show you how to enable device-to-Slack messaging through IoT Hub.

There are three steps to achieve this:

  • Create a Slack workspace and a Slack application
  • Create an IoT Hub and add a Device
  • Set up an IoT Hub Route to the Slack application
  • Publish messages on the IoT Hub and see the messages on the Slack channel

Creating a Slack workspace and application

1 . Create a Slack workspace to be our sandbox. To do so, follow the instructions available on the Slack website.

2 . To create the application, go to this page, type in any name, and select your Slack workspace.

3 . Once created, enable the application to send messages to your workspace’s channels. Head to the OAuth & Permissions page, under Scopes > Bot Token Scopes, add an OAuth scope named chat:write. It should look like the following.

4 . Finally, install the application in your workspace: scroll up the page and click on the install button.

5 . Once authorized, the token used to publish messages from IoT Hub should appear. Write down the token, we will need it for next step.

6 . Add your application to a Slack channel and find the channel ID. To get this ID get the link for any message in the channel (it should look something like https://iot-sandbox.slack.com/archives/C01FLQXBJF2/p1605613307000200) and copy the code displayed right after archives (here C01FLQXBJF2). This is your Slack channel ID, which will be used in a later step.

Creating an IoT Hub

Now that we have the Slack application ready, we need to create an IoT Hub. This will be where our simulated IoT Device will publish messages to be sent to our Slack workspace.

Please follow this procedure to create an IoT Hub. Select your name and plan of choice, and click the Create Hub and Add a Device button.

Setting up an IoT Route to our Slack application

Here is where the magic happens: We will instruct our IoT Hub to call our Slack application when a message is published on the alert/high topic. To do so we will use an IoT Hub REST Route.

1 . In the Scaleway console, head to the Routes tab of your IoT Hub.

2 . Create a new route with the settings as follows:

3 . In the headers, replace the authorization token with the one you got for your Slack application.

Sending messages to your Slack workspace

Now that we have everything set up, let’s add a Device and send a message to trigger a Slack alert message.

1 . Head back to the Devices tab of your IoT Hub. Add a new Device, name it as you wish, choose Allow insecure connection and click the Add device button. On the Device overview page, click the MQTT Webclient button. The client will open and automatically connect.

Note: We chose to allow insecure connection to simplify this guide, in a production environment you will want to deny insecure connections and use mutual TLS authentication.

2 . Now let’s publish a message to the alert/high topic, in the Publish block:

  • Write alert/high as the topic
  • Write { "channel": "<Slack channel ID>", "text": "Hi from IoT Hub!" } as the message (replace <Slack channel ID> with the one you collected earlier)
  • Click Publish (no confirmation will be issued)

3 . As we have published a message, we should see it appear in the Slack workspace.

Congratulations, you sent your first Slack message from a (simulated) Device.

Going further: using Serverless functions to do it the smart way

Real world use cases aren’t that simple, of course. Here is how we could improve it:

  • store all alerts in a database to keep a history.
  • send the alerts to an on-premises real-time dashboard.
  • for important alerts, depending on the alert’s contents, notify the right Slack channel using a serverless function to make this choice.

Your architecture could look like this:

We're done!

We hope you had fun connecting your IoT Hub to your Slack workspace. This is one of the many possibilities the IoT Hub REST Route provides. Stay tuned for more REST Route usage examples.


Learn more about Scaleway IoT Hub, discover how to add Devices to the hub, check the IoT Hub metrics or get started in a few clicks with the IoT Hub Kickstarts.

Discover the Cloud That Makes Sense