More Related Content Similar to hubotで快適BOT生活 (20) hubotで快適BOT生活 2. 最初に
前半でhubot自体の説明をしていますが、
そんなのわかっているという方は、
後半でIRCとの連携方法や基本的なコマンドの書き方を説明しているので
そこだけ見て下さい
6. BOTとは
呼びかけに反応する
定型文を定期的に発言する
単体テストの失敗を通知する
今日の天気を調べる
IRCなどの上でこういった定形処理を
人に代わって行なってくれるソフトウェアです
7. BOTのフレームワーク
対象サービスごとに様々なフレームワークが存在します
IRCなら……
IRCbot console http://www.enjoyxstudy.com/ircbotconsole/
http://search.cpan.org/~yappo/App-
ikachan Ikachan-0.02/bin/ikachan
phenny http://inamidst.com/phenny/
とにかく沢山あります。それらと比べてhubotは?
8. BOTのフレームワーク
サービス BOTフレームワーク コマンド
呼びかけに反応する
Twitter twittbot
定期的につぶやく
テストの結果を表示
ikachan
今日の天気を表示
IRC
プラットフォームごとに様々なBOT作成フレームワークがあります
twittbot http://twittbot.net/
9. BOTのフレームワーク
サービス BOTフレームワーク コマンド
呼びかけに反応する
Twitter twittbot
定期的につぶやく
テストの結果を表示
ikachan
今日の天気を表示
IRC
例えばtwittbotはTwitterと結合しているのでIRCのBOTは作れません
10. BOTのフレームワーク
サービス BOTフレームワーク コマンド
呼びかけに反応する
Twitter twittbot
定期的につぶやく
テストの結果を表示
ikachan
今日の天気を表示
IRC
それぞれのフレームワークは別の仕組みで動いているので、
一方に向けて作ったコマンドをもう一方で動かすこともできません
11. hubotを使うとどうなるか
サービス Adapter Scripts
呼びかけに反応する
hubot-twitter h
Twitter u 定期的につぶやく
b
o
テストの結果を表示
hubot-irc t
今日の天気を表示
IRC
プラットフォームとの接続部分とコマンドを本体から切り離す
ことで多くのプラットフォームに対応可能となっています
12. 多様なサービスに対応
IRC
Twitter
Google Talk
Skype
加えて、新しいAdapterを作るのも簡単です
16. 簡単に動かす方法を説明します、が
hubotの動作には、次の3点が必要です
Node.js
CoffeeScript
Redis
カンタン
インストール方法はWebで
http://blog.fumiz.me/2012/07/26/hubot-irc-bot/
21. 手順3 コマンドを入れる
本体同梱のコマンドをカレントディレクトリにコピー
cp -R node_modules/hubot/src/scripts ./
今度こそ動きました
www12395ue:miff% node_modules/hubot/bin/hubot
path.exists is now called `fs.exists`.
Hubot> hubot ping
Hubot> PONG
Hubot> hubot die
Hubot> Goodbye, cruel world.
Hubot> %
22. % tree -L 2
.
¦-- node_modules
¦ `-- hubot
¦-- package.json
`-- scripts
¦-- google-images.coffee
¦-- help.coffee
¦-- httpd.coffee
¦-- maps.coffee
¦-- math.coffee
¦-- ping.coffee
¦-- pugme.coffee
¦-- roles.coffee
¦-- rules.coffee
¦-- storage.coffee
¦-- translate.coffee
`-- youtube.coffee
実行時のカレントディレクトリ下にあるscriptsディレクトリに
入っているスクリプトが起動時に実行される仕組み
25. プラットフォーム Adapter Scripts
呼びかけに反応する
hubot-twitter h
Twitter u 定期的につぶやく
b
o
テストの結果を表示
hubot-irc t
今日の天気を表示
IRC
素の状態にAdapterを追加することでIRCに接続できます
27. 1. インストール
パッケージをダウンロードしてモジュールをインストール
% git clone git://github.com/fumiz/hubot-ircbot-example.git
% cd hubot-ircbot-example
% npm install
32. 基本的な書き方
hello.coffee
module.exports = (robot) ->
robot.respond /who are you/i, (msg) ->
msg.send "I'm hubot!"
robot.hear /HELLO$/i, (msg) ->
msg.send "hello!"
robot.respond /who am I/i, (msg) ->
msg.send "You are #{msg.message.user.name}"
robot.respond /what is this (.*)/i, (msg) ->
msg.send "This is #{msg.match[1]}"
34. msgオブジェクト
robot.respond /who am I/i, (msg) ->
msg.send "You are #{msg.message.user.name}"
msgオブジェクトには、関数が呼び出された時の発言に関する
情報が入っています
msg.send関数によって発言が行われたチャンネルに対して
BOTに発言させることができます
35. 正規表現とキャプチャ
robot.respond /what is this (.*)/i, (msg) ->
msg.send "This is #{msg.match[1]}"
第一引数に渡す正規表現にマッチした発言が行われた場合に
第二引数に渡す関数が実行されます
正規表現でキャプチャした文字列は、msgオブジェクトから
取り出せます
39. 定期実行 cron
cron. coffee
cronJob = require('cron').CronJob
module.exports = (robot) ->
send = (room, msg) ->
response = new robot.Response(robot, {user : {id : -1, name : room}, text : "none", done : false}, [])
response.send msg
# *(sec) *(min) *(hour) *(day) *(month) *(day of the week)
new cronJob('0 0 * * * *', () ->
currentTime = new Date
send '#your-channel-name', "current time is #{new Date().currentTime.getHours()}:00."
).start()
cronモジュールを使うことで処理の定期実行が可能です
処理タイミングの指定方法は、通常のcronと同じです
cronモジュール https://github.com/ncb000gt/node-cron/
参考 http://d.hatena.ne.jp/anatoo/20120204/1328368042
40. WEB API http
twitter. coffee
module.exports = (robot) ->
robot.respond /twitter (.*)/i, (msg) ->
keyword = encodeURIComponent msg.match[1]
request = msg.http('http://search.twitter.com/search.json')
.query(q: keyword)
.get()
request (err, res, body) ->
json = JSON.parse body
msg.send json.results[0].text if json.results.length > 0
msgオブジェクトのhttpメソッドを使うとHTTP通信できます
bodyに取得結果のテキストが入っているのでそのまま使えます
※)ただし、レスポンスがUTF-8の場合に限る
httpで取得できるオブジェクトの実体
https://github.com/technoweenie/node-scoped-http-client
例で検索に引っかかったツイート https://twitter.com/deep_hoge/status/231719844532350977
41. スクレイピング request
title. coffee
request = require 'request'
cheerio = require 'cheerio'
module.exports = (robot) ->
robot.respond /title (.*)/i, (msg) ->
url = msg.match[1]
options =
url: url
timeout: 2000
headers: {'user-agent': 'node title fetcher'}
request options, (error, response, body) ->
$ = cheerio.load body
title = $('title').text().replace(/n/g, '')
msg.send(title)
msg.httpよりも高性能なrequestを使う方がリダイレクト時の処
理などが入っており実用的
cheerioを使うとjQueryと同じ感覚で要素を取得でき便利
デフォルトではUTF-8しか処理できない
色んなエンコーディングも読むなら→ http://blog.fumiz.me/2012/07/28/node-js-scraping-with-multibyte-characters/
42. 記憶 robot.brain
plus.coffee
module.exports = (robot) ->
robot.hear /^(.+)++$/i, (msg) ->
user = msg.match[1]
if not robot.brain.data[user]
robot.brain.data[user] = 0
robot.brain.data[user]++
robot.brain.save()
msg.send robot.brain.data[user]
robot.brain.dataにデータを保存しておけます
robot.brain.saveで保存したデータを永続化できる筈が、できま
せんでした今のところ原因は不明です
43. HTTPD
module.exports = (robot) ->
robot.router.get "/version", (req, res) ->
res.end robot.version
robot.routerを使うことでhttp経由での呼び出しを受付可能
ブラウザや他のツールと組み合わせることで色々できそう
hubot本体同梱のサンプル
https://github.com/github/hubot/blob/master/src/scripts/httpd.coffee
47. というわけで
BOTにnoticeで発言させたければ、自分でパッチを当てましょう
https://github.com/fumiz/hubot-irc/commit/
a70f95bf0b784b2f402d86dc87258938fed38926
module.exports = (robot) ->
robot.hear /hi$/i, (msg) ->
msg.notice "hi"
49. hubotはBOTのフレームワークであり、
hubot-ircを併用することでIRCのBOTとして活用できます。
BOTとしての動作はJavaScriptもしくはCoffeeScriptで簡単に拡張でき
Node.jsの持つ豊富なモジュール群を活用して仕事を効率化できます。
50. 参考文献
hubot本家 http://hubot.github.com/
hubot-irc https://github.com/nandub/hubot-irc/
github社製ボットフレームワーク、hubotを
http://d.hatena.ne.jp/anatoo/20120204/1328368042
IRCボットとして導入した話(修正あり
hubot irc を使うHow To http://qiita.com/items/c117b64ac3f7aeab3389
hubotを使ってircのルームに http://chobie.hatenablog.com/entry/
しゃべらせてみる 2012/02/26/125532
http://theprogrammingbutler.com/blog/archives/
hubot Scripts Explained
2011/10/28/hubot-scripts-explained/
Editor's Notes \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n