fix token error
Browse files- app/main.py +15 -8
app/main.py
CHANGED
|
@@ -104,6 +104,7 @@ async def webhook(request: Request):
|
|
| 104 |
try:
|
| 105 |
body = json.loads(body_bytes)
|
| 106 |
message_data = facebook_client.parse_message(body)
|
|
|
|
| 107 |
|
| 108 |
if not message_data:
|
| 109 |
return {"status": "ok"}
|
|
@@ -116,6 +117,7 @@ async def webhook(request: Request):
|
|
| 116 |
logger.error(f"Error processing webhook: {e}")
|
| 117 |
raise HTTPException(status_code=500, detail="Internal server error")
|
| 118 |
|
|
|
|
| 119 |
async def process_message(message_data: Dict[str, Any]):
|
| 120 |
"""
|
| 121 |
Xử lý message từ người dùng Facebook, phân tích, truy vấn, gửi phản hồi và log lại.
|
|
@@ -129,8 +131,9 @@ async def process_message(message_data: Dict[str, Any]):
|
|
| 129 |
if not message_text:
|
| 130 |
return
|
| 131 |
|
| 132 |
-
# Get page access token
|
| 133 |
-
page_token =
|
|
|
|
| 134 |
if not page_token:
|
| 135 |
logger.error(f"No access token found for page {page_id}")
|
| 136 |
return
|
|
@@ -140,9 +143,11 @@ async def process_message(message_data: Dict[str, Any]):
|
|
| 140 |
keywords = extract_keywords(message_text, VEHICLE_KEYWORDS)
|
| 141 |
|
| 142 |
# Get conversation history (run in thread pool)
|
| 143 |
-
|
| 144 |
-
|
|
|
|
| 145 |
)
|
|
|
|
| 146 |
|
| 147 |
response = ""
|
| 148 |
if command == "xong":
|
|
@@ -151,9 +156,11 @@ async def process_message(message_data: Dict[str, Any]):
|
|
| 151 |
else:
|
| 152 |
# Create embedding from message
|
| 153 |
embedding = await embedding_client.create_embedding(message_text)
|
|
|
|
| 154 |
|
| 155 |
-
# Search for similar documents
|
| 156 |
-
matches =
|
|
|
|
| 157 |
|
| 158 |
if matches:
|
| 159 |
response = format_search_results(matches)
|
|
@@ -166,8 +173,8 @@ async def process_message(message_data: Dict[str, Any]):
|
|
| 166 |
await facebook_client.send_message(page_token, sender_id, response)
|
| 167 |
|
| 168 |
# Log conversation (run in thread pool)
|
| 169 |
-
await
|
| 170 |
-
executor, lambda: sheets_client.log_conversation(sender_id, page_id, message_text, keywords, response)
|
| 171 |
)
|
| 172 |
|
| 173 |
def format_search_results(matches: List[Dict[str, Any]]) -> str:
|
|
|
|
| 104 |
try:
|
| 105 |
body = json.loads(body_bytes)
|
| 106 |
message_data = facebook_client.parse_message(body)
|
| 107 |
+
logger.info(f"[DEBUG] message_data: {message_data}")
|
| 108 |
|
| 109 |
if not message_data:
|
| 110 |
return {"status": "ok"}
|
|
|
|
| 117 |
logger.error(f"Error processing webhook: {e}")
|
| 118 |
raise HTTPException(status_code=500, detail="Internal server error")
|
| 119 |
|
| 120 |
+
@timing_decorator_async
|
| 121 |
async def process_message(message_data: Dict[str, Any]):
|
| 122 |
"""
|
| 123 |
Xử lý message từ người dùng Facebook, phân tích, truy vấn, gửi phản hồi và log lại.
|
|
|
|
| 131 |
if not message_text:
|
| 132 |
return
|
| 133 |
|
| 134 |
+
# Get page access token (sync)
|
| 135 |
+
page_token = supabase_client.get_page_token(page_id)
|
| 136 |
+
logger.info(f"[DEBUG] page_token: {page_token}")
|
| 137 |
if not page_token:
|
| 138 |
logger.error(f"No access token found for page {page_id}")
|
| 139 |
return
|
|
|
|
| 143 |
keywords = extract_keywords(message_text, VEHICLE_KEYWORDS)
|
| 144 |
|
| 145 |
# Get conversation history (run in thread pool)
|
| 146 |
+
loop = asyncio.get_event_loop()
|
| 147 |
+
history = await loop.run_in_executor(
|
| 148 |
+
executor, lambda: sheets_client.get_conversation_history(sender_id, page_id)
|
| 149 |
)
|
| 150 |
+
logger.info(f"[DEBUG] history: {history}")
|
| 151 |
|
| 152 |
response = ""
|
| 153 |
if command == "xong":
|
|
|
|
| 156 |
else:
|
| 157 |
# Create embedding from message
|
| 158 |
embedding = await embedding_client.create_embedding(message_text)
|
| 159 |
+
logger.info(f"[DEBUG] embedding: {embedding[:5]} ... (total {len(embedding)})")
|
| 160 |
|
| 161 |
+
# Search for similar documents (sync)
|
| 162 |
+
matches = supabase_client.match_documents(embedding)
|
| 163 |
+
logger.info(f"[DEBUG] matches: {matches}")
|
| 164 |
|
| 165 |
if matches:
|
| 166 |
response = format_search_results(matches)
|
|
|
|
| 173 |
await facebook_client.send_message(page_token, sender_id, response)
|
| 174 |
|
| 175 |
# Log conversation (run in thread pool)
|
| 176 |
+
await loop.run_in_executor(
|
| 177 |
+
executor, lambda: sheets_client.log_conversation(sender_id, page_id, message_text, keywords, response)
|
| 178 |
)
|
| 179 |
|
| 180 |
def format_search_results(matches: List[Dict[str, Any]]) -> str:
|