Hubot をインストールし HipChat と連携を行う


## 前書き

記事探せば豊富に見つかりますし超今更感が否めませんが、備忘録として残しておきます。

## nodebrew

[hokaccha/nodebrew: Node.js version manager](https://github.com/hokaccha/nodebrew "hokaccha/nodebrew: Node.js version manager")

### インストール

Node.js をバージョン管理したいため nodebrew を使います。

```
$ curl -L git.io/nodebrew | perl - setup
```

or

```
$ wget git.io/nodebrew
$ perl nodebrew setup
```

### PATH を追加

```
echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bashrc
```

### `~/.bashrc` を再読み込み

```
$ source ~/.bashrc
```

### 確認

```
$ nodebrew help
```

## Node.js

### インストールできるバージョンを確認

```
$ nodebrew ls-remote
```

### インストール

```
$ nodebrew install-binary v5.7.1
```

### バージョンを選択

```
$ nodebrew use v5.7.1
use v5.7.1
```

### 確認

インストールしたバージョンをすべて表示します。
`current` が選択したバージョンとなります。

```
$ nodebrew ls
v5.7.1

current: v5.7.1
```

## Hubot

[github/hubot: A customizable life embetterment robot.](https://github.com/github/hubot "github/hubot: A customizable life embetterment robot.")

### インストール

Hubot 2.9.x は Yeoman ジェネレーターでインストールするようになっています。

```
$ npm install -g coffee-script yo generator-hubot
```

### インスタンスを生成

```
$ mkdir myhubot
$ cd myhubot
$ yo hubot
                     _____________________________  
                    /                             \ 
   //\              |      Extracting input for    |
  ////\    _____    |   self-replication process   |
 //////\  /_____\   \                             / 
 ======= |[^_/\_]|   /----------------------------  
  |   | _|___@@__|__                                
  +===+/  ///     \_\                               
   | |_\ /// HUBOT/\\                             
   |___/\//      /  \\                            
         \      /   +---+                            
          \____/    |   |                            
           | //|    +===+                            
            \//      |xx|                            

? Owner kotarella1110@gmail.com
? Bot name myhubot
? Description A simple helpful robot for your Company
? Bot adapter (campfire) hipchatgot back false
? Bot adapter hipchat
   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|                            
```

### 起動

```
$ cd ~/myhubot
$ bin/hubot
myhubot> [Thu Mar 03 2016 08:16:24 GMT+0000 (UTC)] ERROR hubot-heroku-alive included, but missing HUBOT_HEROKU_KEEPALIVE_URL. `heroku config:set HUBOT_HEROKU_KEEPALIVE_URL=$(heroku apps:info -s  | grep web-url | cut -d= -f2)`
[Thu Mar 03 2016 08:16:24 GMT+0000 (UTC)] INFO hubot-redis-brain: Using default redis on localhost:6379
[Thu Mar 03 2016 08:16:24 GMT+0000 (UTC)] INFO hubot-redis-brain: Data for hubot brain retrieved from Redis
```

`ERROR hubot-heroku-alive included, but missing HUBOT_HEROKU_KEEPALIVE_URL.` というエラーが発生したため、下記のように修正します。

### `external-scripts.json` を修正

```diff:~/myhubot/external-scripts.json
  [
    "hubot-diagnostics",
    "hubot-help",
-   "hubot-heroku-keepalive",
    "hubot-google-images",
    "hubot-google-translate",
    "hubot-pugme",
    "hubot-maps",
    "hubot-redis-brain",
    "hubot-rules",
    "hubot-shipit"
  ]
```

### 再度起動し確認

```
$ cd ~/myhubot
$ bin/hubot
myhubot> [Thu Mar 03 2016 08:24:56 GMT+0000 (UTC)] INFO hubot-redis-brain: Using default redis on localhost:6379
[Thu Mar 03 2016 08:24:56 GMT+0000 (UTC)] INFO hubot-redis-brain: Data for hubot brain retrieved from Redis

myhubot> myhubot ping
myhubot> PONG

```

## HipChat

[hipchat/hubot-hipchat: HipChat adapter for GitHub's Hubot](https://github.com/hipchat/hubot-hipchat "hipchat/hubot-hipchat: HipChat adapter for GitHub's Hubot")

### HipChat Adapter をインストール

HipChat本体はAdapterというものを通してHubotとやりとりを行います。

```
$ cd ~/myhubot
$ npm install hubot-hipchat --save
```

```diff:~/myhubot/bin/hubot
  #!/bin/sh
  
  set -e
  
  npm install
  export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"
+ 
+ export HUBOT_HIPCHAT_JID="XXXXXX_XXXXXX@chat.hipchat.com"
+ export HUBOT_HIPCHAT_PASSWORD="XXXXXX"
+ export HUBOT_HIPCHAT_ROOMS="XXXXXX_hubot@conf.hipchat.com"
+ export HUBOT_LOG_LEVEL="debug"
  
  exec node_modules/.bin/hubot --name "myhubot" "$@"
```

### 起動

```
$ bin/hubot -a hipchat
```

### 確認

![HipChat で Hubot とやりとり](https://lh3.googleusercontent.com/ovpQkwE9lZrsO43LddboZKtLmFOy8HjGU765W5iRlRITlvzKEokiFZePyA9IrQNJE5Po7fW1uHACQQqD6JUNe3K1tcUFN2pg7SPKoV7eCUbJXYdR9kOKlcyAmLQQd8rvFH7Efom5aF2ZD0dhaY5dar0qqamChYuUD9LUSGLOV4eY9Dyupg4Rt40tqd5kHSfcf4J44tb5xdkIVGbZycQ1DLrZgGixxIVECFSJAHOOusJI_tmNVTD-zHwZFD9w2kruBMUY6XbGpR2jVc5K36i6_BSH01EJbii9MYU8ANZOqLJ5rEBmxiKEkrtqKnJCnSlFKeE0SISvLZ2mSce3P8sTxurm0YchYEARsBY6Xfup3dITuGjtFeMGwo5lh21F6cr_M4gchIQH6EEbOdm3_x21fD84YQc7tNYGwH4mA5i6q3Jg83kZq7-zlmrJEuEdoMi_SreodAGDiK8gTLDbUnfFhjcVg6q3lfnfStWEpbB7-e-RQrd7u3d8wUj3z-z3eBcoImct1RCM3lAi-bX6yVAmiJ4mBf6iTUZzWr7w55yAvtzvdpPHrtJovZrOpJCR3fG7d92fjzjCozkAyD9e6Uike30Bf5q62g=w1618-h1044-no "HipChat で Hubot とやりとり")

## forever

forever をインストールして Hubot をデーモン化します。

[foreverjs/forever: A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)](https://github.com/foreverjs/forever "foreverjs/forever: A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)")

### インストール

```
$ npm install forever -g
```

### `bin/hubot` の編集

```diff:bin/hubot
  #!/bin/sh
  
  set -e
  
  npm install
  export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"
  
  export HUBOT_HIPCHAT_JID="XXXXXX_XXXXXX@chat.hipchat.com"
  export HUBOT_HIPCHAT_PASSWORD="XXXXXX"
  export HUBOT_HIPCHAT_ROOMS="XXXXXX_hubot@conf.hipchat.com"
  export HUBOT_LOG_LEVEL="debug"
  
- exec node_modules/.bin/hubot --name "myhubot" "$@"
+ forever start -c coffee node_modules/.bin/hubot -a hipchat
```

### 起動

```
$ bin/hubot
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: node_modules/.bin/hubot
```

### 確認

```
$ forever list
info:    Forever processes running
data:        uid  command script                             forever pid  id logfile                         uptime
data:    [0] 2sAI coffee  node_modules/.bin/hubot -a hipchat 2137    2142    /home/vagrant/.forever/2sAI.log 0:0:0:19.806
```

### 参考文献

* [Hubotをインストール、hipchatと連携し、foreverでデーモン化(CentOS6.4) - Qiita](http://qiita.com/kon_yu/items/cdc452bcd2bb89f85de1 "Hubotをインストール、hipchatと連携し、foreverでデーモン化(CentOS6.4) - Qiita")
* [【Hubot】HubotとHipChatでお手軽ChatOpsを作る - Quadrophenia](http://quadrophenia.hateblo.jp/entry/2015/07/21/213830 "【Hubot】HubotとHipChatでお手軽ChatOpsを作る - Quadrophenia")