7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SORACOM LTE-M ButtonからLINEにメッセージを送る

Last updated at Posted at 2018-11-05

やったことは、LINEのAPIに登録して、SORACOM LTE-M Button powered by AWS をクリックしてSlackに通知する | Getting Started with SORACOM LTE-M Button | SORACOM Developers の、Lambdaのソースコードを下記に変えただけです。

const https = require('https');

exports.handler = (event, context, callback) => {
    console.log('Received event:', JSON.stringify(event, null, 2));

   // var text = event.stdEvent.clickType;

    var payload = {
        "to": [process.env.RECIPIENT_LINE_ID],
        "messages": [{
                "type": "text",
                "text": "こんにちは! LTE-M Buttonが押されたよ!"
            }
        ]
    }

    var body = JSON.stringify(payload);

    var req = https.request({
        hostname: "api.line.me",
        port: 443,
        path: "/v2/bot/message/multicast",
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Content-Length": Buffer.byteLength(body),
            "Authorization": "Bearer " + process.env.CHANNEL_ACCESS_TOKEN
        }
    }, function(res) {
        if (res.statusCode === 200) {
            console.log('Posted to LINE');
            callback(null, { "result": "ok" });
        }
        else {
            callback(false, { "result": "ng", "reason": 'Failed to post to LINE ' + res.statusCode });
        }
        return res;
    });
    req.write(body);
    req.end();
};

Lambdaの環境変数は、下記2点。
スクリーンショット 2018-11-05 22.42.47.png

 いずれも、LINEのチャネル基本設定から参照します。
アクセストークンは、ロングタームトークンの「再発行」を押すと発行できます。

スクリーンショット 2018-11-05 22.43.52.png スクリーンショット 2018-11-05 22.44.51.png
7
7
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
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?