fix duplicate message
Browse files- app/sheets.py +38 -12
app/sheets.py
CHANGED
|
@@ -140,12 +140,46 @@ class SheetsClient:
|
|
| 140 |
if not self.service:
|
| 141 |
raise RuntimeError("Google Sheets service not initialized")
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
ts = datetime.now().isoformat()
|
| 144 |
|
| 145 |
if not conversation_id:
|
| 146 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
conversation_id = generate_conversation_id(user_id, page_id, ts)
|
| 148 |
-
|
| 149 |
conversation_id,
|
| 150 |
command,
|
| 151 |
content,
|
|
@@ -158,10 +192,10 @@ class SheetsClient:
|
|
| 158 |
purpose,
|
| 159 |
timestamp,
|
| 160 |
str(is_done).lower()
|
| 161 |
-
]
|
| 162 |
|
| 163 |
body = {
|
| 164 |
-
'values':
|
| 165 |
}
|
| 166 |
|
| 167 |
range_name = SHEET_RANGE
|
|
@@ -189,14 +223,6 @@ class SheetsClient:
|
|
| 189 |
}
|
| 190 |
else:
|
| 191 |
# Update existing conversation
|
| 192 |
-
|
| 193 |
-
# Find the row with this conversation_id
|
| 194 |
-
result = self.service.spreadsheets().values().get(
|
| 195 |
-
spreadsheetId=self.sheet_id,
|
| 196 |
-
range=SHEET_RANGE
|
| 197 |
-
).execute()
|
| 198 |
-
|
| 199 |
-
values = result.get('values', [])
|
| 200 |
row_index = None
|
| 201 |
|
| 202 |
for i, row in enumerate(values):
|
|
|
|
| 140 |
if not self.service:
|
| 141 |
raise RuntimeError("Google Sheets service not initialized")
|
| 142 |
|
| 143 |
+
# Get existing data to check for duplicates
|
| 144 |
+
result = self.service.spreadsheets().values().get(
|
| 145 |
+
spreadsheetId=self.sheet_id,
|
| 146 |
+
range=SHEET_RANGE
|
| 147 |
+
).execute()
|
| 148 |
+
values = result.get('values', [])
|
| 149 |
+
|
| 150 |
ts = datetime.now().isoformat()
|
| 151 |
|
| 152 |
if not conversation_id:
|
| 153 |
+
# Check for duplicates before creating new conversation
|
| 154 |
+
for row in values:
|
| 155 |
+
# Ensure row has enough columns
|
| 156 |
+
if len(row) >= 11:
|
| 157 |
+
row_timestamp = row[10]
|
| 158 |
+
row_user_id = row[4]
|
| 159 |
+
row_page_id = row[5]
|
| 160 |
+
if (str(row_timestamp) == str(timestamp) and
|
| 161 |
+
str(row_user_id) == str(user_id) and
|
| 162 |
+
str(row_page_id) == str(page_id)):
|
| 163 |
+
# Found duplicate, return existing conversation
|
| 164 |
+
logger.info(f"Found duplicate conversation for user {user_id}, page {page_id}, timestamp {timestamp}")
|
| 165 |
+
return {
|
| 166 |
+
'conversation_id': row[0],
|
| 167 |
+
'originalcommand': row[1],
|
| 168 |
+
'originalcontent': row[2],
|
| 169 |
+
'originalattachments': json.loads(row[3]) if row[3] else [],
|
| 170 |
+
'recipient_id': row[4],
|
| 171 |
+
'page_id': row[5],
|
| 172 |
+
'originaltext': row[6],
|
| 173 |
+
'originalvehicle': row[7],
|
| 174 |
+
'originalaction': row[8],
|
| 175 |
+
'originalpurpose': row[9],
|
| 176 |
+
'timestamp': row[10],
|
| 177 |
+
'isdone': row[11].lower() == 'true' if len(row) > 11 else False
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
# No duplicate found, create new conversation
|
| 181 |
conversation_id = generate_conversation_id(user_id, page_id, ts)
|
| 182 |
+
new_row = [
|
| 183 |
conversation_id,
|
| 184 |
command,
|
| 185 |
content,
|
|
|
|
| 192 |
purpose,
|
| 193 |
timestamp,
|
| 194 |
str(is_done).lower()
|
| 195 |
+
]
|
| 196 |
|
| 197 |
body = {
|
| 198 |
+
'values': [new_row]
|
| 199 |
}
|
| 200 |
|
| 201 |
range_name = SHEET_RANGE
|
|
|
|
| 223 |
}
|
| 224 |
else:
|
| 225 |
# Update existing conversation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
row_index = None
|
| 227 |
|
| 228 |
for i, row in enumerate(values):
|