How to integrate Hubot with Slack to create an efficient Chatbot

In this article, let me get you through the steps to integrate  Hubot  (a chatbot app) with  slack . Have you thought of having a chatbot who can automate most of your office works? like Jarvis of Tony Stark  (not exactly 😛 ). If yes, go ahead and read this article.

If you are not familiar with Hubot, visit official page here >>. You should already be familiar with Slack which is a popular office chat application. Let’s start integrating Hubot with Slack.

Step 1: Install NodeJS and NPM for Hubot.

This is the first step to integrate Hubot with Slack. Let’s bring up a Linux box and install NodeJS  and NPM (NPM is a package manager of javascript modules) on it.

In Ubuntu/Debian:

sudo apt-get update
curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -     #It downloads PPA for latest LTS version of nodejs
sudo apt install nodejs

In CentOS/RedHat:

yum update
sudo yum install epel-release
sudo yum install nodejs
sudo yum install npm

Once done, check version of NPM and NodeJS to verify it was properly installed.

node -v 
npm -v

Step 2: Install Hubot on the box.

Once you have installed NodeJS and NPM. You can proceed with installation of Hubot package. Let’s start by installing Yeoman which would be used to kickstart our Hubot  chatbot.

sudo npm install -g yo generator-hubot

Output would look like this:

vagrant@ubuntu-bionic:~$ sudo npm install -g yo generator-hubot
npm WARN deprecated CSSselect@0.4.1: the module is now available as 'css-select'
npm WARN deprecated CSSwhat@0.4.7: the module is now available as 'css-what'
npm WARN deprecated simple-bufferstream@0.0.4: no longer maintained
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated minimatch@0.3.0: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated cross-spawn-async@2.2.5: cross-spawn no longer requires a build toolchain, use it instead
/usr/bin/yo -> /usr/lib/node_modules/yo/lib/cli.js
/usr/bin/yo-complete -> /usr/lib/node_modules/yo/lib/completion/index.js

> spawn-sync@1.0.15 postinstall /usr/lib/node_modules/yo/node_modules/spawn-sync
> node postinstall


> yo@2.0.5 postinstall /usr/lib/node_modules/yo
> yodoctor


Yeoman Doctor
Running sanity checks on your system

✔ Global configuration file is valid
✔ NODE_PATH matches the npm root
✔ Node.js version
✔ No .bowerrc file in home directory
✔ No .yo-rc.json file in home directory
✔ npm version
✔ yo version

Everything looks all right!
+ yo@2.0.5
+ generator-hubot@0.4.0
added 845 packages from 303 contributors in 15.834s

Now create a directory jarvis  and switch to the directory. Note: This directory name would be your bot name.

vagrant@ubuntu-bionic:~$ mkdir jarvis
vagrant@ubuntu-bionic:~$ cd jarvis/

Now use Yeoman to kickstart your chatbot Jarvis , for that run below command.

yo hubot

Output would look like this:

vagrant@ubuntu-bionic:~/jarvis$ yo hubot
? ==========================================================================
We're constantly looking for ways to make yo better!
May we anonymously report usage statistics to improve the tool over time?
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== Yes
                     _____________________________
                    /                             \
   //\              |      Extracting input for    |
  ////\    _____    |   self-replication process   |
 //////\  /_____\   \                             /
 ======= |[^_/\_]|   /----------------------------
  |   | _|___@@__|__
  +===+/  ///     \_\
   | |_\ /// HUBOT/\\
   |___/\//      /  \\
         \      /   +---+
          \____/    |   |
           | //|    +===+
            \//      |xx|

? Owner Nijil
? Bot name jarvis
? Description Slack Bot
? Bot adapter campfire
   create bin/hubot
   create bin/hubot.cmd
   create Procfile
   create README.md
   create external-scripts.json
   create hubot-scripts.json
   create .gitignore
   create package.json
   create scripts/example.coffee
   create .editorconfig
                     _____________________________
 _____              /                             \
 \    \             |   Self-replication process   |
 |    |    _____    |          complete...         |
 |__\\|   /_____\   \     Good luck with that.    /
   |//+  |[^_/\_]|   /----------------------------
  |   | _|___@@__|__
  +===+/  ///     \_\
   | |_\ /// HUBOT/\\
   |___/\//      /  \\
         \      /   +---+
          \____/    |   |
           | //|    +===+
            \//      |xx|

