hubot導入めも (インストールからherokuアップロードまで)
hubot(というかbot)が面白そうだということで触ってみた。
hubotってなにさ?
「botを作るだけだったらフレームワークなんて使わんでも簡単にできるよ、twitterのbot用ツールとかあるし」
そう思った方、正にそうです。
ただhubotは様々なチャットツールに対応しています。
hubotとチャットツールをつなぐ役目を担うモジュール「Adapter」を切り替えることで、
botの処理を1回書くだけで様々なチャットツールに対応させることができます。
イメージこんなん(勉強会で使った資料から抜粋)
やること
- Node.jsインストール
- hubotインストール
- hubot起動
- adapterインストール
- herokuデプロイ
- (おまけ)Cronで動かす
ちなみに今回いろいろと調べながら導入作業をしていたのですが、
この記事が全般的にとても参考になりました。マジ感謝。gihyo.jp
Node.jsインストール
node.jsのバージョン管理をしやすくするためにNodebrewをインストールします。
$ brew update
$ brew install nodebrew
インストールの過程でパスを通してね、というメッセージが出ます。
Add path: export PATH=$HOME/.nodebrew/current/bin:$PATH To use Homebrew's directories rather than ~/.nodebrew add to your profile: export NODEBREW_ROOT=/usr/local/var/nodebrew
ということでパスを追加(bashの場合)
$ vi ~/.bash_profile
$ source ~/.bash_profile
nodebrewを最新版に更新
$ nodebrew install latest
使用するバージョンを指定する場合には以下
$ nodebrew use v4.2.1
$ node -v
v4.2.1
hubotインストール
ではではhubotをインストールしましょう( ´ ω ` )
$ sudo npm install -g hubot coffee-script
hubotを召喚します
$ 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.
どうやら「createはもう古いぜyeomanを使えよfuck」ってことらしいですqiita.com
ということでそのままコマンドを・・・叩きこむっ!
$ npm install -g yo generator-hubot $ mkdir -p myhubot $ cd myhubot $ yo hubot
召喚成功するとヤツが登場します。
_____________________________ / \ //\ | fuXX you. | ////\ _____ | What’s up, man ? | //////\ /_____\ \ / ======= |[^_/\_]| /---------------------------- | | _|___@@__|__ +===+/ /// \_\ | |_\ /// HUBOT/\\ |___/\// / \\ \ / +---+ \____/ | | | //| +===+ \// |xx| ※一部改変あり
hubot起動
hubotを起動してデフォルトで備わっているコマンドを試してみる
$ ./bin/hubot
するとhubotと対話モードになります。
コマンド一覧を表示する
myhubot>myhubot help 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 myhubot 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 <query> - Searches Google Images for the specified query and mustaches it. myhubot mustache me <url> - Adds a mustache to the specified URL. myhubot ping - Reply with pong myhubot pug bomb N - get N pugs myhubot pug me - Receive a pug myhubot the rules - Make sure myhubot 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
パグの画像を返してくれる'ああ^~こころがぴょんぴょんするんじゃあ^~'なコマンドがあるらしいので試してみる。
myhubot> myhubot pug me - Receive a pug myhubot> http://24.media.tumblr.com/tumblr_mc4u6lwZHr1qf4k86o1_500.jpg
hubotの終了はexit
myhubot>exit
hubotに何かさせたいときはscriptディレクトリ配下にXXX.coffeeを作成して処理を書きます。
※CoffeeScriptで書きます
script配下にhello.coffeeというファイルを作成し、挨拶したら爽やかに返してくれるスクリプトを書いてみましょう
module.exports = (robot) -> robot.respond /HELLO$/i, (msg) -> msg.send "fuck you"
挨拶してみる
myhubot> myhubot hello myhubot> fuck you ^ ^
前衛的な挨拶ですね。
adapterインストール
adapterというのはHubotとチャットツールをつなぐモジュールのことです。
デフォルトで提供されているものもあれば、自分で作ることもできます。
今回はtwitterのadapterを導入
$ npm install hubot-twitter --save && npm install
twitterは起動時に環境変数としてAPIキー等を渡す必要があるので、sh起動時に必要なものは設定するようにしましょう。
この記事を参考にさせていただきました。qiita.com
#!/bin/sh export HUBOT_TWITTER_KEY="自分のAPI key" export HUBOT_TWITTER_SECRET="自分のAPI secret" export HUBOT_TWITTER_TOKEN="自分のAccess token" export HUBOT_TWITTER_TOKEN_SECRET="自分のAccess token secret" export HUBOT_NAME="自分のbotの名前" bin/hubot -a twitter
'bin/hubot -a twitter'の'-a'はadapterを明示的に指定する際のオプションです。
これでローカルでhubotを起動してtwitter上で動作するようになりました。
herokuデプロイ
herokuから参照するprocfileの編集。前述と同じ理由でtwitterのadapterを指定。
bin/hubot -a twitter
ここで一旦gitにコミットしておく
$ git init $ git add . $ git commit -m "initial commit" $ git remote add origin git@XXXXXXXX
いよいよherokuにデプロイ
$ heroku login $ heroku create $ git push heroku master
shでsetしたものと同様の設定をherokuにも行ってあげます
$ heroku config:add HUBOT_TWITTER_KEY="自分のAPI key" $ heroku config:add HUBOT_TWITTER_SECRET="自分のAPI secret" $ heroku config:add HUBOT_TWITTER_TOKEN="自分のAccess token" $ heroku config:add HUBOT_TWITTER_TOKEN_SECRET="自分のAccess token secret"
(おまけ)Cronで動かす
hubotは基本的に特定の発言に対して特定の動作を行いますが
Cron使って定時起動 > 自発的に発言できるようにしたいなぁ、と。
以下を参考にしてCronで動くようにしました。qiita.com
まずCronを導入するためにpackage.jsonに以下を追記
"cron": "^1.0.5", "time": "^0.11.0"
Scriptは参考記事ほぼまんまでこんな感じで。
Cronで毎分10,20,・・・50秒のときにツイートを行うという簡単なもの。
module.exports = (robot) -> cronjob = new cronJob( cronTime: "00,10,20,30,40,50 * * * * *" start: true timeZone: "Asia/Tokyo" onTick: -> d = new Date min = d.getMinutes() sec = d.getSeconds() message = "#{sec}secなう!" robot.send {user:{user:'XXXXX'},screen_name:'XXXXX', room: 'Twitter'}, "#{sec}秒なう!" )
これで一通りの導入、動作確認は完了。
これを使って何か作ってみよう。
はてな記法使ってるんだけどshのシンタックスハイライトの効き方がとても。。。微妙です。
twitterのadapterについて
ここで導入したtwitterのadapterはStreaming APIを使用してタイムラインを探索しています。
以前私の記事で取り上げましたが、Streaming APIは全ツイートを取得できるわけではないので稀に漏れが発生することに注意ですsusunshun.hatenablog.com