fix duplicate message
Browse files- app/main.py +7 -2
- app/sheets.py +8 -2
app/main.py
CHANGED
|
@@ -195,8 +195,11 @@ async def process_message(message_data: Dict[str, Any]):
|
|
| 195 |
if history:
|
| 196 |
# 1. Chặn duplicate message (trùng sender_id, page_id, timestamp)
|
| 197 |
for row in history:
|
|
|
|
|
|
|
|
|
|
| 198 |
if (
|
| 199 |
-
str(timestamp) in
|
| 200 |
and str(row.get('recipient_id')) == str(sender_id)
|
| 201 |
and str(row.get('page_id')) == str(page_id)
|
| 202 |
):
|
|
@@ -213,7 +216,7 @@ async def process_message(message_data: Dict[str, Any]):
|
|
| 213 |
'vehicle': row.get('originalvehicle'),
|
| 214 |
'action': row.get('originalaction'),
|
| 215 |
'purpose': row.get('originalpurpose'),
|
| 216 |
-
'timestamp':
|
| 217 |
'is_done': row.get('isdone')
|
| 218 |
}
|
| 219 |
else:
|
|
@@ -230,6 +233,8 @@ async def process_message(message_data: Dict[str, Any]):
|
|
| 230 |
conv[key] = value
|
| 231 |
logger.info(f"[DEBUG] Message history sau update: {conv}")
|
| 232 |
# Thêm timestamp mới nếu chưa có
|
|
|
|
|
|
|
| 233 |
if timestamp not in conv['timestamp']:
|
| 234 |
conv['timestamp'].append(timestamp)
|
| 235 |
|
|
|
|
| 195 |
if history:
|
| 196 |
# 1. Chặn duplicate message (trùng sender_id, page_id, timestamp)
|
| 197 |
for row in history:
|
| 198 |
+
row_timestamps = row.get('timestamp', [])
|
| 199 |
+
if isinstance(row_timestamps, list) and len(row_timestamps) == 1 and isinstance(row_timestamps[0], list):
|
| 200 |
+
row_timestamps = row_timestamps[0]
|
| 201 |
if (
|
| 202 |
+
str(timestamp) in [str(ts) for ts in row_timestamps]
|
| 203 |
and str(row.get('recipient_id')) == str(sender_id)
|
| 204 |
and str(row.get('page_id')) == str(page_id)
|
| 205 |
):
|
|
|
|
| 216 |
'vehicle': row.get('originalvehicle'),
|
| 217 |
'action': row.get('originalaction'),
|
| 218 |
'purpose': row.get('originalpurpose'),
|
| 219 |
+
'timestamp': row_timestamps,
|
| 220 |
'is_done': row.get('isdone')
|
| 221 |
}
|
| 222 |
else:
|
|
|
|
| 233 |
conv[key] = value
|
| 234 |
logger.info(f"[DEBUG] Message history sau update: {conv}")
|
| 235 |
# Thêm timestamp mới nếu chưa có
|
| 236 |
+
if isinstance(conv['timestamp'], list) and len(conv['timestamp']) == 1 and isinstance(conv['timestamp'][0], list):
|
| 237 |
+
conv['timestamp'] = conv['timestamp'][0]
|
| 238 |
if timestamp not in conv['timestamp']:
|
| 239 |
conv['timestamp'].append(timestamp)
|
| 240 |
|
app/sheets.py
CHANGED
|
@@ -92,6 +92,8 @@ class SheetsClient:
|
|
| 92 |
for row in values:
|
| 93 |
row = row + [""] * (12 - len(row))
|
| 94 |
timestamps = json.loads(row[10]) if row[10] else []
|
|
|
|
|
|
|
| 95 |
if row[4] == user_id and row[5] == page_id and row[11].lower() == 'false':
|
| 96 |
history.append({
|
| 97 |
'conversation_id': row[0],
|
|
@@ -154,9 +156,11 @@ class SheetsClient:
|
|
| 154 |
for row in values:
|
| 155 |
if len(row) >= 11:
|
| 156 |
row_timestamps = json.loads(row[10]) if row[10] else []
|
|
|
|
|
|
|
| 157 |
row_user_id = row[4]
|
| 158 |
row_page_id = row[5]
|
| 159 |
-
if (str(timestamp) in row_timestamps and str(row_user_id) == str(user_id) and str(row_page_id) == str(page_id)):
|
| 160 |
# Found duplicate, return existing conversation
|
| 161 |
logger.info(f"Found duplicate conversation for user {user_id}, page {page_id}, timestamp {timestamp}")
|
| 162 |
return {
|
|
@@ -239,6 +243,8 @@ class SheetsClient:
|
|
| 239 |
current_row.append("")
|
| 240 |
# Tạo dòng mới với giá trị mới nếu có, giữ nguyên nếu không
|
| 241 |
current_timestamps = json.loads(current_row[10]) if current_row[10] else []
|
|
|
|
|
|
|
| 242 |
if timestamp not in current_timestamps:
|
| 243 |
current_timestamps.append(timestamp)
|
| 244 |
new_row = [
|
|
@@ -255,7 +261,7 @@ class SheetsClient:
|
|
| 255 |
json.dumps(current_timestamps),
|
| 256 |
str(is_done).lower() if is_done is not None else current_row[11]
|
| 257 |
]
|
| 258 |
-
update_range = f"{SHEET_RANGE.split('!')[0]}!A{
|
| 259 |
logger.info(f"[DEBUG] Gsheet update range {update_range}")
|
| 260 |
body = {
|
| 261 |
'values': [new_row]
|
|
|
|
| 92 |
for row in values:
|
| 93 |
row = row + [""] * (12 - len(row))
|
| 94 |
timestamps = json.loads(row[10]) if row[10] else []
|
| 95 |
+
if isinstance(timestamps, list) and len(timestamps) == 1 and isinstance(timestamps[0], list):
|
| 96 |
+
timestamps = timestamps[0]
|
| 97 |
if row[4] == user_id and row[5] == page_id and row[11].lower() == 'false':
|
| 98 |
history.append({
|
| 99 |
'conversation_id': row[0],
|
|
|
|
| 156 |
for row in values:
|
| 157 |
if len(row) >= 11:
|
| 158 |
row_timestamps = json.loads(row[10]) if row[10] else []
|
| 159 |
+
if isinstance(row_timestamps, list) and len(row_timestamps) == 1 and isinstance(row_timestamps[0], list):
|
| 160 |
+
row_timestamps = row_timestamps[0]
|
| 161 |
row_user_id = row[4]
|
| 162 |
row_page_id = row[5]
|
| 163 |
+
if (str(timestamp) in [str(ts) for ts in row_timestamps] and str(row_user_id) == str(user_id) and str(row_page_id) == str(page_id)):
|
| 164 |
# Found duplicate, return existing conversation
|
| 165 |
logger.info(f"Found duplicate conversation for user {user_id}, page {page_id}, timestamp {timestamp}")
|
| 166 |
return {
|
|
|
|
| 243 |
current_row.append("")
|
| 244 |
# Tạo dòng mới với giá trị mới nếu có, giữ nguyên nếu không
|
| 245 |
current_timestamps = json.loads(current_row[10]) if current_row[10] else []
|
| 246 |
+
if isinstance(current_timestamps, list) and len(current_timestamps) == 1 and isinstance(current_timestamps[0], list):
|
| 247 |
+
current_timestamps = current_timestamps[0]
|
| 248 |
if timestamp not in current_timestamps:
|
| 249 |
current_timestamps.append(timestamp)
|
| 250 |
new_row = [
|
|
|
|
| 261 |
json.dumps(current_timestamps),
|
| 262 |
str(is_done).lower() if is_done is not None else current_row[11]
|
| 263 |
]
|
| 264 |
+
update_range = f"{SHEET_RANGE.split('!')[0]}!A{row_index + 1}"
|
| 265 |
logger.info(f"[DEBUG] Gsheet update range {update_range}")
|
| 266 |
body = {
|
| 267 |
'values': [new_row]
|