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 the 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:
1 . 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.
Now that we have the function 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.
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.
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:
alert/high
as the topic{ "channel": "<Slack channel ID>", "text": "Hi from IoT Hub!" }
as the message (replace <Slack channel ID>
with the one you collected earlier)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.
Real world use cases aren’t that simple, of course. Here is how we could improve it:
Your architecture could look like this:
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.