/webmoni

A Slack Bot for demonstration purposes.

Primary LanguageCoffeeScript

Webmoni - SlackBot demo

Webmoni is a SlackBot based on the Hubot framework. It was created for demonstration purposes.

Slides/Presentation

You can find the corresponding slides in presentation/Slack.pdf.

Example Scripts

The shown example scripts that send messages to Slack are located in example-scripts.

To use the scripts, you have to set up an incoming webhook.

Once you have your webhook URL, you pass it via environment variable SLACK_ENDPOINT to the scripts.

Send a single message:

SLACK_ENDPOINT=https://your.slack.com/endpoint ./example-scripts/to-slack-command-line.sh

Pipe output of another command to Slack:

SLACK_ENDPOINT=https://your.slack.com/endpoint ./example-scripts/to-slack-pipe.sh

Bot

Follow the following steps to set up your own bot.

Install the generator, that helps you to set up the project infrastructure. This requires a working version of NPM:

npm install -g yo generator-hubot

Initialize the project in a new directory, e.g. webmoni:

mkdir webmoni && cd webmoni
yo hubot

Update the Node.js version in package.json, e.g. to 8.10.x. Otherwise the deployment to Heroku might fail if there is an NPM version mismatch between local setup and host environment.

Register your bot at https://my.slack.com/apps/A0F7YS25R-bots to get a Slack token for your bot.

Create a Heroku app to host your Slack bot. The simple server (so-called "dyno") that is required to host such a bot is free:

heroku create webmoni --region eu

Add your bot configuration:

heroku config:set HUBOT_SLACK_TOKEN=your-slack-token-from-previous-step
heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=$(heroku apps:info -s | grep web.url | cut -d= -f2)

Push your project to Heroku. That will trigger the build and the deployment:

git push heroku master

Your Bot should show up in Slack now. Invite it to channels by mentioning it, e.g. via

@webmoni Come here!

Notice: Your free dyno will sleep somewhen. You might want to trigger a wake up call for your Slack bot. Learn how to set it up at https://github.com/hubot-scripts/hubot-heroku-keepalive#waking-hubot-up.

webmoni

webmoni is a chat bot built on the Hubot framework. It was initially generated by generator-hubot, and configured to be deployed on Heroku to get you up and running as quick as possible.

Running webmoni Locally

You can test your hubot by running the following, however some plugins will not behave as expected unless the environment variables they rely upon have been set.

You can start webmoni locally by running:

% bin/hubot

You'll see some start up output and a prompt:

[Sat Feb 28 2015 12:38:27 GMT+0000 (GMT)] INFO Using default redis on localhost:6379
webmoni>

Then you can interact with webmoni by typing webmoni help.

webmoni> webmoni help
webmoni animate me <query> - The same thing as `image me`, except adds [snip]
webmoni help - Displays all of the help commands that webmoni knows about.
...

Configuration

A few scripts (including some installed by default) require environment variables to be set as a simple form of configuration.

Each script should have a commented header which contains a "Configuration" section that explains which values it requires to be placed in which variable. When you have lots of scripts installed this process can be quite labour intensive. The following shell command can be used as a stop gap until an easier way to do this has been implemented.

grep -o 'hubot-[a-z0-9_-]\+' external-scripts.json | \
  xargs -n1 -I {} sh -c 'sed -n "/^# Configuration/,/^#$/ s/^/{} /p" \
      $(find node_modules/{}/ -name "*.coffee")' | \
    awk -F '#' '{ printf "%-25s %s\n", $1, $2 }'

How to set environment variables will be specific to your operating system. Rather than recreate the various methods and best practices in achieving this, it's suggested that you search for a dedicated guide focused on your OS.

Scripting

An example script is included at scripts/example.coffee, so check it out to get started, along with the Scripting Guide.

For many common tasks, there's a good chance someone has already one to do just the thing.

external-scripts

There will inevitably be functionality that everyone will want. Instead of writing it yourself, you can use existing plugins.

Hubot is able to load plugins from third-party npm packages. This is the recommended way to add functionality to your hubot. You can get a list of available hubot plugins on npmjs.com or by using npm search:

% npm search hubot-scripts panda
NAME             DESCRIPTION                        AUTHOR DATE       VERSION KEYWORDS
hubot-pandapanda a hubot script for panda responses =missu 2014-11-30 0.9.2   hubot hubot-scripts panda
...

To use a package, check the package's documentation, but in general it is:

  1. Use npm install --save to add the package to package.json and install it
  2. Add the package name to external-scripts.json as a double quoted string

You can review external-scripts.json to see what is included by default.

Advanced Usage

It is also possible to define external-scripts.json as an object to explicitly specify which scripts from a package should be included. The example below, for example, will only activate two of the six available scripts inside the hubot-fun plugin, but all four of those in hubot-auto-deploy.

{
  "hubot-fun": [
    "crazy",
    "thanks"
  ],
  "hubot-auto-deploy": "*"
}

Be aware that not all plugins support this usage and will typically fallback to including all scripts.

hubot-scripts

Before hubot plugin packages were adopted, most plugins were held in the hubot-scripts package. Some of these plugins have yet to be migrated to their own packages. They can still be used but the setup is a bit different.

To enable scripts from the hubot-scripts package, add the script name with extension as a double quoted string to the hubot-scripts.json file in this repo.

Persistence

If you are going to use the hubot-redis-brain package (strongly suggested), you will need to add the Redis to Go addon on Heroku which requires a verified account or you can create an account at Redis to Go and manually set the REDISTOGO_URL variable.

% heroku config:add REDISTOGO_URL="..."

If you don't need any persistence feel free to remove the hubot-redis-brain from external-scripts.json and you don't need to worry about redis at all.

Adapters

Adapters are the interface to the service you want your hubot to run on, such as Campfire or IRC. There are a number of third party adapters that the community have contributed. Check Hubot Adapters for the available ones.

If you would like to run a non-Campfire or shell adapter you will need to add the adapter package as a dependency to the package.json file in the dependencies section.

Once you've added the dependency with npm install --save to install it you can then run hubot with the adapter.

% bin/hubot -a <adapter>

Where <adapter> is the name of your adapter without the hubot- prefix.

Deployment

% heroku create --stack cedar
% git push heroku master

If your Heroku account has been verified you can run the following to enable and add the Redis to Go addon to your app.

% heroku addons:add redistogo:nano

If you run into any problems, checkout Heroku's docs.

You'll need to edit the Procfile to set the name of your hubot.

More detailed documentation can be found on the deploying hubot onto Heroku wiki page.

Deploying to UNIX or Windows

If you would like to deploy to either a UNIX operating system or Windows. Please check out the deploying hubot onto UNIX and deploying hubot onto Windows wiki pages.

Campfire Variables

If you are using the Campfire adapter you will need to set some environment variables. If not, refer to your adapter documentation for how to configure it, links to the adapters can be found on Hubot Adapters.

Create a separate Campfire user for your bot and get their token from the web UI.

% heroku config:add HUBOT_CAMPFIRE_TOKEN="..."

Get the numeric IDs of the rooms you want the bot to join, comma delimited. If you want the bot to connect to https://mysubdomain.campfirenow.com/room/42 and https://mysubdomain.campfirenow.com/room/1024 then you'd add it like this:

% heroku config:add HUBOT_CAMPFIRE_ROOMS="42,1024"

Add the subdomain hubot should connect to. If you web URL looks like http://mysubdomain.campfirenow.com then you'd add it like this:

% heroku config:add HUBOT_CAMPFIRE_ACCOUNT="mysubdomain"

Restart the bot

You may want to get comfortable with heroku logs and heroku restart if you're having issues.