Windows + python
$ sudo mv <檔案目前的位置> /usr/bin/
$ ngrok
$ ngrok http 5000
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_CHANNEL_SECRET')
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text))
if __name__ == "__main__":
app.run()
if __name__ == "__main__":
app.run(host='0.0.0.0',port=9000, debug=True)
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
[2017-07-04 14:48:33,233] ERROR in app: Exception on /callback [POST]
Traceback (most recent call last):
File "C:\Anaconda2\lib\site-packages\flask\app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "C:\Anaconda2\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Anaconda2\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Anaconda2\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Anaconda2\lib\site-packages\flask\app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "<ipython-input-1-c3bc0381a4f5>", line 33, in callback
handler.handle(body, signature)
File "C:\Anaconda2\lib\site-packages\linebot\webhook.py", line 227, in handle
func(event)
File "<ipython-input-1-c3bc0381a4f5>", line 44, in handle_message
TextSendMessage(text=event.message.text))
File "C:\Anaconda2\lib\site-packages\linebot\api.py", line 94, in reply_message
'/v2/bot/message/reply', data=json.dumps(data), timeout=timeout
File "C:\Anaconda2\lib\site-packages\linebot\api.py", line 262, in _post
self.__check_error(response)
File "C:\Anaconda2\lib\site-packages\linebot\api.py", line 271, in __check_error
raise LineBotApiError(response.status_code, error)
LineBotApiError: <LineBotApiError [Invalid reply token]>
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
msg = event.message.text
print(msg)
msg = msg.encode('utf-8')
if msg=="DOG":
line_bot_api.reply_message(event.reply_token,TextSendMessage(text="汪汪叫"))
if msg=="ian":
line_bot_api.reply_message(event.reply_token,TextSendMessage(text="甚麼"))
if msg=="lillian_hong":
line_bot_api.reply_message(event.reply_token,TextSendMessage(text="So cute"))
else:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=event.message.text))
$ sudo apt-get --purge remove ngrok-client