import os import sys from argparse import ArgumentParser
from flask import Flask, request, abort from linebot import ( LineBotApi, WebhookParser ) from linebot.exceptions import ( InvalidSignatureError ) from linebot.models import ( MessageEvent, TextMessage, TextSendMessage, )
app = Flask(__name__)
# get channel_secret and channel_access_token from your environment variable channel_secret = os.getenv('LINE_CHANNEL_SECRET', '你的CHANNEL_SECRET') channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', '你的CHANNEL_ACCESS_TOKEN')
if channel_secret is'你的CHANNEL_SECRET': print('Specify LINE_CHANNEL_SECRET as environment variable.') sys.exit(1) if channel_access_token is'你的CHANNEL_ACCESS_TOKEN': print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.') sys.exit(1)
# if event is MessageEvent and message is TextMessage, then echo text for event in events: ifnotisinstance(event, MessageEvent): continue ifnotisinstance(event.message, TextMessage): continue
然後下面的運行部分是在 linux 環境下的指令,所以 windows 會出現找不到指令的情形,這個時候就到下載好解壓縮完的 ngrok.exe 所在資料夾,記下路徑然後在虛擬環境內新開一個 terminal (終端),將路徑導向程式所在資料夾。
cd C:\path to file
接下來輸入下面的指令。
windows
ngrok.exe http 8000
linux
./ngrok http 80
就可以看到類似以下的介面。
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online Session Expires 1 hour, 8 minutes Version 2.3.40 Region United States (us) Web Interface http://127.0.0.1:4041 Forwarding http://3327-218-166-17-89.ngrok.io -> http://localhost:8000 Forwarding https://3327-218-166-17-89.ngrok.io -> http://localhost:8000