alanchen1115 commited on
Commit
4a7f8d2
β€’
1 Parent(s): 04161a8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -14
main.py CHANGED
@@ -18,23 +18,34 @@ def root():
18
  @app.post("/callback")
19
  async def callback(
20
  request: Request,
21
- background_tasks: BackgroundTasks,
22
- x_line_signature=Header(None),
23
  ):
24
- body = await request.body()
25
-
 
 
 
 
26
  try:
27
- background_tasks.add_task(
28
- handler.handle, body.decode("utf-8"), x_line_signature
29
- )
30
  except InvalidSignatureError:
31
- raise HTTPException(status_code=400, detail="Invalid signature")
32
-
33
- return "ok"
34
 
35
- @handler.add(MessageEvent)
36
  def handle_message(event):
37
- if event.type != "message" or event.message.type != "text":
 
 
 
 
 
38
  return
39
- message = TextMessage(text=event.message.text)
40
- line_bot_api.reply_message(event.reply_token, message)
 
 
 
 
 
 
 
 
18
  @app.post("/callback")
19
  async def callback(
20
  request: Request,
 
 
21
  ):
22
+ # get X-Line-Signature header value
23
+ signature = request.headers['X-Line-Signature']
24
+ # get request body as text
25
+ body = request.get_data(as_text=True)
26
+ app.logger.info("Request body: " + body)
27
+ # handle webhook body
28
  try:
29
+ line_handler.handle(body, signature)
 
 
30
  except InvalidSignatureError:
31
+ abort(400)
32
+ return 'OK'
 
33
 
34
+ @line_handler.add(MessageEvent, message=TextMessage)
35
  def handle_message(event):
36
+ global working_status
37
+ if event.message.text == "再見":
38
+ working_status = True
39
+ line_bot_api.reply_message(
40
+ event.reply_token,
41
+ TextSendMessage(text="Bye!"))
42
  return
43
+
44
+ if working_status:
45
+
46
+ out = event.message.text
47
+ if not out:
48
+ out = "δΈζ‡‚ζ‚¨ηš„ζ„ζ€!"
49
+ line_bot_api.reply_message(
50
+ event.reply_token,
51
+ TextSendMessage(text=out))