How to deploy Hubot in Slack to an Azure Website

If you are on a Slack team, do you want this sort of awesomeness?

Bender is the name of our Hubot. On top of being able to mustachify me, it can do a plethora of nifty things like pulling Travis states, integrating with GitHub, retrieving the latest stock prices, etc. This blog post will go into detail on the steps needed to deploy Hubot onto an Azure Website and integrating into your Slack team. Credit goes to Qijin Zhou for figuring out most of this stuff.

If you want to skip to the end and deploy a hubot ready to be deployed on Azure and integrated with Slack: https://github.com/jpoon/hubot-bender or Qijin's version (https://github.com/qijinzhou/Hubot).

Generate Hubot

Install and run the hubot generator to generate your own chatbot:

% npm install -g yo generator-hubot
% mkdir my-hubot
% cd my-hubot
% yo hubot

The generated directory is prepped and ready to be made into a git repository, it already includes things like a .gitignore.

% git init
% git add .
% git commit -m "Initial commit"

The hubot generated defaults to using Heroku for deployment and Redis for persistence. Since we are going to be deploying this to Azure instead, let's clean our bot of all things Heroku and Redis.

% npm uninstall hubot-heroku-keepalive --save
% npm uninstall hubot-redis-brain --save
% rm Procfile

and in external-scripts.json, remove hubot-heroku-keepalive and hubot-redis-brain scripts.

Configuration

Persistence (Brains)

Hubot's brain can be stored on a variety of data stores. As I'm going with an all Azure solution, we're going to use Azure Storage for Hubot's brains.

% npm install hubot-azure-scripts --save

Add it to your external-scripts.json

[
   ...
   'hubot-azure-scripts/brain/storage-blob-brain',
   ...
]

The storage-blob-brain requires the following environment variables to be defined:

  • HUBOT_BRAIN_AZURE_STORAGE_ACCOUNT- Azure storage account name
  • HUBOT_BRAIN_AZURE_STORAGE_ACCESS_KEY - Azure storage account access key

Create the Azure Storage account using the Azure Command-Line Interface (Azure CLI). You can install the CLI tool using NPM:

npm install azure-cli -g  

Create the storage account.

% azure storage account create <my-hubot-brain-storage> 
% azure storage account keys list <my-hubot-brain-storage>

Take note of the storage account name and key from the above commands, and set the appropriate environment variables.

set HUBOT_BRAIN_AZURE_STORAGE_ACCOUNT=<my-hubot-azure-storage-name>  
set HUBOT_BRAIN_AZURE_STORAGE_ACCESS_KEY=<my-hubot-azure-storage-access-key>  

Slack Adapter

Adapters interface to the service you want your Hubot to run on (e.g. Campfire, IRC, Slack). We will configure our Hubot to run with Slack.

% npm install hubot-slack --save

Configure Hubot to run with the Slack adapter using the environment variable.

% export HUBOT_ADAPTER=slack
% export HUBOT_SLACK_TOKEN=<slack-api-token>

You can obtain the by logging into your Slack and adding a new Hubot integration.

Running Locally

At this point, you should be able to run your Hubot locally from ./bin/hubot. To make things easier, I recommend updating your package.json to configure the start script such that you can go npm start to run Hubot.

{
  "name": "my-hubot",
  ...
  "scripts": {
    "start": "bash ./bin/hubot"
  },
  "dependencies": {
  ...
  }
}

Deploy

To the cloud! Let's deploy Hubot to an Azure Website.

Deployment Scripts

Generate the necessary deployment scripts using the Azure CLI:

azure site deploymentscript --node -t bash  

In the newly-generated deploy.sh, add step 4 as shown below to create the hubot.coffee.

...
# 3. Install npm packages
if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then  
  cd "$DEPLOYMENT_TARGET"
  eval $NPM_CMD install --production
  exitWithMessageOnError "npm failed"
  cd - > /dev/null
fi

# 4. Create Hubot file with coffee extension
cp "$DEPLOYMENT_TARGET/node_modules/hubot/bin/hubot" "$DEPLOYMENT_TARGET/node_modules/hubot/bin/hubot.coffee"  
...

Upon deployment, Azure will try to find a server.js or app.js to use as an entry point for your node application. Let's create a server.js where we run hubot.coffee.

require('coffee-script/register');  
module.exports = require('hubot/bin/hubot.coffee');  

Azure Website

Using Azure CLI, create an Azure website.

% azure site create <my-hubot> --git

The above command will create a git repository on the Azure website. Let's add it as a remote repository in our local repository.

% git remote add azure https://<user-name>@<my-hubot-bender>.azurewebsites.net/<my-hubot-bender>.git

If you have configured Azure Storage peristance, add the necessary application settings.

% azure site appsetting add HUBOT_BRAIN_AZURE_STORAGE_ACCOUNT=<azure-storage-name>
% azure site appsetting add HUBOT_BRAIN_AZURE_STORAGE_ACCESS_KEY=<azure-storage-key>

Same goes for the Slack adapter.

% azure site appsetting add HUBOT_SLACK_TOKEN=<slack-api-token>
% azure site appsetting add HUBOT_ADAPTER=slack

Ship it!

% git push azure master

At this point, your hubot should be up and running on Azure and integrated with Slack. To get you started, here are some hubot scripts that you can extend your bot with: