LoginSignup
7
5

More than 5 years have passed since last update.

AWS Lambdaを利用してChatWorkにメッセージ送信

Last updated at Posted at 2017-04-10

環境

AWS Lambda

  • Node.js 6.10
  • Lambdaの実行権限だけつけて、No VPC環境で実行(単にめんどくさかった。)

ChatWorkでやること

この辺はChatWorkのドキュメント見てください・・・

実装

外部モジュールは使わない。

index.handler
var https = require ('https');
exports.handler = function(event, context) {
    var body = 'body=' + event.message;

    var options = {
        host: 'api.chatwork.com',
        path: '/v2/rooms/'+ event.room_id +'/messages',
        method: 'POST',
        headers: {
            'X-ChatWorkToken': process.env.TOKEN,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': body.length
        }
    };


    var req = https.request(options, function (res) {
        res.on('data', function (chunk) {
            // console.log('--- on  ' + (+new Date() * 0.0001) + ' ---');
            // console.log(chunk.toString());
        }).on('error', function (e) {
            // console.log('--- err ' + (+new Date() * 0.0001) + ' ---');
            // console.log('ERROR:' + e.stack);
        });
    });

    // console.log('--- req ' + (+new Date() * 0.0001) + ' ---');
    req.write(body);

    // console.log('--- end ' + (+new Date() * 0.0001) + ' ---');
    req.end();
};

event

event
{
    "room_id" : "{ChatWorkの送信対象のチャンネル}",
    "message" : "投稿するメッセージ"
}

Lambda環境変数

  • TOKEN
    • 自分のAPIキー

TODO

  • API Gateway経由してWebhookをする。
  • Jenkinsから通知
  • GitHubから通知
  • JIRAから通知
  • 日時レポート的なやつの発行

経緯

SlackからChatWorkを使わざるをえなくなったためWebhookを移行するために泣く泣く調べてる最中なう。
ということで、サーバレスにしようということでAWS Lambdaで環境構築メモを残しておけば誰かの役に立つかもしれないと(いないか。。。)

参考

Tattin's App History - node.jsでhttpのPOSTリクエスト

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5