LoginSignup
43
44

More than 5 years have passed since last update.

Hubot スクリプトを追加インストールしよう & 自分だけの Hubot スクリプトをつくろう

Last updated at Posted at 2014-12-04

これは Hubot Advent Calendar 2014 の 4 日目の記事です。

また @bouzuya の Hubot 連載の第 4 回です。目次は、第 1 回の記事にあるので、そちらをどうぞ。

前回は Hubot を Heroku で動かして Slack から話す方法 について紹介しました。ここまでで Hubot は元気よく動きますし、みんなで hubot pug bomb してログもめちゃくちゃ、最高ですね!

……でも、まだまだです。

もっとイカした Hubot にするために、今回は Hubot スクリプトの追加を紹介しましょう。

前提条件

その前に今回の前提条件を。

  • 前回までにつくった Hubot
  • GitHub アカウント

今回は GitHub 上にある Hubot スクリプトを fork し、書きかえますので、GitHub アカウントと基本的な操作ができることが前提です。

Hubot スクリプトを追加してみよう (bouzuya/hubot-hello-world)

さて、まずは @bouzuya のつくった Hubot スクリプトをインストールしてみましょう。

まずは npm を使って、 package.jsonbouzuya/hubot-hello-world への依存関係を追加し、node_modules/hubot-hello-world にダウンロードしてもらいましょう。妙に日本語で書くと長いですが、コマンドは短いです。

$ npm install --save bouzuya/hubot-hello-world

これで GitHub の bouzuya/hubot-hello-world から取得できます。npm リポジトリに登録しなくてもいいんですね。便利!

次に external-scripts.json を編集し、"hubot-hello-world" を追記します。

$ vi external-scripts.json
... () ...

$ cat external-scripts.json
[
  "hubot-hello-world",
  "hubot-diagnostics",
  "hubot-help",
  "hubot-heroku-keepalive",
  "hubot-google-images",
  "hubot-google-translate",
  "hubot-pugme",
  "hubot-maps",
  "hubot-redis-brain",
  "hubot-rules",
  "hubot-shipit",
  "hubot-youtube"
]

external-scripts.json は JSON ファイルです。ここには Hubot の起動時に読み込む npm パッケージ名を書いておきます。

これで Hubot スクリプトのインストールは完了です。

さっそく試してみましょう。手元で動きを見たいだけなので、shell アダプターを使いましょう。

$ ./bin/hubot -a shell -n uran
uran> uran hello
uran> World!

やりました。

Hubot とふたりで完成させる Hello, World! 。もうひとりじゃない。

インストールした Hubot スクリプトの解説

bouzuya/hubot-hello-worldsrc/hello-world.coffee を見てください。

hello-world.coffee
# Description
#   A Hubot script that responds 'World!'
#
# Configuration:
#   None
#
# Commands:
#   hubot hello - responds 'World!'
#
# Author:
#   bouzuya <m@bouzuya.net>

module.exports = (robot) ->
  robot.respond /hello/i, (msg) ->
    msg.send 'World!'

本体は 3 行です。 CoffeeScript 苦手な人向けに JavaScript で書くとこんな感じ。

module.exports = function(robot) {
  robot.respond(/hello/i, function(msg) {
    msg.send('World!');
  });
};

robot.respond(pattern, callback) は listener を登録するメソッドです。Hubot に話しかけるような形 (例えば /hello/i という正規表現は /hubot hello/i のような形 (厳密には違います) で登録されます) に変換されます。パターンにマッチした場合に callback が呼び出されます。

msg.send(message)message を送信するメソッドです。チャットにメッセージを送ります。

なので、hubot hello と話しかけると World! と返されるわけです。

ちなみに、コメントのうち Commands: に書いたものは hubot help などで使われるものです (詳細は後日) 。

メッセージを変えてみよう

じゃあ、メッセージを書き換えて独自のスクリプトにしてみましょう。

まず bouzuya/hubot-hello-world を fork し、git clone します。

右上の fork ボタンでも、hub コマンドで fork しても好きにしてください。

fork.png

次は git clone して……

$ git clone git@github.com:<your GitHub user name>/hubot-hello-world.git

$ cd hubot-hello-world

$ vi src/hubot-hello.coffee

先ほどの src/hello-world.coffee を書き換えます。お好きなメッセージで。

hello-world.coffee

...

module.exports = (robot) ->
  robot.respond /hello/i, (msg) ->
    msg.send 'こんにちは。あれれー英語ですか?ぷすー'

で、git commit して git push

$ git commit -am 'change message'

$ git push origin master

GitHub に Hubot スクリプト (リポジトリ) を準備できました。

新しい Hubot スクリプトをインストール (<your GitHub user name>/hubot-hello-world)

先ほどの bouzuya/hubot-hello-world と同じようにインストールしていきます。

ただ、今回は既に bouzuya/hubot-hello-world をインストールしてしまっているので、部分的に書き換えていきます。

package.json を変更して依存関係を bouzuya/hubot-hello-world から <your GitHub user name>/hubot-hello-world に変更します。

package.json
...
  "dependencies": {
    ...
    "hubot-hello-world": "git://github.com/<your GitHub user name>/hubot-hello-world",
    ...
  },

...

改めて npm install

$ rm -rf node_modules/hubot-hello-world

$ npm install

external-script.json はさきほどのものをそのままで OK です。

では、動かしてみましょう。

$ ./bin/hubot -a shell -n uran
uran> uran hello
uran> こんにちは。あれれー英語ですか?ぷすー

これは腹が立つ。

最後に

駆け足ですが、標準以外の Hubot スクリプトのインストールと、Hubot スクリプトの作成 (forkしての作成) をためしました。ゼロからの Hubot スクリプトの作成は、また次回です。

明日は mako09 さんが書いてくれるようです。楽しみです。ここまでは Hubot Advent Calendar というより @bouzuya Advent Calendar ですからね。

では。

43
44
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
44