Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import threading
|
|
|
3 |
import uvicorn
|
4 |
from fastapi import FastAPI, Request, HTTPException, Header
|
5 |
from linebot import LineBotApi, WebhookHandler
|
@@ -7,7 +8,11 @@ from linebot.exceptions import InvalidSignatureError
|
|
7 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
8 |
from model import classify_esi # นำเข้าโมเดลสำหรับจัดอันดับ ESI
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
11 |
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
12 |
LINE_CHANNEL_SECRET = os.getenv("LINE_CHANNEL_SECRET")
|
13 |
|
@@ -30,30 +35,47 @@ async def webhook(request: Request, x_line_signature: str = Header(None)):
|
|
30 |
body = await request.body()
|
31 |
body_text = body.decode("utf-8")
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
try:
|
34 |
handler.handle(body_text, x_line_signature)
|
35 |
except InvalidSignatureError:
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
# Event handler
|
41 |
@handler.add(MessageEvent, message=TextMessage)
|
42 |
def handle_message(event):
|
43 |
user_message = event.message.text
|
44 |
-
|
45 |
|
46 |
-
#
|
47 |
response_text = get_triage_response(esi_level)
|
48 |
|
49 |
-
|
|
|
|
|
50 |
threading.Thread(target=reply_message, args=(event.reply_token, response_text)).start()
|
51 |
|
52 |
def reply_message(reply_token, message):
|
|
|
53 |
try:
|
54 |
line_bot_api.reply_message(reply_token, TextSendMessage(text=message))
|
|
|
55 |
except Exception as e:
|
56 |
-
|
57 |
|
58 |
def get_triage_response(esi_level):
|
59 |
"""
|
|
|
1 |
import os
|
2 |
import threading
|
3 |
+
import logging
|
4 |
import uvicorn
|
5 |
from fastapi import FastAPI, Request, HTTPException, Header
|
6 |
from linebot import LineBotApi, WebhookHandler
|
|
|
8 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
9 |
from model import classify_esi # นำเข้าโมเดลสำหรับจัดอันดับ ESI
|
10 |
|
11 |
+
# ตั้งค่า Logging
|
12 |
+
logging.basicConfig(level=logging.INFO)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
+
# โหลด Environment Variables
|
16 |
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
17 |
LINE_CHANNEL_SECRET = os.getenv("LINE_CHANNEL_SECRET")
|
18 |
|
|
|
35 |
body = await request.body()
|
36 |
body_text = body.decode("utf-8")
|
37 |
|
38 |
+
# Log ข้อความที่ได้รับจาก LINE
|
39 |
+
logger.info(f"Received webhook event: {body_text}")
|
40 |
+
|
41 |
+
try:
|
42 |
+
# ✅ ตอบกลับ LINE ทันที เพื่อป้องกัน Timeout
|
43 |
+
threading.Thread(target=handle_webhook, args=(body_text, x_line_signature)).start()
|
44 |
+
return {"status": "OK"}
|
45 |
+
except Exception as e:
|
46 |
+
logger.error(f"Error processing webhook: {e}")
|
47 |
+
return {"status": "Error"}, 500
|
48 |
+
|
49 |
+
def handle_webhook(body_text, x_line_signature):
|
50 |
+
""" ฟังก์ชันประมวลผล Webhook แยกต่างหาก """
|
51 |
try:
|
52 |
handler.handle(body_text, x_line_signature)
|
53 |
except InvalidSignatureError:
|
54 |
+
logger.error("Invalid signature error")
|
55 |
+
except Exception as e:
|
56 |
+
logger.error(f"Error handling webhook: {e}")
|
57 |
|
58 |
+
# Event handler สำหรับข้อความที่ได้รับ
|
59 |
@handler.add(MessageEvent, message=TextMessage)
|
60 |
def handle_message(event):
|
61 |
user_message = event.message.text
|
62 |
+
logger.info(f"User message: {user_message}") # Log ข้อความจากผู้ใช้
|
63 |
|
64 |
+
esi_level = classify_esi(user_message) # ใช้โมเดล AI วิเคราะห์ ESI
|
65 |
response_text = get_triage_response(esi_level)
|
66 |
|
67 |
+
logger.info(f"Bot response: {response_text}") # Log ข้อความที่บอทจะส่ง
|
68 |
+
|
69 |
+
# ใช้ threading เพื่อให้ bot ตอบกลับเร็วขึ้น
|
70 |
threading.Thread(target=reply_message, args=(event.reply_token, response_text)).start()
|
71 |
|
72 |
def reply_message(reply_token, message):
|
73 |
+
""" ส่งข้อความตอบกลับผู้ใช้ """
|
74 |
try:
|
75 |
line_bot_api.reply_message(reply_token, TextSendMessage(text=message))
|
76 |
+
logger.info("Reply sent successfully") # Log เมื่อส่งสำเร็จ
|
77 |
except Exception as e:
|
78 |
+
logger.error(f"Error sending reply: {e}")
|
79 |
|
80 |
def get_triage_response(esi_level):
|
81 |
"""
|