So this week I ordered some of those fancy Philips Hue lights, It turns out the Hue Bridge has a REST interface you can use to change the colour of the lights (and some other things). I have already been messing with the fantastic BotKit project for a couple of Slack bots for work and one thing I really love is a good distraction (even now I am meant to be writing a blog post on using the PaperCut Health Monitoring API with Slack).

First up make an authorisation user on the Hue Bridge to do this we can just use curl from the command line, First send the command below then press the button on the front of the bridge and send it again this tells the bridge you are a legit source and will allow you to create the user.

curl -H "Accept: application/json" -X POST --data '{"devicetype":"slack","username":"slackbot"}' http://[bridge ip address]/api

The response will give you the username we will be using so make a note of it for later, Now we need to head to the Slack integrations page and register a new bot. Fill out all the information and make a copy of the token it gives you. Now we have everything we need we can start on the fun bit.

To get this up and running we will need to use NPM to install BotKit and Unirest the 2 commands below should take care of that for you.

npm install botkit
npm install unirest

This could also be done by setting up a package file and adding in the dependencies for them but for this quick example we are not going to bother with that.

Lets start off by creating a few vars in our project for BotKit, Unirest, the Slack bot token, The IP of the Hue Bridge and the user token obtained earlier.

1var Botkit = require('botkit');
2var unirest = require('unirest');
4var slackToken = 'SLACK_TOKEN';
5var hueBridge = 'HUE_BRIDGE_IP';
6var hueUser = 'HUE_USER_TOKEN';

Next up we will setup BotKit and start the RTM process to connect us to our Slack instance.

 8var controller = Botkit.slackbot({
 9 debug: false
13 token: slackToken
14}).startRTM(function(err) {
15 if (err) {
16  throw new Error(err);
17 }

Nice and easy, If you was to run this now you would notice the Slack Bot user will be showing up as online but it is not currently able to do anything. I wanted to make the Hue change colour anytime someone mentioned some keywords anyone that knows me could probably guess what those words are but for this post we will keep it a bit cleaner and just use different variations of my name.

20contoller.hears(['jon', 'jonathan', 'john'], ['ambient'], function(bot, message) {
21  unirest.put('http://' + hueBridge + '/api/' + hueUser + '/lights/1/state').send('{"hue":65280}').end();

Great so if we we run the bot now and anyone mentions any of the words in the list our first Hue light will change to red all we need to do now is throw in a quick function to change it back to our default colour which will be green.

23function backToGreen(){
24  unirest.put('http://' + hueBridge + '/api/' + hueUser + '/lights/1/state').send('{"hue":25500}').end();

Now just under where we set the light to red we can add a 2 second wait so it changes back just by adding:

setTimeout(backToGreen, 2000);

And that is it we now have a Slackbot up and running that will change the colour of one of your Hue lights if any of your keywords are mentioned. For the complete chunk of code you can find a Gist here.