Wouldn’t it be more fun to have a Bot that you can chat with, and help in your daily routines?!

Imagine that you are a chairman and you have a personal assistant whose mind is yours, and you can shape it to do the things exactly as you need them to be done. ChatOps with Hubot configuration on windows and its brain shaping is our scope in this article. This is the second article in Mared series. For an overview about this article series goals and for a good introduction to what we are discussing here in this article, kindly refer to the first article in this series here.

image from https://i.ytimg.com/vi/hJ96smUsZVA/maxresdefault.jpg

Mared series articles:

Mared (Part 1): ChatOps with Hubot For Continuous Integration & Deployment on Windows.

Mared (Part 2): Configure Hubot & Shape its Brain on Windows

Hubot:

Hubot is an open source project invented by GitHub, inc. They define it as follows “Hubot is your company’s robot. Install him in your company to dramatically improve and reduce your employee efficiency”. Hubot is developed with CoffeeScript on Node.js. But don’t worry no need to be expert in CoffeeScript, just a small knowledge is enough to start. Well if you are an expert this would be a big plus :D

Image from https://hubot.github.com/

Mared born:

Mared is our chatting bot which is being used by my team to apply continuous integration & deployment to our web apps. For more info about Mared and how it begins refer to my first article here.

In the rest of this article we are going to walk through all the steps in details to fully configure — Hubot with chat client (Slack) — Mared.

Prerequisites:

1- Chat client (e.g. Campfire, Slack, …) the process should be similar for most of them. In this article we’re going to use Slack.

2- Windows machine with PowerShell version 3, or 4. Version 4 is more preferred. We can easily upgrade PowerShell version to 4 by following the instructions in this simple tutorial.

3- Admin privilege for the windows machine.

Configuration steps:

1- Install Node.js if it isn’t installed. Latest or recommended version could be found here

2- Open Command Prompt Window CMD as an admin.

3- Assuring npm installed with the latest update; type the following command:

npm install -g npm

npm install -g npm

4- Create folder to be our Mared container; start, stop, and restart is going to be done using this directory.

5- In CMD change the working directory to the new created folder in previous step, then type the following command:

npm install -g yo generator-hubot

As a result all the Hubot required modules will be installed.

npm install -g yo generator-hubot

6- To initialize Hubot type in CMD the following command:

yo hubot --adatpter=slack

It will ask for some initialization data (Author name, Bot name, and optional description).

Remember the Bot name as we’re gonna need this in next steps.

yo hubot --adapter=slack

In this stage Hubot files and folders will be generated in our working folder; the one we created in step 4.

Mared working folder

7- Navigate to “Configure Apps” link in your Slack team page, and search for Hubot.

App directory in Slack

8- Select “Add Configuration” button; admin approval will be required.

9- Type Hubot name you selected in step 6 for the “Customize name” entry then click “Save Integration” button, as a result Hubot will get configured to work with Slack and an Api token will be generated for you.

Slack Api token for Hubot

10- Add new System Environment variable as follows:

Name: “HUBOT_SLACK_TOKEN”.

Value: Slack Api token generated in step 9.

— Right click on “This PC” or “My Computer” icon -> Advanced system settings -> Environment variables, then add a new variable in “System variables”.

11- Open new CMD as an admin — to access environment variables — and map to Hubot working folder, then type the following command:

.\bin\hubot --adapter slack

.\bin\hubot --adapter slack

Congratulations! Mared is now live, and you can chat with! Just invite him in Slack either in a group message or direct one and start chatting.

Hubot in action

So far Mared can understand and respond to default commands pre-defined by Hubot in chatting, but we can personalize and increase its capabilities more and more. If you know how to teach it, it will surprise you a lot!

Mared Brain

Hubot comes with default scripts and plugin and one of them is

“hubot-redis-brain”. Referring to our working folder — the one we created in step 4 of configuration steps — there is “external-scripts.json” and in it we can find all installed plugins so far.

“hubot-redis-brain” in external-scripts.json

Hubot supports different ways to be used as a brain storage (e.g. File, Redis), but for the scope of this article we’re going to illustrate how to use Redis as Hubot brain.

So far by the time of writing this article, Redis project doesn’t officially supports windows, but the Microsoft Open Tech group develop and maintain this Windows port, and you can download the latest MSI package from here.

MSI package will install Redis as a windows service.

Redis service

In Redis installation directory, we’ll notice that there’re some “.exe” files, and the most important one for us now is “redis-cli.exe”, as we’re going to use it to query the Redis storage.

“redis-cli.exe” in Redis installation folder

Now run Mared again using the following command:

.\bin\hubot --adapter slack

— the same way mentioned in step 11 in configuration steps section.

Wow! we’ll notice that Mared discovered a new Redis brain and started to initialize data to it.

Mared using Redis brain

Brain feeding:

Mared has its own brain now, let’s start feeding this brain with some info, and let’s start by introducing him to his colleagues in Slack team.

