Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

hubotで快適BOT生活

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Chargement dans…3
×

Consultez-les par la suite

1 sur 51 Publicité

hubotで快適BOT生活

Télécharger pour lire hors ligne

hubotの導入方法をインストール、スクリプトの書き方やサンプルコードを交えて解説します。元となった記事はこちらのURLから http://blog.fumiz.me/2012/08/05/hubot-matome/

hubotの導入方法をインストール、スクリプトの書き方やサンプルコードを交えて解説します。元となった記事はこちらのURLから http://blog.fumiz.me/2012/08/05/hubot-matome/

Publicité
Publicité

Plus De Contenu Connexe

Diaporamas pour vous (20)

Similaire à hubotで快適BOT生活 (20)

Publicité

hubotで快適BOT生活

  1. 1. hubotで快適BOT生活 hubotの導入とIRCでの活用方法のまとめ miff http://blog.fumiz.me/
  2. 2. 最初に 前半でhubot自体の説明をしていますが、 そんなのわかっているという方は、 後半でIRCとの連携方法や基本的なコマンドの書き方を説明しているので そこだけ見て下さい
  3. 3. 目次 第一章 hubotとは何か 第二章 hubotのインストールと起動 第三章 hubotとIRCの接続 第四章 スクリプトの書き方 第五章 具体的なスクリプトの例 おまけ noticeでメッセージを送る 最後に
  4. 4. 第一章 hubotとは何か
  5. 5. hubotとは github社が作ったBOTのフレームワーク http://hubot.github.com/
  6. 6. BOTとは 呼びかけに反応する 定型文を定期的に発言する 単体テストの失敗を通知する 今日の天気を調べる IRCなどの上でこういった定形処理を 人に代わって行なってくれるソフトウェアです
  7. 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. 8. BOTのフレームワーク サービス BOTフレームワーク コマンド 呼びかけに反応する Twitter twittbot 定期的につぶやく テストの結果を表示 ikachan 今日の天気を表示 IRC プラットフォームごとに様々なBOT作成フレームワークがあります twittbot http://twittbot.net/
  9. 9. BOTのフレームワーク サービス BOTフレームワーク コマンド 呼びかけに反応する Twitter twittbot 定期的につぶやく テストの結果を表示 ikachan 今日の天気を表示 IRC 例えばtwittbotはTwitterと結合しているのでIRCのBOTは作れません
  10. 10. BOTのフレームワーク サービス BOTフレームワーク コマンド 呼びかけに反応する Twitter twittbot 定期的につぶやく テストの結果を表示 ikachan 今日の天気を表示 IRC それぞれのフレームワークは別の仕組みで動いているので、 一方に向けて作ったコマンドをもう一方で動かすこともできません
  11. 11. hubotを使うとどうなるか サービス Adapter Scripts 呼びかけに反応する hubot-twitter h Twitter u 定期的につぶやく b o テストの結果を表示 hubot-irc t 今日の天気を表示 IRC プラットフォームとの接続部分とコマンドを本体から切り離す ことで多くのプラットフォームに対応可能となっています
  12. 12. 多様なサービスに対応 IRC Twitter Google Talk Skype 加えて、新しいAdapterを作るのも簡単です
  13. 13. hubotは 自由に拡張できる 簡単に使える 色々なサービスに対応可能 そんなBOTのフレームワークです
  14. 14. 第二章 hubotのインストールと起動
  15. 15. 公式サイトを見ても 使い方がよくわからない
  16. 16. 簡単に動かす方法を説明します、が hubotの動作には、次の3点が必要です Node.js CoffeeScript Redis カンタン インストール方法はWebで http://blog.fumiz.me/2012/07/26/hubot-irc-bot/
  17. 17. 手順1 インストール どこかのディレクトリにpackage.jsonを作る { "name": "hubot-sample", "version": "0.0.1", "dependencies": { "hubot": "2.3.0" } } 必要なモジュールをインストールする % npm install
  18. 18. ディレクトリ構成はこんな感じになっている筈 % tree -L 2 . ¦-- node_modules ¦ `-- hubot `-- package.json
  19. 19. 手順2 実行 hubotコマンドを叩く % node_modules/hubot/bin/hubot すると…… www12395ue:miff% node_modules/hubot/bin/hubot path.exists is now called `fs.exists`. Hubot> 無事、動きました
  20. 20. しかし、このままではコマンドを使えません www12395ue:miff% node_modules/hubot/bin/hubot path.exists is now called `fs.exists`. Hubot> hubot ping Hubot> hubot help Hubot> hubot die Hubot> ( ゚д゚ )
  21. 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. 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ディレクトリに 入っているスクリプトが起動時に実行される仕組み
  23. 23. TIPS 起動時にこういうエラーが出る時はポートが既に使われています Hubot> [Wed Aug 01 2012 02:18:37 GMT+0900 (JST)] ERROR Error: listen EADDRINUSE 使ってないポート番号を指定することで回避できます % export PORT=9999 hubotでは、設定を環境変数で指定します
  24. 24. 第三章 hubotとIRCの接続
  25. 25. プラットフォーム Adapter Scripts 呼びかけに反応する hubot-twitter h Twitter u 定期的につぶやく b o テストの結果を表示 hubot-irc t 今日の天気を表示 IRC 素の状態にAdapterを追加することでIRCに接続できます
  26. 26. hubotとhubot-irc、サンプルスクリプトを まとめたパッケージを作りました https://github.com/fumiz/hubot-ircbot-example
  27. 27. 1. インストール パッケージをダウンロードしてモジュールをインストール % git clone git://github.com/fumiz/hubot-ircbot-example.git % cd hubot-ircbot-example % npm install
  28. 28. 2. 設定 同梱のrunhubot.shを編集してIRC接続用の設定を書きます BOTの呼び名
  29. 29. 3. 実行 % ./runhubot.sh 無事、IRCに接続できました
  30. 30. 第四章 スクリプトの書き方
  31. 31. JavaScriptでもOK scriptsディレクトリ下に.jsか.coffeeを置く
  32. 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]}"
  33. 33. respondとhear robot.respond /who are you/i, (msg) -> msg.send "I'm hubot!" robot.hear /HELLO$/i, (msg) -> msg.send "hello!" respond「呼ばれて答える」 hear「チャンネル上の発言に反応する」
  34. 34. msgオブジェクト robot.respond /who am I/i, (msg) -> msg.send "You are #{msg.message.user.name}" msgオブジェクトには、関数が呼び出された時の発言に関する 情報が入っています msg.send関数によって発言が行われたチャンネルに対して BOTに発言させることができます
  35. 35. 正規表現とキャプチャ robot.respond /what is this (.*)/i, (msg) -> msg.send "This is #{msg.match[1]}" 第一引数に渡す正規表現にマッチした発言が行われた場合に 第二引数に渡す関数が実行されます 正規表現でキャプチャした文字列は、msgオブジェクトから 取り出せます
  36. 36. 基本的な書き方については以上です 後はnode.jsの豊富なライブラリを組み合わせることで、 大抵の処理をBOTに行わせることができる筈です
  37. 37. 第五章 具体的なスクリプトの例
  38. 38. ここで紹介するサンプルは、hubot+hubot-irc導入パッケージの scriptsディレクトリに入っています https://github.com/fumiz/hubot-ircbot-example cron.coffeeのみ、メッセージの送信先チャンネルを 手動で設定する必要があるので注意です
  39. 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. 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. 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. 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. 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
  44. 44. もっと沢山のサンプルが、hubot-scriptsにはあります https://github.com/github/hubot-scripts
  45. 45. おまけ
  46. 46. 標準のhubot/hubot-ircでは、 BOTにnoticeで発言させることができません。 しかし、BOTにnoticeで発言させたいこともありますよね?
  47. 47. というわけで BOTにnoticeで発言させたければ、自分でパッチを当てましょう https://github.com/fumiz/hubot-irc/commit/ a70f95bf0b784b2f402d86dc87258938fed38926 module.exports = (robot) -> robot.hear /hi$/i, (msg) -> msg.notice "hi"
  48. 48. 最後に
  49. 49. hubotはBOTのフレームワークであり、 hubot-ircを併用することでIRCのBOTとして活用できます。 BOTとしての動作はJavaScriptもしくはCoffeeScriptで簡単に拡張でき Node.jsの持つ豊富なモジュール群を活用して仕事を効率化できます。
  50. 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/
  51. 51. 以上で、本スライドは終了です。 知りたいことは書いてあったでしょうか? 読んで頂きありがとうございました。 あなたが良いBOTライフを送ることを祈ります。

Notes de l'éditeur

  • \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

×