実現したいこと
リクルートのA3RTのAPIを使い、LineBotでChatbotを作りたいです。 Flaskでローカルサーバーを立ち上げています。
開発環境
google colaboratory
Python3
問題
Lineのトーク画面から入力しても返答してくれない状態です。
LineBotの管理画面の検証を押すと、「ボットサーバーから200以外のHTTPステータスコードが返されました」と表示されます。
エラーメッセージ
* Serving Flask app "__main__" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Running on http://14e041c2bd1c.ngrok.io * Traffic stats available on http://127.0.0.1:4040 [2020-08-01 14:58:09,481] ERROR in app: Exception on /callback [POST] Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise raise value File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "<ipython-input-12-42e9e79c3669>", line 20, in callback handler.handle(body, signature) File "/usr/local/lib/python3.6/dist-packages/linebot/webhook.py", line 260, in handle func(event) File "<ipython-input-12-42e9e79c3669>", line 29, in handle_message line_bot_api.reply_message(event.reply_token, TextSendMessage(text=ai_message)) NameError: name 'line_bot_api' is not defined 127.0.0.1 - - [01/Aug/2020 14:58:09] "POST /callback HTTP/1.1" 500 -
Line_bot.py
python
1from flask import Flask, request, abort 2from linebot import LineBotApi, WebhookHandler 3from linebot.exceptions import InvalidSignatureError 4from linebot.models import MessageEvent, TextMessage, TextSendMessage 5import pya3rt 6from flask_ngrok import run_with_ngrok 7 8app = Flask(__name__) 9run_with_ngrok(app) 10 11linebot_api = LineBotApi('XXXXXXXXX') 12handler = WebhookHandler('YYYYYYYYYY') 13 14@app.route("/callback", methods=['POST']) 15def callback(): 16 signature = request.headers["X-Line-Signature"] 17 body = request.get_data(as_text=True) 18 19 try: 20 handler.handle(body, signature) 21 except InvalidSignatureError: 22 abort(400) 23 24 return 'OK' 25 26@handler.add(MessageEvent, message=TextMessage) 27def handle_message(event): 28 ai_message = talk_ai(event.message.text) 29 line_bot_api.reply_message(event.reply_token, TextSendMessage(text=ai_message)) 30 31 32def talk_ai(word): 33 apikey = "ZZZZZZZZZZZ" 34 client = pya3rt.TalkClient(apikey) 35 reply_message = client.talk(word) 36 return reply_message['results'][0]['reply'] 37 #print(reply_message['results'][0]['reply'])<------------(1) 38 39 40if __name__=='__main__': 41 app.run()
試したこと1
Line_bot.pyのreturnを消し、printにし、Lineのトーク画面から入力すると返答してくれます。
(Line_bot.pyの(1)部分)
python
1def talk_ai(word): 2 apikey = "DZZHjQEgh66SSzPFIkKaIXWb3rU7Ompy" 3 client = pya3rt.TalkClient(apikey) 4 reply_message = client.talk(word) 5 #return reply_message['results'][0]['reply'] 6 print(reply_message['results'][0]['reply'])
実行結果(「元気ですか?」と入力しました。)
* Serving Flask app "__main__" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Running on http://42acd2e6eea4.ngrok.io * Traffic stats available on http://127.0.0.1:4040 [2020-08-05 04:40:09,586] ERROR in app: Exception on /callback [POST] Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise raise value File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "<ipython-input-3-1cd27c4c45bc>", line 20, in callback handler.handle(body, signature) File "/usr/local/lib/python3.6/dist-packages/linebot/webhook.py", line 260, in handle func(event) File "<ipython-input-3-1cd27c4c45bc>", line 29, in handle_message line_bot_api.reply_message(event.reply_token, TextSendMessage(text=ai_message)) NameError: name 'line_bot_api' is not defined 127.0.0.1 - - [05/Aug/2020 04:40:09] "POST /callback HTTP/1.1" 500 - あなたはどうですか?
試したこと2
A3RTとFlaskで問題が起こっていないか確認しました。
TalkAPI.py
python
1import pya3rt 2 3def talk_ai(word): 4 apikey = "ZZZZZZZZZZZZ" 5 client = pya3rt.TalkClient(apikey) 6 reply_message = client.talk(word) 7 print(reply_message['results'][0]['reply']) 8 9talk_ai("好きな食べ物は?")
実行結果
何でも好きですよ
Flask.py
python
1from flask import Flask 2from flask_ngrok import run_with_ngrok 3 4app = Flask(__name__) 5run_with_ngrok(app) 6 7@app.route('/hello') 8def hello_world(): 9 return "Hello World" 10 11 12if __name__=='__main__': 13 app.run()
実行結果(http://60bc07bf2a27.ngrok.io/hello でHello Worldと出力されていました。)
Serving Flask app "__main__" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Running on http://60bc07bf2a27.ngrok.io * Traffic stats available on http://127.0.0.1:4040 127.0.0.1 - - [01/Aug/2020 12:17:04] "GET / HTTP/1.1" 404 - 127.0.0.1 - - [01/Aug/2020 12:17:04] "GET /favicon.ico HTTP/1.1" 404 - 127.0.0.1 - - [01/Aug/2020 12:17:43] "GET /hello HTTP/1.1" 200 -
pip
!pip install pya3rt !pip install line-bot-sdk !pip install flask-ngrok
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/11 08:24