Receiving Windows Notifications with Ngrok and Webhook

When working at the office, if you don’t want to miss any notification, you can use some tools such as IFTTT and Slack, to subscribe notification service based on Webhook. What if you’re going to receive Windows notifications without installing these tools? Using Node.js and Ngrok, I can quickly create a local notification service in 5 minutes.

Sending Windows Notification in Node.js

I found an open source project node-notifier which helps developers send a native notification with a few lines of JavaScript code.

Install node-notifer:

npm install node-notifier

Show a notification:

const notifier = require('node-notifier');
// String
notifier.notify('Message');

// Object
notifier.notify({
  title: 'My notification',
  message: 'Hello, there!'
});

Building a Web Server with Express

Express is a web framework for Node.js. We can quickly establish a web server as follows:

var express = require('express');
var app = express();

app.use(express.static(__dirname));

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(2018, () => console.log('Example app listening on port 2018!'));

To process an HTTP request, add the code snippet:

app.post('/postreceive', (req, res) => {
  // TODO:
  res.send();
});

Ngrok: Exposing a Local Web Server to the Internet

With Ngrok, you don’t need to purchase a domain for deploying and testing your website. Ngrok can instantly generate a public address and forward HTTP requests to your local machine.

Create a public HTTPS URL.

ngrok http 2018

ngrok

Instead of localhost:2018, I can now visit my website via https://391b1eea.ngrok.io.

Testing Webhook with GitHub

Let’s make a simple test with Webhooks for GitHub. Select a GitHub repository, and then navigate to Settings > Webhooks.

github webhook

Create a payload URL:

app.post('/postreceive', (req, res) => {
  let event = req.header('x-github-event');
  console.log(event + ' event');
    // Object
  notifier.notify({
    title: 'GitHub',
    message: event + ' event'
  });

  res.send('Received the notification')
});

The above code can extract the GitHub event from the HTTP request header and then send it as a Windows notification. You can visit GitHub Webhooks for more detailed information.

Here is a simple test. After starring my repository, I received a notification on Windows.

Windows notification

Source Code

https://github.com/yushulx/webhook-notification