Hubot support external plugins which we can browse on the Node.js package manager site or on GitHub. “hubot-auth” is one of the external plugins Hubot use to be able to store some data about Slack users and also to be able to assign them to different customized roles as desired.

- Stop current Hubot instant at CMD by pressing “ctrl + c” keys then type “y” then press enter key.

- Now type the following command

npm install --save hubot-auth

This should install “hubot-auth” plugin for us and you can check

“node-modules” folder for the newly plugin.

- Then it’s time to edit the “external-scripts.json” plugin to add “hubot-auth” entry if it hasn’t already been added.

“node-modules” folder contains “hubot-auth” plugin

“external-scripts.json” include “hubot-auth” plugin

Mared new plugin needs to define admin or group of admins from Slack users, and it needs Slack users ids not the names. To get Slack ids we need to use some Slack APIs to get Slack users data including their ids.

Date retrieved from Slack API

Now get all the users’ ids who you want Mared to trust the most, and create environment variable as required by “hubot-auth” plugin as follows:

Name: “HUBOT_AUTH_ADMIN”.

Value: All Mared admins’ ids separated by comma.

“hubot-auth” source from https://github.com/hubot-scripts/hubot-auth/blob/master/src/auth.coffee

Run Mared again the same way we mentioned before in step 11 in configuraton steps section. Navigate to Redis client “redis.cli.exe”, and type the following command:

get hubot:storage

This should show an object stored in Redis containing all Slack users. If you can see your users stored in Mared Redis brain, then please skip section “Mared brain issue”.

Mared brain issue

Update(23–01-2017): This problem has been solved in the latest versions of “hubot-slack”, and “hubot-auth” which are v4.3.1, and v2.0.0 respectively by the time of writing this update. Below workaround is left for reference only.

By the time of writing this article Slack integration with Hubot and its authentication plugin has some issues in getting Slack users data and store them in Hubot Redis brain. But no worries there is a workaround we can use till a fix gets committed. Workaround suggested by me and other workarounds as well could be found in this thread. I’m going to show my workaround here as well.

The issue cause seems to be in new versions only, as reported it was working fine without a problem in previous versions. So the workaround is to revert to versions where this specific issue wasn’t exist. Steps are as follows: 1- Open CMD as admin and map to Hubot working folder

2- npm uninstall --save hubot-slack

3- npm uninstall --save hubot-auth

4- npm install --save hubot-slack@3.4.2

5- npm install --save hubot-auth@1.2.0

Time to run Mared again the same way. Open Redis client and type

get hubot:storage

Now you should see all the Slack users stored in Mared Redis brain.

Slack users in Mared Redis brain

In Slack client start ask Mared about your current roles and assign yourself to different roles in case you are an admin.

Mared support roles

Mared with CICD:

As shown above Mared now has its own brain and ready to be taught some cool stuff!

As planned in the first article of this series, we need to teach Mared about CICD — Continuous Integration & Deployment — specially TeamCity.

Another external plugin could be added to Mared to be able to understand how to deal with TeamCity. External TeamCity plugin for Hubot is just a CoffeeScript file contains some commands for Hubot to be able to deal with TeamCity through its APIs. Download the plugin and add the CoffeeScript file to “scripts” folder in Hubot working folder.

teamcity.coffee added to scripts folder

“teamcity.coffee” requires another module which need to be installed first, so open CMD as admin, map to Hubot working folder, then type

npm install --save underscore

undersocore module required by teamcity plugin

We need to add four environment variables used by Hubot TeamCity plugin. If you’ve already installed TeamCity add the following values else postpone the following step till we configure TeamCity in details in coming articles in Mared series.

1- Name: “HUBOT_TEAMCITY_USERNAME”

Value: Valid TeamCity user name.

2- Name: “HUBOT_TEAMCITY_PASSWORD”

Value: Password for user name in step 1.

3- Name: “HUBOT_TEAMCITY_HOSTNAME”

Value: TeamCity host name (TeamCity url).

4- Name: “HUBOT_TEAMCITY_SCHEME”

Value: defaulted to “http” and could be overwritten according to your environment.

TeamCity plugin configuration

After restarting Mared we’re going to see all available commands which Mared can understand so far by typing “help” in chat with Mared. TeamCity commands could also be found.

All commands which Mared can understand so far

And finally we need to make sure that Mared is up and running all the time, and for this we’re going to do the following.

1- Create a “.ps1” file in Hubot working folder with the name “starting_mared.ps1”.

starting_mared.ps1

2- Start a new windows task and trigger it to start upon windows start. In its action add the following command:

ExecutionPolicy ByPass; E:; cd Mared; .\starting_mared.ps1

—change the folder name and partition according to your environment— , and choose “program/script” as “Powershell.exe”.

Windows task action

Conclusion:

Mared is now live as an independent entity. You can proceed in personalizing it and teach it cool stuff as much as you can. In Mared article series our scope is to teach him how to apply Continuous Integration/Deployment.

In coming articles we’re going to walk through the configuration process of TeamCity, Octopus Deploy, and how to integrate them with Mared.