VietCat commited on
Commit
759b17a
·
1 Parent(s): f0cf2d8

fix message format

Browse files
Files changed (1) hide show
  1. app/message_processor.py +22 -8
app/message_processor.py CHANGED
@@ -6,6 +6,7 @@ from .constants import SUMMARY_STATUS_MESSAGES, PROCESSING_STATUS_MESSAGES, FOUN
6
  from .utils import get_random_message
7
  from .facebook import FacebookClient
8
  from app.config import get_settings
 
9
 
10
  class MessageProcessor:
11
  def __init__(self, channel, sender_id):
@@ -186,7 +187,7 @@ class MessageProcessor:
186
  logger.info(f"[DEBUG] Message history sau khi process: {conv}")
187
  # 6. Gửi response và cập nhật final state
188
  # Replace all occurrences of '**' with '*' before sending
189
- response_to_send = response.replace('**', '*') if isinstance(response, str) else response
190
  await self.facebook.send_message(message=response_to_send)
191
  if hasattr(self.channel, 'sheets'):
192
  await loop.run_in_executor(None, lambda: self.channel.sheets.log_conversation(**conv))
@@ -267,12 +268,13 @@ class MessageProcessor:
267
  reranked = await self.channel.reranker.rerank(question, matches, top_k=5)
268
  if reranked:
269
  # Gửi Facebook message sau khi hoàn thành
270
- if self.facebook:
271
- try:
272
- message = get_random_message(BATCH_STATUS_MESSAGES)
273
- await self.facebook.send_message(message=f"... {message} ...")
274
- except Exception as e:
275
- logger.error(f"[RERANK][FACEBOOK] Error sending batch message: {e}")
 
276
  matches = reranked
277
  except Exception as e:
278
  logger.error(f"[RERANK] Lỗi khi rerank: {e}")
@@ -380,4 +382,16 @@ class MessageProcessor:
380
 
381
  async def create_facebook_post(self, page_token: str, sender_id: str, history: List[Dict[str, Any]]) -> str:
382
  logger.info(f"[MOCK] Creating Facebook post for sender_id={sender_id} with history={history}")
383
- return "https://facebook.com/mock_post_url"
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from .utils import get_random_message
7
  from .facebook import FacebookClient
8
  from app.config import get_settings
9
+ import re
10
 
11
  class MessageProcessor:
12
  def __init__(self, channel, sender_id):
 
187
  logger.info(f"[DEBUG] Message history sau khi process: {conv}")
188
  # 6. Gửi response và cập nhật final state
189
  # Replace all occurrences of '**' with '*' before sending
190
+ response_to_send = format_for_facebook(response.replace('**', '*')) if isinstance(response, str) else response
191
  await self.facebook.send_message(message=response_to_send)
192
  if hasattr(self.channel, 'sheets'):
193
  await loop.run_in_executor(None, lambda: self.channel.sheets.log_conversation(**conv))
 
268
  reranked = await self.channel.reranker.rerank(question, matches, top_k=5)
269
  if reranked:
270
  # Gửi Facebook message sau khi hoàn thành
271
+ # Tạm comment đi để test
272
+ # if self.facebook:
273
+ # try:
274
+ # message = get_random_message(BATCH_STATUS_MESSAGES)
275
+ # await self.facebook.send_message(message=f"... {message} ...")
276
+ # except Exception as e:
277
+ # logger.error(f"[RERANK][FACEBOOK] Error sending batch message: {e}")
278
  matches = reranked
279
  except Exception as e:
280
  logger.error(f"[RERANK] Lỗi khi rerank: {e}")
 
382
 
383
  async def create_facebook_post(self, page_token: str, sender_id: str, history: List[Dict[str, Any]]) -> str:
384
  logger.info(f"[MOCK] Creating Facebook post for sender_id={sender_id} with history={history}")
385
+ return "https://facebook.com/mock_post_url"
386
+
387
+ def format_for_facebook(text: str) -> str:
388
+ # 1. Thay bullet markdown bằng ký hiệu khác
389
+ text = text.replace('\n* ', '\n- ')
390
+ text = text.replace('\n * ', '\n + ')
391
+ text = text.replace('\n* ', '\n- ')
392
+ text = text.replace('\n * ', '\n + ')
393
+ # 2. Chuyển **text** hoặc __text__ thành *text*
394
+ text = re.sub(r'\*\*([^\*]+)\*\*', r'*\1*', text)
395
+ text = re.sub(r'__([^_]+)__', r'*\1*', text)
396
+ # 3. Loại bỏ các markdown không hỗ trợ khác nếu cần
397
+ return text