Knowledge for the World

Post a message to Slack using your Raspberry Pi and an Amazon Dash Button

Coffee's done! ☕

This guide will walk you through the process setting up your Raspberry Pi and Amazon Dash Button to send notifications to a Slack channel. Notify coworkers when a new shipment of Red Bull is on its way or when to take cover from an imminent Nerf invasion by the accounting department.

I'll be using a Raspberry Pi 3 Model B running Raspbian Stretch Lite and a basic $5 Amazon Dash Button.

1

We will be using a Node.js module to listen for dash button presses and to send HTTP requests, so naturally the first step is to install Node.js! You can do so with the following commands:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install nodejs

You can check to make sure everything was successful by running node -v.

When writing this guide, I ran into issues using the latest version of NPM installed by the above commands. To avoid encountering the same issues as me, I would recommend manually downgrading to NPM v4 using the following command:

sudo npm install -g npm@4

You can check to make sure installing NPM v4 was successful by running npm -v.

I'll be using Node.js v8.9.0 and NPM v4.6.1 to write this guide.

2

One of the Node.js modules we will be making use of is node-dash-button. It uses a library called libpcap to monitor network traffic for packets sent by a dash button. This isn't installed by default on Raspbian, so we will need to install it ourselves using the following command:

sudo apt-get install libpcap-dev -y

NPM requires a Git client to download some of the modules used by node-dash-button. You can install Git using the following command:

sudo apt-get install git -y
3

Next, we will download and install the Node.js script that will listen for dash button presses and send HTTP requests to Slack. You can do this using the following commands:

wget https://github.com/jesmith0/dashgong/archive/master.zip
unzip master.zip
cd dashgong-master
sudo npm install

These commands will download the Dashgong script from a forked version of the original repository. I forked the repository and made some minor tweaks specifically for this guide in an effort to simplify the installation process. You can find the original repository here.

4

Setup your Amazon Dash Button

To set up your Amazon dash button for sending messages to Slack, we'll need to go through most of the standard setup procedure.

First, install the official Amazon app for iOS or Android.

From the menu in the top left corner, select My Account. Next, select Set up a new device under the "Dash Buttons & Devices" section and choose Dash Button.

Continue with the setup until you get to the "Select a product" step. From here, force close the app without selecting a product. Don't click the X, just close the app.

5

Find your Amazon Dash Button's hardware address

From the "dashgong-master" directory, run the following command:

sudo node node_modules/node-dash-button/bin/findbutton

Press the dash button and wait for a response that includes the dash button's MAC address. The message may contain the text "Manufacturer: Amazon Technologies Inc.", but it may not depending on where your specific dash button was manufactured. In any case, it will have the form "FF:FF:FF:FF:FF:FF". The first six hexadecimal characters correspond to a specific Amazon vendor and the last six hexadecimal characters correspond to your specific dash button.

To make things a bit easier, you can reference this list of Amazon vendor MAC prefixes.

Write down the MAC address so it can be added to the Dashgong configuration file in a later step.

6

Obtain Slack webhook URL

Next you will need to set up an incoming webhook integration in your Slack workspace. To do that, follow this link: https://my.slack.com/services/new/incoming-webhook/

Select the Slack workspace you'd like to work with from the dropdown menu in the top right corner. Then, select the channel you'd like to set up an incoming webhook for and click the button labeled "Add incoming WebHooks integration".

You will be redirected to a page that contains a field called "Webhook URL" that looks something like:

https://hooks.slack.com/services/NUMB3R5/L3773RS/m0r3numb3r54ndl3773r5

Copy this URL to your clipboard so that it can be pasted into the Dashgong configuration file in the next step.

7

Create a new JSON file called config.json with the following content:

{
    "settings": {
        "logging": {
            "url": "http://dot.com",
            "token": "look-a-me-im-a-token",
            "client-id": "CLIENTID",
            "client-secret": "CLIENTSECRET",
            "enabled": true
        }
    },
    "buttons": [{
        "name": "Send message to Slack",
        "id": "DASH_BUTTON_MAC",
        "action": "post",
        "post": {
            "url": "SLACK_WEBHOOK_URL",
            "headers": {"Content-type": "application/json"},
            "json": {"text": "Greetings from Howchoo!"}
        }
    }]
}

Replace the text DASH_BUTTON_MAC with the dash button's MAC address we obtained in step 5.

Replace the text SLACK_WEBHOOK_URL with the Slack webhook URL we obtain in step 6.

Don't worry about any of the logging settings. We are going to set this up perfectly first try, so we won't need any logging.

8

This next step is only required if you want your Raspberry Pi to run the script on startup. That way, you avoid having to manually run the script any time you reboot. To achieve this, we will be using a program called Supervisor, which provides a platform for simple process management.

To install Supervisor, run the following command:

sudo apt-get install supervisor -y

Next, create a configuration file for dashgong:

sudo nano /etc/supervisor/conf.d/dashgong.conf

Finally, add the following content to the file:

[program:dashgong]
command=/usr/bin/node /home/pi/dashgong-master/index.js
directory=/home/pi/dashgong-master/
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/supervisor/dashgong.err.log
stdout_logfile=/var/log/supervisor/dashgong.out.log
user=root

Save and exit.

You may need to explicitly set up Supervisor to start on system boot. You can do so with the following command:

sudo systemctl enable supervisor
9

Now everything should be set up and ready to go! Just reboot the Raspberry Pi by running sudo reboot, press the dash button, and wait for the notification to appear in your Slack channel!

If you chose to skip the step to automatically start the Dashgong script on boot, you can manually start it by running sudo node index.js from the "dashgong-master" directory.

If you encounter any problems, leave a comment below or log an issue on my fork of the Dashgong repository and I'll be happy to help you troubleshoot!

10

You may have noticed that the dash button will not send multiple messages back to back. This is because the Dashgong script has a hardcoded 60000 millisecond timeout. Don't worry, you can easily change this by modifying index.js.

Open the file and search for the following line:

setTimeout(function(){presses[button.id]=false},60000)

Change the value 60000 to whatever you like. Remember, this value is in milliseconds.