An obvious first step of our journey is to make Hubot run. We will be focusing on Linux, so if you want to deploy it elsewhere, refer to the .
In this book we will be running everything on fresh installation of Debian 7 (Wheezy). Since Debian is the mothership of many popular Linux flavors, including Ubuntu, you should have no trouble doing the same with any distribution deriving from Debian.
You will need and . You may prefer to install it via your distribution’s , which is probably the easiest way, but to make sure we have the latest and greatest, we will .
Following installation is performed as root
user. If you are using Ubuntu and having trouble becoming root
, simply run sudo su
. Or keep adding sudo
to commands that require elevated permissions.
First, make sure you have the build tools.
apt-get install python g++ make checkinstall
Now, get the latest source and extract it.
cd /usr/local/src wget -N http://nodejs.org/dist/node-latest.tar.gz tar xzvf node-latest.tar.gz
Enter the directory you just extracted. Remember the version number, you will need it for compiling. At the moment of writing it was 0.10.24
.
root@botserv:/usr/local/src#
ls node-latest.tar.gz node-v0.10.24
root@botserv:/usr/local/src#
cd
node-v0.10.24/ root@botserv:/usr/local/src/node-v0.10.24#
Configure and install it.
./configure checkinstall -y --install=no --pkgversion 0.10.24 # Set correct version number dpkg -i node_*
At this point you should be able to run node
.
root@botserv:~#
node -v v0.10.24
To install npm
, just curl
the and pipe it to sh
. If you may want to make sure what the script does before executing it, first pipe it to less
.
curl https://npmjs.org/install.sh | sh
Test if it worked.
root@botserv:~#
npm -v 1.3.22
Hubot is written in , so let’s install that too.
npm install -g coffee-script
There is one last thing you will need. Hubot needs a “brain” to store the data, and it needs to work out of the box.
apt-get install redis-server
Make sure it’s running.
root@botserv:~#
redis-cli redis 127.0.0.1:6379> exit
Use npm to install Hubot
npm install -g hubot
You should now have hubot
command available.
root@botserv:~#
hubot -v 2.7.1
Now, we are almost there, but from this point we don’t want to run things as root
due to security concerns. If anyone manages to breach into your system using a security hole in Hubot or one of it’s scripts, the perpetrator will be isolated to Hubot’s system user.
Let’s create one. You may want to choose to use the default home directory to keep and run your Hubot, because there will be less hassle with permissions.
useradd --home-dir /home/hubot --create-home --shell /bin/bash hubot
Become the user and see if you can still invoke hubot
command.
root@botserv:~#
su - hubot hubot@botserv:~$
hubot -v 2.7.1
Since hubot user has no password, you can only log in to it using su
from a priviliged account. From non root
user you can do sudo su - hubot
.
spajus@botserv:~$
sudo su - hubot [sudo] password for spajus:
hubot@botserv:~$
Henceforth we will be performing shell commands using hubot
user rather than root
, unless explicitly stated otherwise.
Installing hubot
package did not provide you an instance of Hubot yet, so you will have to create one. First, examine what hubot
command gives you.
hubot@botserv:~$
hubot --help Usage hubot [options]
Available options:
-a, --adapter ADAPTER The Adapter to use
-c, --create PATH Create a deployable hubot
-d, --disable-httpd Disable the HTTP server
-h, --help Display the help information
-l, --alias ALIAS Enable replacing the robot's name with alias
-n, --name NAME The name of the robot in chat
-r, --require PATH Alternative scripts path
-v, --version Displays the version of hubot installed
The most interesting part of the output is the --adapter
option. We will explore several in later chapters, but for now we will stick to Campfire, which is built-in and will need no extra effort to run.
Now, create your first Hubot instance.
hubot@botserv:~$
hubot --create first-hubot Creating a hubot install at first-hubot
<some output omitted>
Renaming /home/hubot/first-hubot/gitignore -> /home/hubot/first-hubot/.gitign\
ore
To see if it works, start it in shell mode and run a hubot ping
command.
hubot@botserv:~$
cd
first-hubot/ hubot@botserv:~/first-hubot$
bin/hubot <npm will install dependencies on first run, output omitted>
Hubot> [Sat Jan 04 2014 01:55:33 GMT-0500 (EST)] WARNING The HUBOT_AUTH_ADMIN\
environment variable
not set
Hubot> [Sat Jan 04 2014 01:55:33 GMT-0500 (EST)] INFO Initializing new data f\
or brain
Hubot> hubot ping
Hubot> PONG
Hubot> exit
hubot@botserv:~/first-hubot$
You may see an error like this being printed in the output:
ERROR [Error: Redis connection to localhost:6379 failed - connect ECONNREFUSE\ D]
It means you don’t have redis-server
installed or running. See the previous paragraph or choose a different brain implementation if you don’t want to use Redis for Hubot. You can edit hubot-scripts.json
and replace redis-brain.coffee
with file-brain.coffee
. Hubot will then use brain-dump.json
file to save it’s memory. If you don’t provide any brain script at all, Hubot will still run, but it will lose all it’s memory after every restart, and since many scripts rely on the brain to store their state, brainless Hubot is not suitable for production use.
Let’s explore the newly created first-hubot
directory and see what’s in there.
We will use tree
command to draw ascii tree of the directory contents. You can get it with apt-get install tree
.
hubot@botserv:~$
tree first-hubot/ first-hubot/
├── bin
│ ├── hubot
│ └── hubot.cmd
├── external-scripts.json
├── hubot-scripts.json
├── node_modules
| └── <output omitted>
├── package.json
├── Procfile
├── README.md
└── scripts
├── auth.coffee
├── events.coffee
├── google-images.coffee
├── help.coffee
├── httpd.coffee
├── maps.coffee
├── ping.coffee
├── pugme.coffee
├── roles.coffee
├── rules.coffee
├── storage.coffee
├── translate.coffee
└── youtube.coffee
bin/
contains executables - hubot
is for Linux, Mac and alike, and hubot.cmd
is for Windows.external-scripts.json
is the place where you define custom scripts that will be used with Hubot. It can be either a path to script, or npm package name.hubot-scripts.json
holds a list of scripts you want to include from package, which holds hundreds of scripts contributed by the community.node_modules/
holds the dependencies that are checked and updated by npm
on every run, or by doing npm install
.package.json
is the . It holds the information about all the dependencies that your Hubot needs to run.Procfile
contains launch instructions for . You can use to run it in your own environment.README.md
holds a short introduction about Hubot itself, along with instructions how to deploy your bot to Heroku, configure Campfire, etc.scripts/
is a place to put your custom scripts. Any script placed there will be autoloaded and available after relaunching your Hubot. It contains some goodies already, though you may want to get rid of or modify some of them. For instance, scripts/ping.coffee
contains hubot die
command, which you don’t want to have unless your Hubot restarts after exiting. Somebody in the chat will definitely try to run it once in a while.