diff --git a/linebot/app.py b/linebot/app.py index 1474df3..9a68d53 100644 --- a/linebot/app.py +++ b/linebot/app.py @@ -7,23 +7,67 @@ # ライブラリのインポート from flask import Flask, request, abort -from linebot.models import (MessageEvent, TextMessage) -from linebot import (LineBotApi, WebhookHandler) -from linebot.exceptions import (InvalidSignatureError) -# Flaskのインスタンス -app = Flask(__name__) +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, + TextMessageContent +) import json - # config.jsonから情報を取得 with open("./config.json", "r", encoding="utf8") as file: info_config = json.load(file) -LINE_BOT_API = LineBotApi(info_config["LINE_CHANNEL_ACCESS_TOKEN"]) +configuration = Configuration(access_token=info_config["LINE_CHANNEL_ACCESS_TOKEN"]) +handler = WebhookHandler(info_config["LINE_CHANNEL_SECRET"]) -HANDLER = WebhookHandler(info_config["LINE_CHANNEL_SECRET"]) +app = Flask(__name__) SEARCH_WORD = ["お腹すいた"] +@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: + app.logger.info("Invalid signature. Please check your channel access token/channel secret.") + abort(400) + + return 'OK' + + +@handler.add(MessageEvent, message=TextMessageContent) +def handle_message(event): + 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=event.message.text)] + ) + ) + +if __name__ == "__main__": + app.run() \ No newline at end of file