Skip to content

Commit

Permalink
update: モジュールの更新
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyHuman00 committed Jul 20, 2024
1 parent 948d8a9 commit 833da6a
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions linebot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 833da6a

Please sign in to comment.