受け取るサーバは Appcelerator の Node.ACS で無料運用。
- Appcelerator のウェブサイトでアカウントを登録
- node.js / npm を使えるように
$ npm install acs -g
で acs コマンドを使えるように$ acs new hoge
で Node.ACS プロジェクトを作成
以下の方法で payload のベタな JSON がマイチャットに送信されます。
ChatWork の API トークンやマイチャットの room_id はご自分のものをどうぞ。
var Request = require('request');
function index(req, res) {
Request.post({
uri: 'https://api.chatwork.com/v1/rooms/{マイチャットの room_id}/messages',
headers: {
'X-ChatWorkToken': 'ChatWork の API トークン'
},
form: {
body: req.body.payload
},
json: true
}, function(error, response, body){
});
}
/github 〜の一行を追加。
{
"routes":
[
{ "path": "/", "callback": "application#index" },
{ "path": "/github", "method": "post", "callback": "github#index" }
],
"filters":
[
{ "path": "/", "callback": "" }
],
"websockets":
[
{ "event": "", "callback": ""}
]
}
dependencies に request モジュールを追加。
"dependencies": {
"request": "*"
},
$ acs publish
でパブリッシュ。
2 回目以降は --force
オプションを付けること。
パブリッシュが完了すると URL( https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.cloudapp.appcelerator.com )が出力されます。
GitHub のレポジトリにある Settings → Service Hooks → WebHook URLs へ https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.cloudapp.appcelerator.com/github として追加します。
hoge/controllers/github.js に手を入れます。
var Request = require('request'),
_ = require('underscore');
function index(req, res) {
var payload = JSON.parse(req.body.payload),
body = [];
body.push('レポジトリ:');
body.push(' ' + payload.repository.name);
body.push('');
_.each(payload.commits, function(commit){
body.push('コミット:');
body.push(' ' + commit.message + '(' + commit.committer.name + ')');
if (commit.added.length > 0) {
body.push('');
body.push('追加されたファイル:');
}
_.each(commit.added, function(added){
body.push(' ' + added);
});
if (commit.removed.length > 0) {
body.push('');
body.push('削除されたファイル:');
}
_.each(commit.removed, function(removed){
body.push(' ' + removed);
});
if (commit.modified.length > 0) {
body.push('');
body.push('変更されたファイル:');
}
_.each(commit.modified, function(modified){
body.push(' ' + modified);
});
});
Request.post({
uri: 'https://api.chatwork.com/v1/rooms/{マイチャットの room_id}/messages',
headers: {
'X-ChatWorkToken': 'ChatWork の API トークン'
},
form: {
body: body.join('\n');
},
json: true
}, function(error, response, body){
});
}
hoge/package.json に手を入れます。
"dependencies": {
"request": "*",
"underscore": "*"
},