alanchen1115 commited on
Commit
34287b9
·
verified ·
1 Parent(s): e85279a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +10 -6
main.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi.middleware.cors import CORSMiddleware
2
- from fastapi import FastAPI, Request, Header, HTTPException, status
3
 
4
  import json
5
  import os
@@ -33,14 +33,18 @@ def root():
33
  return {"title": "Echo Bot"}
34
 
35
  @app.post("/webhook")
36
- async def webhook(request: Request):
37
- signature = request.headers['X-Line-Signature']
 
 
 
38
  body = await request.body()
39
  try:
40
- line_handler.handle(body.decode(),signature)
 
 
41
  except InvalidSignatureError:
42
- raise HTTPException(status_code=400, detail="SignatureError")
43
-
44
  return "ok"
45
 
46
  @line_handler.add(MessageEvent, message=TextMessage)
 
1
  from fastapi.middleware.cors import CORSMiddleware
2
+ from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
3
 
4
  import json
5
  import os
 
33
  return {"title": "Echo Bot"}
34
 
35
  @app.post("/webhook")
36
+ async def webhook(
37
+ request: Request,
38
+ background_tasks: BackgroundTasks,
39
+ x_line_signature=Header(None),
40
+ ):
41
  body = await request.body()
42
  try:
43
+ background_tasks.add_task(
44
+ line_handler.handle, body.decode("utf-8"), x_line_signature
45
+ )
46
  except InvalidSignatureError:
47
+ raise HTTPException(status_code=400, detail="Invalid signature")
 
48
  return "ok"
49
 
50
  @line_handler.add(MessageEvent, message=TextMessage)