fix duplicate message
Browse files- app/sheets.py +29 -23
app/sheets.py
CHANGED
|
@@ -79,21 +79,21 @@ class SheetsClient:
|
|
| 79 |
self.authenticate()
|
| 80 |
if not self.service:
|
| 81 |
raise RuntimeError("Google Sheets service not initialized")
|
| 82 |
-
|
| 83 |
range_name = SHEET_RANGE
|
| 84 |
result = self.service.spreadsheets().values().get(
|
| 85 |
spreadsheetId=self.sheet_id,
|
| 86 |
range=range_name
|
| 87 |
).execute()
|
| 88 |
-
|
| 89 |
values = result.get('values', [])
|
| 90 |
history = []
|
| 91 |
-
|
| 92 |
for row in values:
|
| 93 |
row = row + [""] * (12 - len(row))
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
if row[4] == user_id and row[5] == page_id and row[11].lower() == 'false':
|
| 98 |
history.append({
|
| 99 |
'conversation_id': row[0],
|
|
@@ -109,7 +109,6 @@ class SheetsClient:
|
|
| 109 |
'timestamp': timestamps,
|
| 110 |
'isdone': row[11].lower() == 'true'
|
| 111 |
})
|
| 112 |
-
|
| 113 |
return history
|
| 114 |
except Exception as e:
|
| 115 |
logger.error(f"Error getting conversation history: {e}")
|
|
@@ -128,7 +127,7 @@ class SheetsClient:
|
|
| 128 |
originalvehicle: str = "",
|
| 129 |
originalaction: str = "",
|
| 130 |
originalpurpose: str = "",
|
| 131 |
-
timestamp:
|
| 132 |
isdone: bool = False
|
| 133 |
) -> Optional[Dict[str, Any]]:
|
| 134 |
"""
|
|
@@ -149,14 +148,21 @@ class SheetsClient:
|
|
| 149 |
values = result.get('values', [])
|
| 150 |
logger.info(f"[DEBUG] Gsheet values {values}")
|
| 151 |
ts = datetime.now().isoformat()
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
if not conversation_id:
|
| 154 |
# Check for duplicates before creating new conversation
|
| 155 |
for row in values:
|
| 156 |
if len(row) >= 11:
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
| 160 |
row_recipient_id = row[4]
|
| 161 |
row_page_id = row[5]
|
| 162 |
if (str(timestamp) in [str(ts) for ts in row_timestamps] and str(row_recipient_id) == str(recipient_id) and str(row_page_id) == str(page_id)):
|
|
@@ -193,11 +199,9 @@ class SheetsClient:
|
|
| 193 |
json.dumps(timestamp),
|
| 194 |
str(isdone).lower()
|
| 195 |
]
|
| 196 |
-
|
| 197 |
body = {
|
| 198 |
'values': [new_row]
|
| 199 |
}
|
| 200 |
-
|
| 201 |
range_name = SHEET_RANGE
|
| 202 |
self.service.spreadsheets().values().append(
|
| 203 |
spreadsheetId=self.sheet_id,
|
|
@@ -205,7 +209,6 @@ class SheetsClient:
|
|
| 205 |
valueInputOption='RAW',
|
| 206 |
body=body
|
| 207 |
).execute()
|
| 208 |
-
|
| 209 |
logger.info(f"Thêm mới conversation: {conversation_id} | Giá trị: {dict(zip(['conversation_id','originalcommand','originalcontent','originalattachments','recipient_id','page_id','originaltext','originalvehicle','originalaction','originalpurpose','timestamp','isdone'], new_row))}")
|
| 210 |
|
| 211 |
# Return the conversation data directly
|
|
@@ -240,12 +243,16 @@ class SheetsClient:
|
|
| 240 |
logger.info(f"[DEBUG] Gsheet current row {current_row}")
|
| 241 |
while len(current_row) < 13:
|
| 242 |
current_row.append("")
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
current_timestamps =
|
| 247 |
-
if
|
| 248 |
-
current_timestamps
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
new_row = [
|
| 250 |
conversation_id,
|
| 251 |
originalcommand if originalcommand else current_row[1],
|
|
@@ -289,10 +296,9 @@ class SheetsClient:
|
|
| 289 |
'originalvehicle': new_row[7],
|
| 290 |
'originalaction': new_row[8],
|
| 291 |
'originalpurpose': new_row[9],
|
| 292 |
-
'timestamp':
|
| 293 |
'isdone': new_row[11].lower() == 'true'
|
| 294 |
}
|
| 295 |
-
|
| 296 |
return None
|
| 297 |
except Exception as e:
|
| 298 |
logger.error(f"Error logging conversation: {e}")
|
|
|
|
| 79 |
self.authenticate()
|
| 80 |
if not self.service:
|
| 81 |
raise RuntimeError("Google Sheets service not initialized")
|
|
|
|
| 82 |
range_name = SHEET_RANGE
|
| 83 |
result = self.service.spreadsheets().values().get(
|
| 84 |
spreadsheetId=self.sheet_id,
|
| 85 |
range=range_name
|
| 86 |
).execute()
|
|
|
|
| 87 |
values = result.get('values', [])
|
| 88 |
history = []
|
|
|
|
| 89 |
for row in values:
|
| 90 |
row = row + [""] * (12 - len(row))
|
| 91 |
+
try:
|
| 92 |
+
timestamps = json.loads(row[10]) if row[10] else []
|
| 93 |
+
except Exception:
|
| 94 |
+
timestamps = []
|
| 95 |
+
if not isinstance(timestamps, list):
|
| 96 |
+
timestamps = [timestamps]
|
| 97 |
if row[4] == user_id and row[5] == page_id and row[11].lower() == 'false':
|
| 98 |
history.append({
|
| 99 |
'conversation_id': row[0],
|
|
|
|
| 109 |
'timestamp': timestamps,
|
| 110 |
'isdone': row[11].lower() == 'true'
|
| 111 |
})
|
|
|
|
| 112 |
return history
|
| 113 |
except Exception as e:
|
| 114 |
logger.error(f"Error getting conversation history: {e}")
|
|
|
|
| 127 |
originalvehicle: str = "",
|
| 128 |
originalaction: str = "",
|
| 129 |
originalpurpose: str = "",
|
| 130 |
+
timestamp: Any = None,
|
| 131 |
isdone: bool = False
|
| 132 |
) -> Optional[Dict[str, Any]]:
|
| 133 |
"""
|
|
|
|
| 148 |
values = result.get('values', [])
|
| 149 |
logger.info(f"[DEBUG] Gsheet values {values}")
|
| 150 |
ts = datetime.now().isoformat()
|
| 151 |
+
# Đảm bảo timestamp luôn là list
|
| 152 |
+
if timestamp is None:
|
| 153 |
+
timestamp = []
|
| 154 |
+
elif not isinstance(timestamp, list):
|
| 155 |
+
timestamp = [timestamp]
|
| 156 |
if not conversation_id:
|
| 157 |
# Check for duplicates before creating new conversation
|
| 158 |
for row in values:
|
| 159 |
if len(row) >= 11:
|
| 160 |
+
try:
|
| 161 |
+
row_timestamps = json.loads(row[10]) if row[10] else []
|
| 162 |
+
except Exception:
|
| 163 |
+
row_timestamps = []
|
| 164 |
+
if not isinstance(row_timestamps, list):
|
| 165 |
+
row_timestamps = [row_timestamps]
|
| 166 |
row_recipient_id = row[4]
|
| 167 |
row_page_id = row[5]
|
| 168 |
if (str(timestamp) in [str(ts) for ts in row_timestamps] and str(row_recipient_id) == str(recipient_id) and str(row_page_id) == str(page_id)):
|
|
|
|
| 199 |
json.dumps(timestamp),
|
| 200 |
str(isdone).lower()
|
| 201 |
]
|
|
|
|
| 202 |
body = {
|
| 203 |
'values': [new_row]
|
| 204 |
}
|
|
|
|
| 205 |
range_name = SHEET_RANGE
|
| 206 |
self.service.spreadsheets().values().append(
|
| 207 |
spreadsheetId=self.sheet_id,
|
|
|
|
| 209 |
valueInputOption='RAW',
|
| 210 |
body=body
|
| 211 |
).execute()
|
|
|
|
| 212 |
logger.info(f"Thêm mới conversation: {conversation_id} | Giá trị: {dict(zip(['conversation_id','originalcommand','originalcontent','originalattachments','recipient_id','page_id','originaltext','originalvehicle','originalaction','originalpurpose','timestamp','isdone'], new_row))}")
|
| 213 |
|
| 214 |
# Return the conversation data directly
|
|
|
|
| 243 |
logger.info(f"[DEBUG] Gsheet current row {current_row}")
|
| 244 |
while len(current_row) < 13:
|
| 245 |
current_row.append("")
|
| 246 |
+
try:
|
| 247 |
+
current_timestamps = json.loads(current_row[10]) if current_row[10] else []
|
| 248 |
+
except Exception:
|
| 249 |
+
current_timestamps = []
|
| 250 |
+
if not isinstance(current_timestamps, list):
|
| 251 |
+
current_timestamps = [current_timestamps]
|
| 252 |
+
# Chỉ append nếu chưa có
|
| 253 |
+
for ts in timestamp:
|
| 254 |
+
if ts not in current_timestamps:
|
| 255 |
+
current_timestamps.append(ts)
|
| 256 |
new_row = [
|
| 257 |
conversation_id,
|
| 258 |
originalcommand if originalcommand else current_row[1],
|
|
|
|
| 296 |
'originalvehicle': new_row[7],
|
| 297 |
'originalaction': new_row[8],
|
| 298 |
'originalpurpose': new_row[9],
|
| 299 |
+
'timestamp': current_timestamps,
|
| 300 |
'isdone': new_row[11].lower() == 'true'
|
| 301 |
}
|
|
|
|
| 302 |
return None
|
| 303 |
except Exception as e:
|
| 304 |
logger.error(f"Error logging conversation: {e}")
|