Update main.py
Browse files
main.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from fastapi.middleware.cors import CORSMiddleware
|
2 |
-
from fastapi import FastAPI, Request
|
3 |
|
4 |
import json
|
5 |
import os
|
@@ -33,17 +33,12 @@ def root():
|
|
33 |
return {"title": "Echo Bot"}
|
34 |
|
35 |
@app.post("/webhook")
|
36 |
-
def webhook(request: Request):
|
37 |
-
|
38 |
-
signature = request.headers['X-Line-Signature']
|
39 |
-
# get request body as text
|
40 |
-
body = request.get_data(as_text=True)
|
41 |
-
app.logger.info("Request body: " + body)
|
42 |
-
# handle webhook body
|
43 |
try:
|
44 |
-
line_handler.handle(body,
|
45 |
except InvalidSignatureError:
|
46 |
-
|
47 |
return 'OK'
|
48 |
|
49 |
@line_handler.add(MessageEvent, message=TextMessage)
|
|
|
1 |
from fastapi.middleware.cors import CORSMiddleware
|
2 |
+
from fastapi import FastAPI, Request, Header
|
3 |
|
4 |
import json
|
5 |
import os
|
|
|
33 |
return {"title": "Echo Bot"}
|
34 |
|
35 |
@app.post("/webhook")
|
36 |
+
async def webhook(request: Request, x_line_signature: str = Header(None)):
|
37 |
+
body = await request.body()
|
|
|
|
|
|
|
|
|
|
|
38 |
try:
|
39 |
+
line_handler.handle(body.decode("utf-8"), x_line_signature)
|
40 |
except InvalidSignatureError:
|
41 |
+
raise HTTPException(status_code=400, detail="chatbot handle body error.")
|
42 |
return 'OK'
|
43 |
|
44 |
@line_handler.add(MessageEvent, message=TextMessage)
|