LoginSignup
0
0

More than 1 year has passed since last update.

[Node] AWS Lambda標準ライブラリでPOST

Last updated at Posted at 2021-05-29

こんにちは takeです。
AWS LambdaでSlack botを作成しました。

参考にさせていただいた記事

お急ぎの方用

const https = require('https');
const querystring = require('querystring');
.
.
.
var post_data = querystring.stringify({channel:'',text:}, null);
    let options = {
        host: 'slack.com',
        path: '/api/chat.postMessage',
        port: 443,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
//他にtokenなど
        },
        method: 'POST',
    };
    var post_req = https.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
          console.log('Response: ' + chunk);
          context.succeed();
        });
        res.on('error', function (e) {
        console.log("Got error: " + e.message);
        context.done(null, 'FAILURE');
        });
     });
    post_req.write(post_data);
    post_req.end();

今回のPOST先

詳細

今回AWS Lambda上で定期的に動作するSlackBotを作成しました。

Slackにメッセージを送るためにはSlack APIへPOSTしなければならなかったのですが、requestを使ってPOSTをしようとすると

Task timed out after 3.00 seconds

とタイムアウトしてしまうように

原因を探ってみるとどうやらrequestを使用するためにはlocalでnpm installを行ってnode_moduleをzipファイルにしてアップロードする必要があるようです。

面倒ですよね、、

なんとか標準ライブラリを使用してブラウザでコードを書いてLambdaからPOSTしようと思い今回調べてみました。

0
0
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
0
0