Hubot のインストールと Hello World


初めて Hubot (読み:ヒューボット)に触れたのは 2 年前。
簡単なスクリプトを書いて実行してみただけで、それ以降触っていなかった。
久しぶりに、インストールから Hello World までを実施してみる。

なお、以下の記事を参考にさせていただいた。

第2回 インストールとHello World:GitHub社謹製! bot開発・実行フレームワーク「Hubot」|gihyo.jp … 技術評論社

環境

Ubuntu 16.04.1 LTS (VMware Workstation 12 Player 上に環境構築)

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"

パッケージの更新

$  sudo apt-get update
$  sudo apt-get upgrade

インストール

Node.jsとnpm

$ sudo apt-get install nodejs npm

バージョン確認

$ node -v
v4.2.6
$ npm version
{ npm: '3.5.2',
  ares: '1.10.1-DEV',
  http_parser: '2.5.0',
  icu: '55.1',
  modules: '46',
  node: '4.2.6',
  openssl: '1.0.2g-fips',
  uv: '1.8.0',
  v8: '4.5.103.35',
  zlib: '1.2.8' }

Hubot

$ sudo npm install -g hubot coffee-script

バージョン確認

$ npm list -g hubot coffee-script
/usr/local/lib
├── [email protected] 
└─┬ [email protected] 
  └── [email protected] 

動作確認

“myhubot” という名前でひな形を作ろうと、コマンドを入力すると、次のメッセージが表示された。

$ hubot --create myhubot
'hubot --create' is deprecated. Use the yeoman generator instead:
    npm install -g yo generator-hubot
    mkdir -p myhubot
    cd myhubot
    yo hubot
See https://github.com/github/hubot/blob/master/docs/index.md for more details on getting started.

言われた通りにする。

$ sudo npm install -g yo generator-hubot

バージョン確認

$ npm list -g yo generator-hubot
/usr/local/lib
├── [email protected] 
└── [email protected] 

ひな形を作成

$ mkdir -p myhubot
$ cd myhubot
$ yo hubot

以下の項目を設定

  • Owner : suppy193 <メールアドレス>
  • Bot name : myhubot
  • Description : My first hubot
  • Bot adapter : campfire (デフォルトの設定)

以下のようなディレクトリ、ファイルが作成される

$ ls -la
合計 52
drwxrwxr-x   5 suppy193 suppy193 4096 Dec 12 09:53 .
drwxr-xr-x  20 suppy193 suppy193 4096 Dec 12 09:53 ..
-rw-r--r--   1 suppy193 suppy193  197 May 20  2016 .editorconfig
-rw-r--r--   1 suppy193 suppy193   39 May 20  2016 .gitignore
-rw-r--r--   1 suppy193 suppy193   27 Dec 12 09:53 Procfile
-rw-r--r--   1 suppy193 suppy193 7844 Dec 12 09:53 README.md
drwxrwxr-x   2 suppy193 suppy193 4096 Dec 12 09:53 bin
-rw-rw-r--   1 suppy193 suppy193  213 Dec 12 09:53 external-scripts.json
-rw-rw-r--   1 suppy193 suppy193    2 Dec 12 09:53 hubot-scripts.json
drwxrwxr-x 107 suppy193 suppy193 4096 Dec 12 09:53 node_modules
-rw-rw-r--   1 suppy193 suppy193  573 Dec 12 09:53 package.json
drwxrwxr-x   2 suppy193 suppy193 4096 Dec 12 09:53 scripts

Hubot を起動

$ bin/hubot
myhubot> [Mon Dec 12 2016 09:55:37 GMT+0900 (JST)] 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 Dec 12 2016 09:55:37 GMT+0900 (JST)] 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)`
[Mon Dec 12 2016 09:55:37 GMT+0900 (JST)] INFO hubot-redis-brain: Using default redis on localhost:6379

Enter キーを押すとプロンプトが表示される。

myhubot> 

ヘルプを表示

myhubot> myhubot help
myhubot> Shell: myhubot adapter - Reply with the adapter
myhubot animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.
myhubot echo <text> - Reply back with <text>
myhubot help - Displays all of the help commands that Hubot knows about.
myhubot help <query> - Displays all help commands that match <query>.
myhubot image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
myhubot map me <query> - Returns a map view of the area returned by `query`.
myhubot mustache me <url|query> - Adds a mustache to the specified URL or query result.
myhubot ping - Reply with pong
myhubot pug bomb N - get N pugs
myhubot pug me - Receive a pug
myhubot the rules - Make sure hubot still knows the rules.
myhubot time - Reply with current time
myhubot translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.
myhubot translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional
ship it - Display a motivation squirrel

Enter キーを押すとプロンプトが表示される。

myhubot> 

デフォルトで作成されているスクリプトの "ping" を実行

myhubot> myhubot ping
myhubot> PONG

"PONG" と返答
Enter キーを押すと、プロンプトが表示される

Hubot を終了

myhubot> exit

初めから scripts ディレクトリにあるのは、example.coffee のみである。
このファイルには、多くのサンプルが含まれている。

$ ls -l scripts
合計 4
-rw-r--r-- 1 suppy193 suppy193 3360 May 20  2016 example.coffee

hello スクリプト

以下のスクリプトを作成する

./scripts/hello.coffee
# Description:
#   Hubotと挨拶
#
# Commands:
#   hubot hello - Reply with hello
#
# Author:
#   suppy193

module.exports = (robot) ->
  robot.respond /HELLO$/i, (msg) ->
    msg.send "HELLO"

Hubot を起動して、 hello.coffee を読み込む

$ bin/hubot

コマンドを実行

myhubot> myhubot hello
myhubot> HELLO

コマンドに対して、"HELLO" と返答する。

ヘルプには、スクリプトの "Commands:" に記述した内容が追記されている。

myhubot> myhubot help
myhubot> Shell: myhubot adapter - Reply with the adapter
(略)
myhubot echo <text> - Reply back with <text>
myhubot hello - Reply with hello
myhubot help - Displays all of the help commands that Hubot knows about.
(略)

以上。