Meet Bowie our studio DJ
Slack is a messaging app for teams, allowing people to communicate and send important information like files, links, funny gifs and videos of cats. It also comes with a variety of apps you can add to your team to help increase productivity (and procrastination).
One particular category of extensions that caught our attention was bots, smart little programs that respond and take action based on messages you write in slack. There are lots of bots available but we’re developers, so lets hack our own.
Hubot provides you with the basic setup for a chatbot and allows you to customise it with your own cutom scripts. Following some basic tutorials, getting setup with Hubot on Heroku and working with your Slack team is pretty quick and easy. Then with a name change and avatar you have your own personalised chatbot.
Writing custom scripts for Bowie
Writing custom scripts for Bowie is just as easy. All it takes is a single file and a few lines of coffescript to setup a basic command. Just to test the water I created a simple command that would make Bowie randomly bark when the word ‘bark’ is written in a message. Here’s what the code looks like.
Running a quick test in Slack and as you can see Bowie is alive and barking!
As amusing as this script is, its not very useful. So lets take it further by allowing our chatbot to control the music in the studio.
Creating the DJ script
The idea is that we ask Bowie to play us a song using Slack, so by writing
@bowie dj play lionel ritchie Bowie will search Spotify for
lionel ritchie and play a song.
To get this working I created a Node app called Djay. This app will accept requests through its API, search for a track via the Spotify Web API and then play the song through the Spotify App through its Applescript API.
It might seem complicated but it can be broken down into a few simple steps:
- A Bowie script inteperets a Slack messages asking to play a song, something like:
@bowie dj play lionel ritchie
- Bowie then sends an API request to our Node app called Djay with the query (
- Djay receives the API requests and uses the query to search the Spotify Web API for a track
- When a track is found, Djay takes the track information and tells the Spotify App to play that track via the Spotify Applescript API
- Djay will then send a message back to Bowie with the information about the new track being played
- Bowie then uses that information to create a Slack message to inform the team of the new track
To build the Djay app I used the following modules:
- Express – Framework to build the API endpoints
- Ngrok – For opening up the Djay API to the world and Bowie to send requests to
- Spotify Applescript – For controlling the Spotify App in Node
Once the app was built I then moved onto creating the Bowie script to handle our dj requests focusing on the
@bowie dj play ... command. Once finished we could now control a designated Spotify app through Slack using Bowie, heres the result.
With all the difficult work out the way I took things further by adding a few more commands. Heres the entire list of dj commands available with Bowie.
dj play <search>– Play a track via the
dj any <search>– Play a random track via
dj now– See whats currently playing
dj volume <volume>– Set the volume
dj next– Play the next track
dj previous– Play the previous track
dj pause– Pause the current track
dj resume– Resume the current track
Now we have our own personal chatbot at work, who barks and controls the music in the studio. Needless to say, this set up is a huge success with the team and has us all dancing on the ceiling!