Enzo886 commited on
Commit
8146db8
·
verified ·
1 Parent(s): 529c12f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +5 -1
main.py CHANGED
@@ -141,13 +141,17 @@ async def webhook(
141
  body = await request.body()
142
  try:
143
  background_tasks.add_task(
144
- line_bot_manager.line_handler.async_handle, body.decode("utf-8"), x_line_signature # 修改這裡
145
  )
146
  except InvalidSignatureError:
147
  logging.warning("收到無效的簽章")
148
  raise HTTPException(status_code=400, detail="無效的簽章")
149
  return "ok"
150
 
 
 
 
 
151
  # --- 4. 對話 Session 管理 ---
152
  class ChatSessionManager:
153
  """
 
141
  body = await request.body()
142
  try:
143
  background_tasks.add_task(
144
+ self.run_sync_handler, line_bot_manager.line_handler, body.decode("utf-8"), x_line_signature # 修改這裡
145
  )
146
  except InvalidSignatureError:
147
  logging.warning("收到無效的簽章")
148
  raise HTTPException(status_code=400, detail="無效的簽章")
149
  return "ok"
150
 
151
+ # 定義一個方法讓 line_handler 可以正確處理非同步事件
152
+ async def run_sync_handler(handler, body, signature):
153
+ await asyncio.to_thread(handler.handle, body, signature)
154
+
155
  # --- 4. 對話 Session 管理 ---
156
  class ChatSessionManager:
157
  """