前提・実現したいこと
yahooからトレンドやニュースをスクレイピングして、lineからきた文言に合わせて、スクレイピングした結果を
返すというlinebotを作っています。
(例えば「トレンド」と打ったらトレンドページをスクレイピングして返す。).
発生している問題・エラーメッセージ
herokuに書いたコードをpushして成功したので、メッセージが返ってくるかlineでテストしたのですが、既読スルーされてしまいます。そこで「heroku logs -t」でログを確認。
logs
1 22021-04-11T04:06:50.688436+00:00 heroku[web.1]: Process exited with status 3 32021-04-11T04:06:50.760627+00:00 heroku[web.1]: State changed from up to crashed 42021-04-11T04:08:11.517400+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/callback" host=aiinfobot.herokuapp.com request_id=9bc4e61b-8c84-4038-a6ea-e92b0f5b8396 fwd="147.92.150.195" dyno= connect= service= status=503 bytes= protocol=https 52021-04-11T04:08:19.875543+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/callback" host=aiinfobot.herokuapp.com request_id=dcb662ed-ae6e-4c20-ba57-74a285bf6cc3 fwd="147.92.150.195" dyno= connect= service= status=503 bytes= protocol=https 62021-04-11T04:09:04.830300+00:00 heroku[web.1]: State changed from crashed to down 72021-04-11T04:09:04.756681+00:00 app[api]: Scaled to web@0:Free by user daqingsangyuan567@gmail.com 82021-04-11T04:09:11.456579+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/callback" host=aiinfobot.herokuapp.com request_id=7e670a6d-dd8c-4f8f-a87d-15c22933eecb fwd="147.92.150.195" dyno= connect= service= status=503 bytes= protocol=https 92021-04-11T04:09:18.464202+00:00 app[api]: Scaled to web@1:Free by user daqingsangyuan567@gmail.com 102021-04-11T04:09:26.719234+00:00 heroku[web.1]: Starting process with command `gunicorn app:app` 112021-04-11T04:09:30.598681+00:00 app[web.1]: [2021-04-11 04:09:30 +0000] [4] [INFO] Starting gunicorn 20.1.0 122021-04-11T04:09:30.600445+00:00 app[web.1]: [2021-04-11 04:09:30 +0000] [4] [INFO] Listening at: http://0.0.0.0:15142 (4) 132021-04-11T04:09:30.600616+00:00 app[web.1]: [2021-04-11 04:09:30 +0000] [4] [INFO] Using worker: sync2021-04-11T04:09:30.610160+00:00 app[web.1]: [2021-04-11 04:09:30 +0000] [8] [INFO] Booting worker with pid: 8 142021-04-11T04:09:30.675158+00:00 app[web.1]: [2021-04-11 04:09:30 +0000] [9] [INFO] Booting worker with pid: 9 152021-04-11T04:09:31.170257+00:00 heroku[web.1]: State changed from starting to up 162021-04-11T04:09:32.110789+00:00 app[web.1]: [2021-04-11 04:09:32 +0000] [8] [ERROR] Exception in worker process 172021-04-11T04:09:32.110811+00:00 app[web.1]: Traceback (most recent call last): 182021-04-11T04:09:32.110812+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker 192021-04-11T04:09:32.110813+00:00 app[web.1]: worker.init_process() 202021-04-11T04:09:32.110813+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process 212021-04-11T04:09:32.110814+00:00 app[web.1]: self.load_wsgi() 222021-04-11T04:09:32.110814+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi 232021-04-11T04:09:32.110815+00:00 app[web.1]: self.wsgi = self.app.wsgi() 242021-04-11T04:09:32.110815+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi 252021-04-11T04:09:32.110815+00:00 app[web.1]: self.callable = self.load() 262021-04-11T04:09:32.110816+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load 272021-04-11T04:09:32.110816+00:00 app[web.1]: return self.load_wsgiapp() 282021-04-11T04:09:32.110817+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp 292021-04-11T04:09:32.110817+00:00 app[web.1]: return util.import_app(self.app_uri) 302021-04-11T04:09:32.110817+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app 312021-04-11T04:09:32.110818+00:00 app[web.1]: mod = importlib.import_module(module) 322021-04-11T04:09:32.110818+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module 332021-04-11T04:09:32.110819+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level) 342021-04-11T04:09:32.110819+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import 352021-04-11T04:09:32.110819+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load 362021-04-11T04:09:32.110820+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked 372021-04-11T04:09:32.110820+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked 382021-04-11T04:09:32.110820+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module 392021-04-11T04:09:32.110821+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed 402021-04-11T04:09:32.110821+00:00 app[web.1]: File "/app/app.py", line 51, in <module> 412021-04-11T04:09:32.110821+00:00 app[web.1]: @handler.add(MessageEvent, message=TextMessage) 422021-04-11T04:09:32.110822+00:00 app[web.1]: NameError: name 'TextMessage' is not defined 432021-04-11T04:09:32.111076+00:00 app[web.1]: [2021-04-11 04:09:32 +0000] [8] [INFO] Worker exiting (pid: 8) 442021-04-11T04:09:32.319386+00:00 app[web.1]: [2021-04-11 04:09:32 +0000] [9] [ERROR] Exception in worker process 452021-04-11T04:09:32.319389+00:00 app[web.1]: Traceback (most recent call last): 462021-04-11T04:09:32.319389+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker 47
NameErrorと503, H10エラーが出ていて、これの解決策が調べてもなかなか見つからず困っています。
該当のソースコード
app.py
python
1from flask import Flask, request, abort 2from linebot import (LineBotApi, WebhookHandler) 3from linebot.exceptions import (InvalidSignatureError) 4from linebot.models import (MessageEvent, TextSendMessage, ImageMessage, ImageSendMessage) 5import os 6import sys 7import json 8import urllib.request 9import trend as tr 10import kokunai as kn 11import kokusai as ks 12import keizai as ke 13import entame as en 14import sports as sp 15import it 16import science as sc 17import social as so 18 19app = Flask(__name__) 20 21channel_secret = os.getenv('LINE_CHANNEL_SECRET', None) 22channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None) 23 24if channel_secret is None: 25 print('Specify LINE_CHANNEL_SECRET as environment variable.') 26 sys.exit(1) 27if channel_access_token is None: 28 print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.') 29 sys.exit(1) 30 31line_bot_api = LineBotApi(channel_access_token) 32handler = WebhookHandler(channel_secret) 33 34 35@app.route("/callback", methods=['POST']) 36def callback(): 37 signature = request.headers['X-Line-Signature'] 38 39 # get request body as text 40 body = request.get_data(as_text=True) 41 app.logger.info("Request body: " + body) 42 43 # parse webhook body 44 try: 45 events = handler.handle(body, signature) 46 except InvalidSignatureError: 47 abort(400) 48 49 return 'ok' 50 51@handler.add(MessageEvent, message=TextMessage) 52def handle_text_message(event): 53 word = event.message.text 54 if word == "トレンド": 55 trresult = tr.gettrend(word) 56 line_bot_api.reply_message( 57 event.reply_token, 58 TextSendMessage(text=trresult) 59 ) 60 elif word == "国内": 61 knresult = kn.getkokunai(word) 62 line_bot_api.reply_message( 63 event.reply_token, 64 TextSendMessage(text=knresult) 65 ) 66 67 elif word == "国際": 68 ksresult = ks.getkokusai(word) 69 line_bot_api.reply_message( 70 event.reply_token, 71 TextSendMessage(text=ksresult) 72 ) 73 74 elif word == "経済": 75 keresult = ke.getkeizai(word) 76 line_bot_api.reply_message( 77 event.reply_token, 78 TextSendMessage(text=keresult) 79 ) 80 81 elif word == "エンタメ": 82 enresult = en.getentame(word) 83 line_bot_api.reply_message( 84 event.reply_token, 85 TextSendMessage(text=enresult) 86 ) 87 88 elif word == "スポーツ": 89 spresult = sp.getsports(word) 90 line_bot_api.reply_message( 91 event.reply_token, 92 TextSendMessage(text=spresult) 93 ) 94 95 elif word == "it": 96 itresult = it.getit(word) 97 line_bot_api.reply_message( 98 event.reply_token, 99 TextSendMessage(text=itresult) 100 ) 101 102 elif word == "科学": 103 scresult = sc.getscience(word) 104 line_bot_api.reply_message( 105 event.reply_token, 106 TextSendMessage(text=scresult) 107 ) 108 109 elif word == "地域": 110 soresult = so.getsocial(word) 111 line_bot_api.reply_message( 112 event.reply_token, 113 TextSendMessage(text=soresult) 114 ) 115 116 else: 117 line_bot_api.reply_message( 118 event.reply_token, 119 TextSendMessage(text=word) 120 ) 121 122if __name__ == "__main__": 123 port = int(os.getenv("PORT", 5000)) 124 app.run(host="0.0.0.0", port=port, debug=True)
Procfile
1web: gunicorn app:app
requirements.txt
line_bot_sdk==1.19.0 beautifulsoup4==4.9.3 Flask==1.1.2 gunicorn==20.1.0
runtime.txt
python-3.8.5
試したこと
・pip freeze > requirements.txtで作成していたが、pipenvからpipfileを作ってデプロイ
→エラーは変わらず。
・https://qiita.com/osyo_yn/items/2a626fda336d74382bdc ←
このサイトを参考に自分がやっていないこと(pipreqsとheroku ps:scale web=1)を実行
→変わらず。
かなり調べましたが解決策が全く分からないため質問させていただきました。有識者の方、どうかご教授お願いいたします。
補足情報(FW/ツールのバージョンなど)
Ubuntu20.04(wsl2)
Python3.8.5
pythonは2か月ほど勉強しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/15 12:32