Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
68
Help us understand the problem. What is going on with this article?
@teyosh

AWS Lambda + API GatewayでLINE BOT APIを叩いてみた

More than 5 years have passed since last update.

LINE BOT API公開記念
AWS Lambdaでオウム返しBOTを作成

アカウント設定はhttps://developers.line.me から適当にやってください。

BOTを作成するとLINE_CHANNEL_ID, LINE_CHANNEL_SECRET, LINE_CHANNEL_MIDを取得できます

  • まずはAWSのマネジメントコンソールに入り[Create a Lambda function]をクリック
  • Node.jsのfunctionを選ぶ
  • Code欄を以下のコードに変更
var https = require('https');

exports.handler = function(event, context) {
    console.log('Received event:', JSON.stringify(event, null, 2));
    var msg = event.result[0];
    var data = JSON.stringify({
      to: [msg.content.from.toString()],
      toChannel: 1383378250,
      eventType: "138311608800106203",
      content: msg.content
    });
    var url ='https://trialbot-api.line.me/v1/events';
    var opts = {
        host: 'trialbot-api.line.me',
        path: '/v1/events',
        headers: {
            "Content-type": "application/json; charset=UTF-8",
            "X-Line-ChannelID": "LINE_CHANNEL_ID",
            "X-Line-ChannelSecret": "LINE_CHANNEL_SECRET",
            "X-Line-Trusted-User-With-ACL": "LINE_CHANNEL_MID"
        },
        method: 'POST'
    }
    var req = https.request(opts, function(res){
        res.on('data', function(chunk){
            console.log(chunk.toString())
        }).on('error', function(e){
            console.log('ERROR: '+ e.stack);
        })
    })
    req.write(data)
    req.end();
};
  • LINE_CHANNEL_ID, LINE_CHANNEL_SECRET, LINE_CHANNEL_MIDは適当に変更してください
  • 次にAPI endpointsに移動し、[Add API endpoint]をクリック
  • API Gatewayを選択し、もろもろ設定し、Methodは「POST」に設定
  • 追加後API endpoint URLをコピーしLINE developersのBasic InformationのCallback URLにペースト
  • 作成したBOTにメッセージを送るとLambdaのCloud watchのログに
{
    "statusCode": "427",
    "statusMessage": "Your ip address [xxx.xxx.xxx.xxx] is not allowed to access this API."
}
  • とip addressが書かれるのでこれをLINE developersのServer IP Whitelistに追加
  • 少し経つと設定されオウム返しBOTの完成

追記 2016/4/28
Whitelistを空にするとどこからでもアクセス可能となりました。
サーバーを指定したくない場合はWhitelistの設定を消してください。

つぎはBot Frameworkかな・・・

68
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
teyosh
toreta
飲食店向け予約/顧客台帳サービス「トレタ」、超直前予約アプリ「トレタnow」を開発・提供するスタートアップ企業です。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
68
Help us understand the problem. What is going on with this article?