diff --git a/.gitignore b/.gitignore index e9a76db..dc46fad 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ LineBotPy.code-workspace .env creds.json .DS_Store -virtEnv/* \ No newline at end of file +venv/* \ No newline at end of file diff --git a/app.py b/app.py index bb968aa..d35e641 100644 --- a/app.py +++ b/app.py @@ -5,12 +5,22 @@ from flask import Flask, request, abort -from linebot import LineBotApi, WebhookHandler -from linebot.exceptions import InvalidSignatureError -from linebot.models import ( +from linebot.v3 import ( + WebhookHandler +) +from linebot.v3.exceptions import ( + InvalidSignatureError +) +from linebot.v3.messaging import ( + Configuration, + ApiClient, + MessagingApi, + ReplyMessageRequest, + TextMessage +) +from linebot.v3.webhooks import ( MessageEvent, - TextMessage, - TextSendMessage, + TextMessageContent ) #### bot code #### @@ -30,7 +40,7 @@ print("Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.") sys.exit(1) -line_bot_api = LineBotApi(channel_access_token) +configuration = Configuration(access_token=channel_access_token) handler = WebhookHandler(channel_secret) @@ -56,23 +66,27 @@ def callback(): # Handle text messages -@handler.add(MessageEvent, message=TextMessage) +@handler.add(MessageEvent, message=TextMessageContent) def handle_message(event): # user_line_id = event.source.user_id chat_msg = event.message.text - # handle all normal bot commands response = bot.handle_cmd(chat_msg) - send_response(response, event.reply_token) - -def send_response(response, reply_token): - if response != "": - line_bot_api.reply_message(reply_token, TextSendMessage(text=response)) + if response == "": return + with ApiClient(configuration) as api_client: + line_bot_api = MessagingApi(api_client) + line_bot_api.reply_message_with_http_info( + ReplyMessageRequest( + reply_token=event.reply_token, + messages=[TextMessage(text=response)] + ) + ) + if __name__ == "__main__": - port = int(os.environ.get("PORT", 5000)) + port = int(os.environ.get("PORT", 8000)) app.run(host="0.0.0.0", port=port) diff --git a/requirements.in b/requirements.in new file mode 100644 index 0000000..4e4ce8b --- /dev/null +++ b/requirements.in @@ -0,0 +1,6 @@ +flask +requests +gunicorn +line-bot-sdk +pytz +python-dotenv \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 0c81fbd..cbaec93 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,25 +1,86 @@ -aiohttp==3.8.3 -aiosignal==1.2.0 -async-timeout==4.0.2 -attrs==22.1.0 -certifi==2022.9.24 -charset-normalizer==2.1.1 -click==8.1.3 -Flask==2.2.2 -frozenlist==1.3.1 -future==0.18.2 -gunicorn==20.1.0 -idna==3.4 -importlib-metadata==5.0.0 -itsdangerous==2.1.2 -Jinja2==3.1.2 -line-bot-sdk==2.3.0 -MarkupSafe==2.1.1 -multidict==6.0.2 -python-dotenv==0.21.0 -pytz==2022.5 -requests==2.28.1 -urllib3==1.26.12 -Werkzeug==2.2.2 -yarl==1.8.1 -zipp==3.10.0 +# +# This file is autogenerated by pip-compile with Python 3.12 +# by the following command: +# +# pip-compile +# +aenum==3.1.15 + # via line-bot-sdk +aiohttp==3.9.5 + # via line-bot-sdk +aiosignal==1.3.1 + # via aiohttp +annotated-types==0.7.0 + # via pydantic +attrs==23.2.0 + # via aiohttp +blinker==1.8.2 + # via flask +certifi==2024.6.2 + # via requests +charset-normalizer==3.3.2 + # via requests +click==8.1.7 + # via flask +deprecated==1.2.14 + # via line-bot-sdk +flask==3.0.3 + # via -r requirements.in +frozenlist==1.4.1 + # via + # aiohttp + # aiosignal +future==1.0.0 + # via line-bot-sdk +gunicorn==22.0.0 + # via -r requirements.in +idna==3.7 + # via + # requests + # yarl +itsdangerous==2.2.0 + # via flask +jinja2==3.1.4 + # via flask +line-bot-sdk==3.11.0 + # via -r requirements.in +markupsafe==2.1.5 + # via + # jinja2 + # werkzeug +multidict==6.0.5 + # via + # aiohttp + # yarl +packaging==24.1 + # via gunicorn +pydantic==2.7.4 + # via line-bot-sdk +pydantic-core==2.18.4 + # via pydantic +python-dateutil==2.9.0.post0 + # via line-bot-sdk +python-dotenv==1.0.1 + # via -r requirements.in +pytz==2024.1 + # via -r requirements.in +requests==2.31.0 + # via + # -r requirements.in + # line-bot-sdk +six==1.16.0 + # via python-dateutil +typing-extensions==4.12.2 + # via + # pydantic + # pydantic-core +urllib3==2.2.2 + # via + # line-bot-sdk + # requests +werkzeug==3.0.3 + # via flask +wrapt==1.16.0 + # via deprecated +yarl==1.9.4 + # via aiohttp diff --git a/runtime.txt b/runtime.txt index b5f092a..7f3d94a 100644 --- a/runtime.txt +++ b/runtime.txt @@ -1 +1 @@ -python-3.9.15 \ No newline at end of file +python-3.12.4 \ No newline at end of file