VietCat commited on
Commit
86f006f
·
1 Parent(s): 9b7d215

fix duplicate message

Browse files
Files changed (1) hide show
  1. app/main.py +40 -13
app/main.py CHANGED
@@ -174,17 +174,6 @@ async def process_message(message_data: Dict[str, Any]):
174
  )
175
  logger.info(f"[DEBUG] history: {history}")
176
 
177
- # 1. Chặn duplicate message (trùng sender_id, page_id, timestamp)
178
- for row in history:
179
- if (
180
- str(row.get('timestamp')) == str(timestamp)
181
- and str(row.get('recipient_id')) == str(sender_id)
182
- and str(row.get('page_id')) == str(page_id)
183
- ):
184
- logger.info("[DUPLICATE] Message duplicate, skipping log.")
185
- return
186
-
187
- # 2. Ghi conversation mới NGAY LẬP TỨC với thông tin cơ bản
188
  log_kwargs = {
189
  'conversation_id': None,
190
  'user_id': sender_id,
@@ -199,10 +188,44 @@ async def process_message(message_data: Dict[str, Any]):
199
  'timestamp': timestamp,
200
  'is_done': False
201
  }
202
- conv = await loop.run_in_executor(executor, lambda: sheets_client.log_conversation(**log_kwargs))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  if not conv:
204
  logger.error("Không thể tạo conversation mới!")
205
  return
 
 
 
 
206
 
207
  # Get page access token (sync)
208
  page_token = supabase_client.get_page_token(page_id)
@@ -256,8 +279,12 @@ async def process_message(message_data: Dict[str, Any]):
256
  'is_done': False
257
  }
258
 
 
 
 
 
259
  # 5. Xử lý logic nghiệp vụ
260
- response = await process_business_logic(update_kwargs, page_token)
261
 
262
  # 6. Gửi response và cập nhật final state
263
  await facebook_client.send_message(page_token, sender_id, response)
 
174
  )
175
  logger.info(f"[DEBUG] history: {history}")
176
 
 
 
 
 
 
 
 
 
 
 
 
177
  log_kwargs = {
178
  'conversation_id': None,
179
  'user_id': sender_id,
 
188
  'timestamp': timestamp,
189
  'is_done': False
190
  }
191
+
192
+ conv = None
193
+
194
+ if history:
195
+ # 1. Chặn duplicate message (trùng sender_id, page_id, timestamp)
196
+ for row in history:
197
+ if (
198
+ str(row.get('timestamp')) == str(timestamp)
199
+ and str(row.get('recipient_id')) == str(sender_id)
200
+ and str(row.get('page_id')) == str(page_id)
201
+ ):
202
+ logger.info("[DUPLICATE] Message duplicate, skipping log.")
203
+ return
204
+ conv = {
205
+ 'conversation_id': row.get('conversation_id'),
206
+ 'user_id': row.get('recipient_id'),
207
+ 'page_id': row.get('page_id'),
208
+ 'message': row.get('originaltext'),
209
+ 'command': row.get('originalcommand'),
210
+ 'content': row.get('originalcontent'),
211
+ 'attachments': row.get('originalattachments'),
212
+ 'vehicle': row.get('originalvehicle'),
213
+ 'action': row.get('originalaction'),
214
+ 'purpose': row.get('originalpurpose'),
215
+ 'timestamp': row.get('timestamp'),
216
+ 'is_done': row.get('isdone')
217
+ }
218
+ else:
219
+ # 2. Ghi conversation mới NGAY LẬP TỨC với thông tin cơ bản
220
+ conv = await loop.run_in_executor(executor, lambda: sheets_client.log_conversation(**log_kwargs))
221
+
222
  if not conv:
223
  logger.error("Không thể tạo conversation mới!")
224
  return
225
+ else:
226
+ for key, value in log_kwargs.items():
227
+ if value not in (None, "", []) and conv.get(key) in (None, "", []):
228
+ conv[key] = value
229
 
230
  # Get page access token (sync)
231
  page_token = supabase_client.get_page_token(page_id)
 
279
  'is_done': False
280
  }
281
 
282
+ for key, value in update_kwargs.items():
283
+ if value not in (None, "", []) and conv.get(key) in (None, "", []):
284
+ conv[key] = value
285
+
286
  # 5. Xử lý logic nghiệp vụ
287
+ response = await process_business_logic(conv, page_token)
288
 
289
  # 6. Gửi response và cập nhật final state
290
  await facebook_client.send_message(page_token, sender_id, response)