GithubのイベントをChatworkに通知する
ChabotというChatworkが提供しているアプリを使います。
http://c-note.chatwork.com/post/69274738468/chabot
上記公式情報に説明がなかった点、修正が必要だった点をご紹介します。
Node環境整備
まずNode.jsの環境を作ります。
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash $ nvm install v5.1 $ npm install express (なくてもよいかな) $ nvm alias default v5.1.1
パスを通す
$ vi .bash_profile if [[ -s ~/.nvm/nvm.sh ]]; then source ~/.nvm/nvm.sh fi
chabotインストール
$ npm install chabot -g
不具合の修正
bots/github.js
にGithub用のプログラムが作成されますが、このままではエラーが出て動きませんでした。
module.exports = function (chabot) { // WebHook で受けたデータをセット var payload = JSON.parse(chabot.data.payload); // ChatWork API の endpoint をセット var endpoint = '/rooms/' + chabot.roomid + '/messages'; // templats/ 内のメッセージテンプレートを読み込む var template = chabot.readTemplate('github.ejs'); // WebHook で受けたデータでメッセージテンプレートを描画 var message_body = chabot.render(template, payload); // ChatWork API でメッセージ送信 chabot.client .post(endpoint, { body: message_body }) .done(function (res) { chabot.log('done'); }) .fail(function (err) { chabot.error(err); }); };
どうも、chabot.roomidに値がセットされないようです。
module.exports = function (chabot) { chabot.roomid = 123456789; // WebHook で受けたデータをセット var payload = JSON.parse(chabot.data.payload); // ChatWork API の endpoint をセット var endpoint = '/rooms/' + chabot.roomid + '/messages'; // templats/ 内のメッセージテンプレートを読み込む var template = chabot.readTemplate('github.ejs'); // WebHook で受けたデータでメッセージテンプレートを描画 var message_body = chabot.render(template, payload); // ChatWork API でメッセージ送信 chabot.client .post(endpoint, { body: message_body }) .done(function (res) { chabot.log('done'); }) .fail(function (err) { chabot.error(err); }); };
のようにroomidを書いてあげる事で解決しました。
Githubの設定
リポジトリのメニュー、
Settings > Webhooks & services > Add webhook
からアプリURLを登録しますが、Content typeを「application/json」にするとパースエラーが発生しました。
「application/x-www-form-urlencoded」とすると正常に動くのですが、NodeアプリなのでJsonで動いて欲しいところです。。
アプリの初期設定では、Pushイベントのみの対応となっています。
templates/github.ejs
IssueとPull Requestへのコメントも通知したかったので、追記したテンプレートが、
https://github.com/t-shida/chabot-github-template
になります。
Githubの設定としては、
Which events would you like to trigger this webhook? > Let me select individual events.
- Push
- Issues
- Issue comment
- Pull request review comment
にチェックを入れます。
Apacheの設定
アプリをポート8080で起動する想定で、バーチャルホストを以下のように設定
<VirtualHost *:80> ServerName chabot.domain.jp ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ ProxyPreserveHost On </VirtualHost>
ProxyPreserveHostがポイントです。これがないと動きません。
foreverインストール
$ cd ./path/to/app $ node app.js
で起動出来ますが、実際の運用ではデーモンとして動かしたいですので、foreverを利用します。
$ npm install forever -g $ forever start app.js
とします。
あまりメンテされていない印象ですが、以上の内容で正常に動作しており、メールでの確認が億劫なメンバーには好評です。