npm notice created a lockfile as package-lock.json. You should commit this file.
+ hubot-maps@0.0.3
+ hubot-google-translate@0.2.1
+ hubot-pugme@0.1.1
+ hubot-scripts@2.17.2
+ hubot-google-images@0.2.7
+ hubot@3.2.0
+ hubot-help@1.0.1
+ hubot-diagnostics@1.0.0
+ hubot-redis-brain@1.0.0
+ hubot-heroku-keepalive@1.0.3
+ hubot-shipit@0.2.1
+ hubot-rules@1.0.0
added 91 packages from 53 contributors and audited 178 packages in 7.974s
found 0 vulnerabilities

Once done, start your Jarvis chatbot by running below command.

./bin/hubot

Output would look like this:

vagrant@ubuntu-bionic:~/jarvis$ ./bin/hubot
audited 178 packages in 1.714s
found 0 vulnerabilities

body-parser deprecated undefined extended: provide extended option node_modules/hubot/src/robot.js:446:21
jarvis> [Mon Feb 11 2019 15:49:04 GMT+0000 (UTC)] WARNING Loading scripts from hubot-scripts.json is deprecated and will be removed in 3.0 (https://github.com/github/hubot-scripts/issues/1113) in favor of packages for each script.

Your hubot-scripts.json is empty, so you just need to remove it.
[Mon Feb 11 2019 15:49:04 GMT+0000 (UTC)] INFO hubot-redis-brain: Using default redis on localhost:6379
[Mon Feb 11 2019 15:49:04 GMT+0000 (UTC)] ERROR hubot-heroku-keepalive included, but missing HUBOT_HEROKU_KEEPALIVE_URL. `heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=$(heroku apps:info -s | grep web.url | cut -d= -f2)`

jarvis>

At this point, you will see a prompt where you can interact with your chatbot. Let’s talk to our chatbot for the first time. For example,

jarvis> @jarvis ping
jarvis> PONG

Step 3: Integrate Hubot with Slack.

Let’s integrate Hubot with Slack. To do this, you need to install hubot-slack  on the box. Let’s do it.

cd jarvis/
npm install hubot-slack --save

Open Slack account in the browsers https://your_workspace_name.slack.com . And then expand your Workspace settings by clicking Workspace name on the top-left side of your page. Here my Workspace name is easyaslinux .

 

Then Click on Customize Slack .

Then Click Configure apps .

 

You will see a new tab opened on your browser. Search our Hubot application and Click on it.

 

Next, Click Add Configuration .

 

Next, give a name for your chatbot. I gave Jarvis  –  the one and only. Then Click Add Hubot Integration .

Make note of API token shown on the next page and Click Save Integration .

 

Now go to the box where Hubot  is installed, and run below command.

HUBOT_SLACK_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ./bin/hubot --adapter slack

Output would look like this.

vagrant@ubuntu-bionic:~/jarvis$ HUBOT_SLACK_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ./bin/hubot  --adapter slack
audited 269 packages in 1.818s
found 0 vulnerabilities

body-parser deprecated undefined extended: provide extended option node_modules/hubot/src/robot.js:446:21
[Sat Feb 16 2019 07:50:34 GMT+0000 (UTC)] INFO hubot-slack adapter v4.6.0
warn: SlackDataStore is deprecated and will be removed in the next major version. See project documentation for a migration guide.
warn: SlackDataStore is deprecated and will be removed in the next major version. See project documentation for a migration guide.
[Sat Feb 16 2019 07:50:34 GMT+0000 (UTC)] INFO Logged in as @jarvis in workspace easyaslinux
[Sat Feb 16 2019 07:50:35 GMT+0000 (UTC)] INFO Connected to Slack RTM
[Sat Feb 16 2019 07:50:35 GMT+0000 (UTC)] WARNING Loading scripts from hubot-scripts.json is deprecated and will be removed in 3.0 (https://github.com/github/hubot-scripts/issues/1113) in favor of packages for each script.

Your hubot-scripts.json is empty, so you just need to remove it.
[Sat Feb 16 2019 07:50:35 GMT+0000 (UTC)] INFO hubot-redis-brain: Using default redis on localhost:6379
[Sat Feb 16 2019 07:50:35 GMT+0000 (UTC)] ERROR hubot-heroku-keepalive included, but missing HUBOT_HEROKU_KEEPALIVE_URL. `heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=$(heroku apps:info -s | grep web.url | cut -d= -f2)`

This command should be running for the chatbot to be online. You should see Jarvis  online now under Apps  section.

 

Now invite Jarvis  to a channel like you invite a person.

/invite @jarvis

And say first Hello.

 

Congratulation! You have intergrated Hubot with slack. Now make your chatbot to do anything you want. You can add more intelligence to the chatbot by writing JavaScript or CoffeeScript and put it in directory scripts which located in the main bot directory . More info here >>

Step4: Daemonize Hubot service for production(Optional):

Now the Hubot  service is a foreground process. Whenever you exit terminal, the process also exits. It cannot be like this if you are planning to setup the chatbot for production. Let’s daemonize it.

As all Linux distributions are adopting Systemd  as initsystem, I will explain how you can daemonize Hubot service with Systemd.

Let’s create a run file for the Hubot service. Create file /root/hubot_run.sh  and add content given below.

#!/bin/bash
cd chatbot_directory && HUBOT_SLACK_TOKEN=XXXXXXXXXXXXXXXXXXXX ./bin/hubot  --adapter slack

Make it executable.

sudo chmod 755 /root/hubot_run.sh

Now create a Systemd unit file for the Hubot service.

sudo touch /etc/systemd/system/hubot.service

And add following content.

[Unit]
Description=Hubot service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/bin/bash /root/hubot_run.sh

[Install]
WantedBy=multi-user.target

Now Reload Systemd units and start the Hubot service.

sudo systemctl daemon-reload
sudo systemctl start hubot

Let’s check status of the Hubot service.

vagrant@ubuntu-bionic:~/jarvis/scripts$ sudo  systemctl status hubot
● hubot.service - Jarvis bot service
   Loaded: loaded (/etc/systemd/system/hubot.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-02-16 09:33:43 UTC; 2min 59s ago
 Main PID: 7274 (bash)
    Tasks: 11 (limit: 1152)
   CGroup: /system.slice/hubot.service
           ├─7274 /bin/bash /root/hubot_run.sh
           └─7281 node node_modules/.bin/coffee node_modules/.bin/hubot --name jarvis --adapter slack

Feb 16 09:33:46 ubuntu-bionic bash[7274]: Sat, 16 Feb 2019 09:33:46 GMT body-parser deprecated undefined extended: provide extended option at node_modules/hubot/src/ro
Feb 16 09:33:46 ubuntu-bionic bash[7274]: [Sat Feb 16 2019 09:33:46 GMT+0000 (UTC)] INFO hubot-slack adapter v4.6.0
Feb 16 09:33:46 ubuntu-bionic bash[7274]: warn: SlackDataStore is deprecated and will be removed in the next major version. See project documentation for a migration g
Feb 16 09:33:46 ubuntu-bionic bash[7274]: warn: SlackDataStore is deprecated and will be removed in the next major version. See project documentation for a migration g
Feb 16 09:33:47 ubuntu-bionic bash[7274]: [Sat Feb 16 2019 09:33:47 GMT+0000 (UTC)] INFO Logged in as @jarvis in workspace easyaslinux
Feb 16 09:33:47 ubuntu-bionic bash[7274]: [Sat Feb 16 2019 09:33:47 GMT+0000 (UTC)] INFO Connected to Slack RTM
Feb 16 09:33:47 ubuntu-bionic bash[7274]: [Sat Feb 16 2019 09:33:47 GMT+0000 (UTC)] WARNING Loading scripts from hubot-scripts.json is deprecated and will be removed i
Feb 16 09:33:47 ubuntu-bionic bash[7274]: Your hubot-scripts.json is empty, so you just need to remove it.
Feb 16 09:33:47 ubuntu-bionic bash[7274]: [Sat Feb 16 2019 09:33:47 GMT+0000 (UTC)] INFO hubot-redis-brain: Using default redis on localhost:6379
Feb 16 09:33:47 ubuntu-bionic bash[7274]: [Sat Feb 16 2019 09:33:47 GMT+0000 (UTC)] ERROR hubot-heroku-keepalive included, but missing HUBOT_HEROKU_KEEPALIVE_URL. `her

Use the enable  command to ensure that the Hubot service starts whenever the system boots.

sudo systemctl enable hubot

 

Hurray 🙂 You have completed the course for integrating Hubot with Slack.

As I mentioned above, you can add more intelligence to the chatbot by writing JavaScript or CoffeeScript and put it in directory scripts which located in the main bot directory . More info here >>

Thanks for the time taken to read my blog. Subscribe to this blog so that you don’t miss out anything useful (Checkout Right Sidebar for the Subscription Form) . Please also put your thoughts on this article as  comments .

 

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
x