Hometutorials
configure realtime alerting slack

Jump toUpdate content

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

Reviewed on 28 February 2022 • Published on 17 November 2020
  • Scaleway
  • IoT-Hub
  • Slack
  • Real-time-alerting

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

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

Replace the authorization token with the one you got for your Slack application in the headers.

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. Go to the Devices tab of your IoT Hub.

  2. Add a new device and make sure to select the Allow insecure connections option.

    Note:

    We selected the Allow insecure connections to simplify this example. However, in a production environment we recommend you select the Deny Insecure Connections option and use mutual TLS authentication.

    Important:

    After you click Add a device to your Hub, you are taken to the credentials section. In this section, you can add a Certificate Authority for your device. This step is not mandatory.

  3. Click Back to devices. Then, click the name of your device. The device information page displays.

  4. Click MQTT Webclient on the device overview page. The client will open and automatically connect.

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

The message appears 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

When you use this tutorial in a production setting, we recommend that you:

  • 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 content of the alert, notify the right Slack channel using a Serverless Function to make this choice.

You can set up your achitecture in the following manner:


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.