Automatically setting Slack status at Work & Home

When working from home it's helpful to tell people by use of our Slack Status, but it's always a pain to update it back and forth as I move about.

Here I'll show how to use the act of plugging my laptop into a USB Dock as a trigger to call the Slack API to set my status.

Step 1: choose an event engine

In almost any system like this you'll need an event engine. I looked at Microsoft Flow and IFTT, but neither could call Set Status on Slack as an option at a glance. Instead I settled on node-red.

node-red
Low-code programming for event-driven applications

Step 2: Install Node-RED

npm install -g node-red
node-red

This should install node-red and then start it. It'll show some console output then should be accessible on 127.0.0.1:1880. Play around with it, then exit the engine by typing Ctrl-C in the console.

Step 2: Install Node-RED as a service

We're going to want to run node-red all the time as a background service, for this can we use pm2.

pm2
Production process manager for Node.JS applications with a built-in load balancer.

pm2 is an service that'll run any node program in the background of your computer, required otherwise you'd have to keep a Console window open the whole time.

npm install -g pm2
npm install pm2-windows-startup -g
pm2-startup install

pm2 start (Join-Path -Path $env:USERPROFILE "AppData\Roaming\npm\node_modules\node-red\red.js")

pm2 save
Install PM2

Step 3: Detect USB's being plugged in.

There seems to be a node-red plugin called node-red-contrib-usb , the only issue is that it doesn't seem to support hot-loading.

I found a npm package called usb-detection which will raise events when a USB is plugged in or detached. I followed the guides here, and created a Node-RED package you can find here.

node-red-contrib-usb-watch
[![Build Status](https://dev.azure.com/moritonal/node-red-contrib-usb-watch/_apis/build/status/Windows%20-%20Release?branchName=master)](https://dev.azure.com/moritonal/node-red-contrib-usb-watch/_build/latest?definitionId=2&branchName=master…

You can install the module by going into node-red's Manage Palate mode found here:

Menu shown via the button in the top-right

Then searching for usb-watch and installing it:

The badge seems to be causing issues at the time..

It might take a hot-second to install depending on whether it finds a binary for your OS or has to build it itself (due to USB being quite low-level relatively).

Step 4: Putting together the flow

With the package now in place, we can put together our flow. We start with a simple check that our usb watch node works correctly:

Single node detecting USB events! (bonus points if you can tell me what USB this is...)

Then we add a filter for only the USB device we're interested in:

Filter it to only my USB dock's ID

Confirm that it works:

Now we need to setup the JSON payload that we'll send to Slack using a template node:

Use of a template node to create a payload that we'll send to Slack.

Then add the http request node for calling Slack's API. We'll need a bearer authentication token to authenticate ourselves with Slack, which can be generated here.

http request node with URL, Token and Method set.

Finally we Deploy and plug in our USB Dock and watch the magic:

Voila! We have a set our Status! All we have to do it again with different filter ID's and payloads at work and we'll have a working system.