今更だけどhubot触ってみた~前編~[hubot][slack][coffeescript][heroku]
色々と記事も上がっているけれど自分の言葉に落とすためにも記事を書く。
hubotって?
github製のbotフレームワークです。 botをさくさくっと作れてかつ色々なチャットに簡単に登場させることのできるわくてかなものです。
Hubot is your company's robot.
イイカンジですね。
Today's version of Hubot is open source, written in CoffeeScript on Node.js, and easily deployed on platforms like Heroku. More importantly, Hubot is a standardized way to share scripts between everyone's robots.
Node.js上で動くCoffeeScriptで書かれていて、Herokuのようなプラットフォームに簡単にdeployできます。
bot作ってみたかったしCoffeeScript触ったことなかったしHerokuもなんだかんだ使ったことなかったのでちょうどいいやと好奇心で使ってみました!楽しい✌('ω'✌ )三✌('ω')✌三( ✌'ω')✌
インストールしてみる
環境はmacです。Node.jsは入っているとして。。
npm install -g hubot coffee-script
install終了!!
installの簡単具合がすごいなと思いました本当に。
実際にbotを作ってみます。
hubot --create <bot名>
ディレクトリ作成終了!
ディレクトリ構成は以下のようになっています。
. ├── Procfile ├── README.md ├── bin ├── external-scripts.json ├── hubot-scripts.json ├── node_modules ├── package.json └── scripts
DBを使いたい場合はRedisもinstallする必要がありますが、はじめは特段必要でなかったので使わない方向にしました。使わない時はhubot-scripts.json
からredis-brain.coffee
は外してしまってください。使うときはRedisを入れましょう。
起動してみる
./bin/hubot
Hubot> hubot help Hubot> Events: debug - {user: <user object to send message to>} Hubot <user> is a badass guitarist - assign a role to a user Hubot <user> is not a badass guitarist - remove a role from a user Hubot animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead. Hubot die - End Hubot process Hubot echo <text> - Reply back with <text> Hubot fake event <event> - Triggers the <event> event for debugging reasons Hubot help - Displays all of the help commands that Hubot knows about. Hubot help <query> - Displays all help commands that match <query>. Hubot image me <query> - The Original. Queries Google Images for <query> and returns a random top result. Hubot map me <query> - Returns a map view of the area returned by `query`. Hubot mustache me <query> - Searches Google Images for the specified query and mustaches it. Hubot mustache me <url> - Adds a mustache to the specified URL. Hubot ping - Reply with pong Hubot pug bomb N - get N pugs Hubot pug me - Receive a pug Hubot show storage - Display the contents that are persisted in the brain Hubot show users - Display all users that Hubot knows about Hubot the rules - Make sure Hubot still knows the rules. Hubot time - Reply with current time Hubot translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out. Hubot translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional Hubot who is <user> - see what roles a user has Hubot youtube me <query> - Searches YouTube for the query and returns the video embed link. ship it - Display a motivation squirrel
hubotにははじめからこのようなサンプル機能が搭載されていてぶっちゃけinstallするだけでもだいぶ楽しいですw
Hubot> hubot ping Hubot> PONG Hubot> hubot echo hoge Hubot> hoge
pingと言ったらpongと返してくれるしecho hogeと言ったら命令通りhogeと返してくれる。可愛いやつですね(?)
どう動いているのか見てみる
先ほどのhubot ping
や、hubot echo hoge
はどのように動作しているのでしょうか。実際にコードを見てみましょう。
module.exports = (robot) -> robot.respond /PING$/i, (msg) -> msg.send "PONG" robot.respond /ECHO (.*)$/i, (msg) -> msg.send msg.match[1]
これがcoffeescript...! coffeescript関連の話は今回は省略しますmm javascriptと対比しながら調べていくとだいぶわかる気がします。
robot.respond
がhubot
という呼びかけに対応していて、その後ろの正規表現と入力された引数がマッチしたらマッチした内容(msg.send "PONG"
)を実行するといったかんじですね。
msg.send
で引数に渡された値を出力します。
robot.respond
以外にも色々応答するためのバリエーションがありますので調べてみると楽しめます。
hubot/robot.coffee at master · github/hubot · GitHub とか。
次書くときはこのbotをherokuに上げてslackと接続してみます!