Tutorial: Implementing Your Own James Brown Bot [Part 2]

Now that you’ve read a high level overview of James Brown (see the first part in our series, how to set up a DJ chatbot for your office), and what’s required to get him running, it’s time to turn on some smooth jazz and get coding. Fortunately for us, James Brown is surprisingly simple to set up, as most of the heavy lifting has been done for us.

Let’s start with some of the first requirements to get the code up and running locally.

James Brown is built on the well known bot framework from our friends at Howdy.ai called Botkit. Botkit is built on Node.js, so you’ll need to install Node if you haven’t already. We’re also going to assume that you have git installed in order to clone the James Brown repository, otherwise you’ll just want to download the zip file from the repository found here.

Once you’ve got the source code locally on your computer (under the jamesbrown folder), you’ll want to jump into your command line, change your directory to the jamesbrown folder, and grab all of the node packages that James Brown relies on.

npm install

After we’ve got the latest node packages, we need to set up a local configuration file that will host all of your Slack and Spotify tokens. In the root folder, create a file called bot-setup.js.

Here, you can copy the following code, in which we will be modifying throughout the setup:

module.exports = {
  server: {
    port: 3000,
    subdomain: null,
    ngrokToken: '7Wmb6...' // NGROK AUTH TOKEN
  },
  slack: {
    clientId: '22312...',  // SLACK APP CLIENT ID
    clientSecret: 'c5181...',  // SLACK APP CLIENT SECRET
    channel: 'studio-54',  // SLACK REPORTING CHANNEL NAME
  },
  spotify: {
    userName: 'fizzbuzz',  // SPOTIFY USERNAME
    playlistId: '14KDK...',  // DESIGNATED SPOTIFY PLAYLIST ID
    clientId: '8047e...',  // SPOTIFY APP CLIENT ID
    clientSecret: '1683f...',  // SPOTIFY APP CLIENT SECRET
    redirectUri: 'http://dev.tylershambora.com/spotify-callback'
  }
};

Let’s walk through each of the different options and how to modify them for your configuration file.

We first need to visit ngrok.io and sign up for their free service, in order to get a token. As mentioned in the previous post, ngrok allows our local machine to be visible to incoming webhooks, which will eventually control things like moving to the next song or adjusting the volume.

Once you create your account, visit the get started dashboard to view your token.

Copy the token from the dashboard and pasted it in the ‘ngrokToken’ string in the configuration file.

Next, we’ll set up the Slack App, which will create our James Brown bot user, and give us our Slack API access. You can create your slack app by visiting the Slack API apps page. At the top of the page, click “Create New App.” You’ll create a name for your app, and we somehow settled on James Brown.

Once you create it, you’ll be taken directly to your app screen, which will display the Client ID and Secret. Copy and paste these values into your configuration value.

The next slack related config to complete is which channel you want James Brown to talk in. This is just simply the name of the channel. We created our own channel specifically for James Brown, to contain all music related information/requests within that channel.

The last part of the configuration we need to modify is the Spotify section.

We’ll need to sign up for a Spotify developer account, and set up a new application. Click “Create an App” and give it a simple name—maybe James Brown?  Once created, you’ll see a similar screen to your Slack App.  This is where you’ll get your client ID and secret, to be pasted into your config file.

Another important thing to note here is we need to add a redirect URI for authenticating our app. We’re going to use the original James Brown developer’s spotify call back page for simplicity. Simply paste the URL (which is also in the example config file above) into the input box and hit add, then don’t forget to click “save” at the bottom.

The last configuration variables to set are the user name and playlist id. Username should be pretty simple, unless you were using a facebook account for your spotify account. If that’s the case, here’s a quick guide on how to retrieve your ID. Hopefully you’ve already setup a playlist for James Brown to run on, and if you have, finding its ID is relatively simple as well. If you right click the playlist (in the app or web client), you should be able to “Copy Playlist Link.” If you paste that link somewhere, you’ll see the link in the URL like so.

https://open.spotify.com/user/username/playlist/7e6wkEuzdOEbHe0x4

Copy and paste everything after the last slash into your configuration file.

Now that we’ve got the configuration setup, it’s time to get James Brown running! Jump into your command line, change your directory to your jamesbrown folder, and run:

node jamesbrown.js

Time to get groovy!

Check out the next series in the blog, on how to get James Brown working through Message.io. This would be great if you want to utilize the source code for James Brown (written for Slack) but your company uses Microsoft Teams, Hipchat, or any other platform we support.

1

About Trevor Thompson

Software Developer for Message.io