Platform
Docs
Solutions
ContactLog In
Courier Live Header
COURIER

Controlling IoT using the Webhook Provider

Aydrian Howard

September 09, 2020

Riley Napier from our engineering team returned for our September 2nd Courier Live. Together we used the Courier Webhook Provider to send a notification using an unconventional channel, a BlinkStick Square.

Check out the video below to watch us:

  • Demo an application using a BlinkStick Square
  • Configure and add the Webhook Provider to an existing notification
  • Create an endpoint to accept and handle the Webhook Post from Courier
  • Send a test notification using the new Notification Designer Send tab

Be sure to Like the video and Subscribe to our YouTube channel.

Configuring the Webhook Provider

Courier allows for static and dynamic configuration of the Webhook Provider. By default, Courier sends a payload via POST or GET request to a specified url. For this project, we opted to use the dynamic configuration. This allowed us to specify destination configuration on a per recipient basis by supplying it in the webhook object in the recipient profile.

1
// Profile
2
{
3
  "webhook": {
4
    "url": "https://blinkstick-server.herokuapp.com/api/courier",
5
    "Method": "POST"  // optional: defaults to POST
6
  }
7
}

You can learn more about Dynamic Destination configuration in the Courier Docs.

Accepting the POST

The url supplied to the destination configuration needs to accept a POST and return a status code of 200. If Courier doesn't receive a 200 response, it will attempt to retry the POST. For our project, we used a Next.js application deployed to Heroku. To create our route, we added a courier.js file to the /pages/api directory.

1
// /pages/api/courier.js
2
export default (req, res) => {
3
  if (req.method === "POST") {
4
    req.context.io.emit("alert", req.body);
5
    res.status(200).send("ok");
6
  } else {
7
    res.satus(405).send();
8
  }
9
};

This results in a route that will accept a request and emit an alert event using Socket.io if the request method is a post. You can find the full blinkstick-server code on GitHub.

Handling the alert

Now that the webhook has been accepted and an event has been emitted, the client needs to handle it. Our client is a simple Node.js application running locally on the machine connected to the BlinkStink Square. We use the Socket.io Client to connect to the server running in Heroku. We can handle the alert event by adding the following listener.

1
const io = require("socket.io-client");
2
const blinkstick = require("blinkstick");
3
const player = require("play-sound")();
4
5
const device = blinkstick.findFirst();
6
const socket = io("https://blinkstick-server.herokuapp.com/");
7
8
socket.on("alert", (data) => {
9
  console.log(data);
10
  device.morph("red", function () {
11
    device.morph("orange", function () {
12
      device.morph("yellow", function () {
13
        device.morph("green", function () {
14
          device.morph("blue", function () {
15
            device.morph("purple", function () {
16
              device.morph("#000000");
17
            });
18
          });
19
        });
20
      });
21
    });
22
  });
23
  player.play("./media/youve-got-mail.mp3", (err) => {
24
    if (err) console.log(`Could not play sound: ${err}`);
25
  });
26
});

The listener uses the BlinkStick Node.js module to make the Square transition through all the colors of the rainbow. And then for added fun, we used the play-sound package to play a sound through the connect computer's speakers. You can find the full blinkstick-client code on GitHub.

Webhook IoT Success GIF

Now this is just a simple exercise of how Courier can trigger an IoT device. There's so much more that can be done. You could use the data being passed in the webhook payload. You could have this running on a Raspberry Pi. The only limit is your imagination (and maybe your pocketbook). Definitely sign up for a free Courier Account and let us know what you create.

Is there something you’d like to see us do using Courier? Let us know and it might be the subject of our next Courier Live. We stream a new Courier Live every Wednesday at noon Pacific. Follow us on Twitch to be notified when we go live.

-Aydrian

More from Courier

data-thumbnail
COURIER

Tools and Techniques to Establish Your Data Team Early

How tools like Segment, Metabase, Snowflake, Census, and others, can help establish a data team from the very early stages in a startup.

Raymond See

Raymond See

February 16, 2023

QOTD Thumbnail
ENGINEERINGCOURIER

Develop a Motivational QOTD with Courier and GPT2

Courier and OpenGPT2 in action: build a service that sends friends and family an AI generated motivational quote of the day.

Prakhar Srivastav

Prakhar Srivastav

February 09, 2023

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.