hubotをHipChatと連携させる + Backlog, Stashから情報を取得してHipChatになげるcronjobを実行

背景

  • 会社でBacklogを使っているが、HipChatに通知を流したい
  • 会社でStashを使っているが標準のHipChat連携機能ではPull Requestをしても通知が来ないので通知するようにしたい。
  • 楽しいボットを作ってみたい

ゴール

  • HubotをHipChatにつなげる
  • HipChatに地獄のミサワの画像をランダムに表示させる
  • HubotとBacklogを連携させる。バックログの特定のカテゴリ
  • 定期的にHubotがHipChatにつぶやく
  • Hubotにshellを実行させる
  • StashのPull Requestが作られたらHubotを使ってルームに通知を送る
  • Hubotをデーモン化する

環境

インストール

公式ドキュメントを参考にしながらインストールを行う

node.jsとnpmのインストール

$ brew install node
$ node -v
v0.12.2

$ npm -v
2.11.0

hubotのgeneratorをインストールする

$ npm install -g yo generator-hubot

generatorでHubotのプロジェクトを作成する

任意のディレクトリに移動し、以下を実行
$ yo hubot

名前はwapa5powに
? Bot name: wapa5pow

HipChatと連携させたいので、adaptorはhipchatとして実行する
? Bot adapter: hipchat

実際に動かしてみる

$ ./bin/hubot

ボットの名前を忘れずに打つ
wapa5pow> wapa5pow help
wapa5pow adapter - Reply with the adapter
wapa5pow animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.
wapa5pow echo <text> - Reply back with <text>
wapa5pow help - Displays all of the help commands that wapa5pow knows about.
wapa5pow help <query> - Displays all help commands that match <query>.
wapa5pow image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
wapa5pow map me <query> - Returns a map view of the area returned by `query`.
wapa5pow mustache me <query> - Searches Google Images for the specified query and mustaches it.
wapa5pow mustache me <url> - Adds a mustache to the specified URL.
wapa5pow ping - Reply with pong
wapa5pow pug bomb N - get N pugs
wapa5pow pug me - Receive a pug
wapa5pow the rules - Make sure wapa5pow still knows the rules.
wapa5pow time - Reply with current time
wapa5pow translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.
wapa5pow translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional
wapa5pow youtube me <query> - Searches YouTube for the query and returns the video embed link.
ship it - Display a motivation squirrel

HubotをHipChatにつなげる

HipChatのHubot Adapterを参考にしながら設定する

HipChatで新しいユーザを作り、ここXMPPの情報を確認する。

以下のように実行する(アカウント、パスワードは適宜読み替える)

HipChat用の環境を設定
$ export HUBOT_HIPCHAT_JID="111111_11111111@chat.hipchat.com"
$ export HUBOT_HIPCHAT_PASSWORD="1234567890"

起動
$ bin/hubot -a hipchat

好きなチャットルームに移動して、作成した新しいBot用HipChatユーザのメンションをつけて発言する

@wapa5pow ping

そうするとPONGが帰ってくる

HipChatに地獄のミサワの画像をランダムに表示させる

Hubotをインストール、hipchatと連携し、foreverでデーモン化(CentOS6.4)を参考にしてミサワをHipChatに表示させる

定期的にHubotがHipChatにつぶやく

こちらを参考に設定できます

Hubotにshellを実行させる

こちらを参考に、以下のファイルを作成し、scripts以下に配置する。 HipChatでcalendarと呼びかけるとカレンダーを返す

child_process = require('child_process')

module.exports = (robot) ->
  robot.respond /calendar$/i, (msg) ->
    child_process.exec 'cal 2015', (error, stdout, stderr) ->
      msg.send(stdout)

HubotとBacklogを連携させる。バックログの特定のカテゴリを取得する

Backlog API 課題一覧の取得を参考にしてリクエストを組み立てそれをpythonスクリプトにしました。 HubotでCoffeeScriptでつくろうと思ったのですがpythonでつくりそれをcronで呼ぶほうが早そうだったのでそのようにしました。

StashのPull Requestが作られたらHubotを使ってルームに通知を送る

Stash APIを参考にしてpythonスクリプトで作った。

Hubotをデーモン化する

こちらを参考にする