-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
44 lines (31 loc) · 1.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from fastapi import FastAPI, Request
from linebot import WebhookParser, LineBotApi
from linebot.models import TextSendMessage
import os
LINE_CHANNEL_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
LINE_CHANNEL_SECRET = os.getenv("LINE_SECRET")
line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
line_parser = WebhookParser(LINE_CHANNEL_SECRET)
app = FastAPI()
@app.post('/')
async def main(request: Request):
# X-Line-Signature ヘッダーの値を取得
signature = request.headers.get('X-Line-Signature', '')
# request body から event オブジェクトを取得
events = line_parser.parse((await request.body()).decode('utf-8'), signature)
# 各イベントの処理(※1つの Webhook に複数の Webhook イベントオブジェっzクトが含まれる場合あるため)
for event in events:
if event.type != 'message':
continue
if event.message.type != 'text':
continue
# LINE パラメータの取得
line_user_id = event.source.user_id
line_message = event.message.text
# LINE メッセージの送信
line_bot_api.push_message(line_user_id, TextSendMessage(line_message))
# LINE Webhook サーバーへ HTTP レスポンスを返す
return 'ok'
@app.get("/")
async def mainget(*args, **kwargs):
return "ok"