LoginSignup
4
0

More than 1 year has passed since last update.

AWS Lambda で作った messagingAPI を、SAM + ngrok でローカルテストする

Last updated at Posted at 2020-03-03

目的

AWS Lambda(SAM)を使用した LINEBOT 作成をしたい人が、ローカル環境で動作をテスト出来るようになる。

環境

Python 3.7.4
SAM CLI 0.37.0
line-bot-sdk 1.15.0
pipenv 2018.11.26

概要

ngrokを用いる方法。
ローカルに立てたサーバをポートフォワーディングで一時的に外部通信できるようにして、
それを messagingAPI の webhook に引っ掛ける形で動作のテストをする。

※ngrok の会員登録は不要ですが、有料会員以外は時間制限(8 時間)があって毎回 URL が変わってしまうので、この方法でやるなら日付が変わるごとに messagingAPI の設定を変える必要があります。チョットメンドウ

手順

sam local

作成したfunction をビルド
$ sam build --use-container

ビルドした function を立ち上げる。 デフォルトのポートは 3000。

$ sam local start-api --env-vars env_local.json --docker-network my-app

Mounting TestLineBotFunction at http://127.0.0.1:3000/line/reply [POST]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
yyyy-mm-dd 15:05:36  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

ngrok

ngrok をインストール
$ brew cask install ngrok

テストしたいローカルのポート番号を入れる。

$ ngrok http 3000

ngrok by @inconshreveable                                   (Ctrl+C to quit)

Session Status                online
Session Expires               7 hours, 52 minutes
Version                       2.3.35
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://hogehoge.ngrok.io -> http://localhost:3000
Forwarding                    https://hogehoge.ngrok.io -> http://localhost:3000

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

HTTP Requests
-------------

messagingAPI

LINE developers の管理画面から、messagingAPI の webhook に LINEBOT 用に切ったエンドポイントを登録する。
私の場合は LINEBOT の function をline/replyで切ったので、以下の様になる。
https://hogehoge.ngrok.io/line/reply

※「検証」ボタンは送られてくるリプライトークンが無効?らしくて上手く反応しない(しそれが割とデフォらしい)です。
実際の動作を試してみてサーバに反応があったら上手く紐づいてるな、くらいの確認でいいと思います。

動作テスト

LINE Developers の管理画面で友達登録の URL が見られるので、そこから登録する。
反応語彙等を確認。

終わったら ngrok と sam local 共に閉じて完了。


参考にさせていただいたもの

ngrok が便利すぎる - Qiita

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