zenaight commited on
Commit ·
e2e4cf7
1
Parent(s): 9991703
Enhance property suggestion formatting and message handling
Browse files- Updated `process_message` to include visual separators in property suggestion responses, improving readability and user engagement.
- Implemented message splitting in `send_whatsapp_message` to handle long messages, ensuring compliance with WhatsApp's character limits.
- Added a helper function `_split_message` to efficiently divide lengthy messages while preserving content structure, enhancing overall message delivery experience.
- ai_chat.py +47 -7
- whatsapp.py +45 -1
ai_chat.py
CHANGED
|
@@ -436,7 +436,17 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
|
|
| 436 |
for msg in property_msgs:
|
| 437 |
print(f"Property suggestion: {msg}")
|
| 438 |
if property_msgs:
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
return "Let me know if you'd like to see images or more details!"
|
| 441 |
|
| 442 |
# Check if user is asking about property suggestions/results in general
|
|
@@ -446,7 +456,17 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
|
|
| 446 |
for msg in property_msgs:
|
| 447 |
print(f"Property suggestion: {msg}")
|
| 448 |
if property_msgs:
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
return "Let me know if you'd like to see images or more details!"
|
| 451 |
|
| 452 |
# Handle confusion or clarification requests
|
|
@@ -465,10 +485,20 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
|
|
| 465 |
for msg in property_msgs:
|
| 466 |
# Send each property message (simulate WhatsApp message send)
|
| 467 |
print(f"Property suggestion: {msg}")
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
# Handle greetings with persona context
|
| 474 |
if is_greeting and updated_persona.get("location_preference"):
|
|
@@ -502,7 +532,17 @@ async def process_message(user_message: str, user_info: dict = None, session_id:
|
|
| 502 |
for msg in property_msgs:
|
| 503 |
print(f"Property suggestion: {msg}")
|
| 504 |
if property_msgs:
|
| 505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
return "Let me know if you'd like to see images or more details!"
|
| 507 |
|
| 508 |
elif any(word in user_response_lower for word in negative_words):
|
|
|
|
| 436 |
for msg in property_msgs:
|
| 437 |
print(f"Property suggestion: {msg}")
|
| 438 |
if property_msgs:
|
| 439 |
+
# Add some visual separators to make it more readable
|
| 440 |
+
formatted_messages = []
|
| 441 |
+
for i, msg in enumerate(property_msgs):
|
| 442 |
+
if i == 0: # Intro message
|
| 443 |
+
formatted_messages.append(msg)
|
| 444 |
+
elif i == len(property_msgs) - 1: # Last message (follow-up)
|
| 445 |
+
formatted_messages.append(f"\n{msg}")
|
| 446 |
+
else: # Property descriptions
|
| 447 |
+
formatted_messages.append(f"\n🏢 **Property {i}**\n{msg}")
|
| 448 |
+
|
| 449 |
+
return "\n\n".join(formatted_messages)
|
| 450 |
return "Let me know if you'd like to see images or more details!"
|
| 451 |
|
| 452 |
# Check if user is asking about property suggestions/results in general
|
|
|
|
| 456 |
for msg in property_msgs:
|
| 457 |
print(f"Property suggestion: {msg}")
|
| 458 |
if property_msgs:
|
| 459 |
+
# Add some visual separators to make it more readable
|
| 460 |
+
formatted_messages = []
|
| 461 |
+
for i, msg in enumerate(property_msgs):
|
| 462 |
+
if i == 0: # Intro message
|
| 463 |
+
formatted_messages.append(msg)
|
| 464 |
+
elif i == len(property_msgs) - 1: # Last message (follow-up)
|
| 465 |
+
formatted_messages.append(f"\n{msg}")
|
| 466 |
+
else: # Property descriptions
|
| 467 |
+
formatted_messages.append(f"\n🏢 **Property {i}**\n{msg}")
|
| 468 |
+
|
| 469 |
+
return "\n\n".join(formatted_messages)
|
| 470 |
return "Let me know if you'd like to see images or more details!"
|
| 471 |
|
| 472 |
# Handle confusion or clarification requests
|
|
|
|
| 485 |
for msg in property_msgs:
|
| 486 |
# Send each property message (simulate WhatsApp message send)
|
| 487 |
print(f"Property suggestion: {msg}")
|
| 488 |
+
# Return all messages as a single response (they will be sent sequentially)
|
| 489 |
+
if property_msgs:
|
| 490 |
+
# Add some visual separators to make it more readable
|
| 491 |
+
formatted_messages = []
|
| 492 |
+
for i, msg in enumerate(property_msgs):
|
| 493 |
+
if i == 0: # Intro message
|
| 494 |
+
formatted_messages.append(msg)
|
| 495 |
+
elif i == len(property_msgs) - 1: # Last message (follow-up)
|
| 496 |
+
formatted_messages.append(f"\n{msg}")
|
| 497 |
+
else: # Property descriptions
|
| 498 |
+
formatted_messages.append(f"\n🏢 **Property {i}**\n{msg}")
|
| 499 |
+
|
| 500 |
+
return "\n\n".join(formatted_messages)
|
| 501 |
+
return "Let me know if you'd like to see images or more details!"
|
| 502 |
|
| 503 |
# Handle greetings with persona context
|
| 504 |
if is_greeting and updated_persona.get("location_preference"):
|
|
|
|
| 532 |
for msg in property_msgs:
|
| 533 |
print(f"Property suggestion: {msg}")
|
| 534 |
if property_msgs:
|
| 535 |
+
# Add some visual separators to make it more readable
|
| 536 |
+
formatted_messages = []
|
| 537 |
+
for i, msg in enumerate(property_msgs):
|
| 538 |
+
if i == 0: # Intro message
|
| 539 |
+
formatted_messages.append(msg)
|
| 540 |
+
elif i == len(property_msgs) - 1: # Last message (follow-up)
|
| 541 |
+
formatted_messages.append(f"\n{msg}")
|
| 542 |
+
else: # Property descriptions
|
| 543 |
+
formatted_messages.append(f"\n🏢 **Property {i}**\n{msg}")
|
| 544 |
+
|
| 545 |
+
return "\n\n".join(formatted_messages)
|
| 546 |
return "Let me know if you'd like to see images or more details!"
|
| 547 |
|
| 548 |
elif any(word in user_response_lower for word in negative_words):
|
whatsapp.py
CHANGED
|
@@ -3,6 +3,23 @@ from config import PHONE_NUMBER_ID, WHATSAPP_API_TOKEN
|
|
| 3 |
|
| 4 |
async def send_whatsapp_message(wa_id: str, message: str):
|
| 5 |
"""Send a message via WhatsApp Business API"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
url = f"https://graph.facebook.com/v18.0/{PHONE_NUMBER_ID}/messages"
|
| 7 |
headers = {
|
| 8 |
"Authorization": f"Bearer {WHATSAPP_API_TOKEN}",
|
|
@@ -21,4 +38,31 @@ async def send_whatsapp_message(wa_id: str, message: str):
|
|
| 21 |
return resp.status_code == 200
|
| 22 |
except Exception as e:
|
| 23 |
print(f"Failed to send message: {e}")
|
| 24 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
async def send_whatsapp_message(wa_id: str, message: str):
|
| 5 |
"""Send a message via WhatsApp Business API"""
|
| 6 |
+
# Split message if it's too long (WhatsApp limit is ~4096 characters)
|
| 7 |
+
max_length = 3000 # Leave some buffer
|
| 8 |
+
|
| 9 |
+
if len(message) <= max_length:
|
| 10 |
+
# Single message
|
| 11 |
+
return await _send_single_message(wa_id, message)
|
| 12 |
+
else:
|
| 13 |
+
# Split into multiple messages
|
| 14 |
+
messages = _split_message(message, max_length)
|
| 15 |
+
success = True
|
| 16 |
+
for msg in messages:
|
| 17 |
+
if not await _send_single_message(wa_id, msg):
|
| 18 |
+
success = False
|
| 19 |
+
return success
|
| 20 |
+
|
| 21 |
+
async def _send_single_message(wa_id: str, message: str):
|
| 22 |
+
"""Send a single message via WhatsApp Business API"""
|
| 23 |
url = f"https://graph.facebook.com/v18.0/{PHONE_NUMBER_ID}/messages"
|
| 24 |
headers = {
|
| 25 |
"Authorization": f"Bearer {WHATSAPP_API_TOKEN}",
|
|
|
|
| 38 |
return resp.status_code == 200
|
| 39 |
except Exception as e:
|
| 40 |
print(f"Failed to send message: {e}")
|
| 41 |
+
return False
|
| 42 |
+
|
| 43 |
+
def _split_message(message: str, max_length: int):
|
| 44 |
+
"""Split a long message into smaller chunks"""
|
| 45 |
+
if len(message) <= max_length:
|
| 46 |
+
return [message]
|
| 47 |
+
|
| 48 |
+
# Split by double newlines first (to preserve property separations)
|
| 49 |
+
parts = message.split('\n\n')
|
| 50 |
+
messages = []
|
| 51 |
+
current_message = ""
|
| 52 |
+
|
| 53 |
+
for part in parts:
|
| 54 |
+
# If adding this part would exceed limit, start a new message
|
| 55 |
+
if len(current_message) + len(part) + 2 > max_length and current_message:
|
| 56 |
+
messages.append(current_message.strip())
|
| 57 |
+
current_message = part
|
| 58 |
+
else:
|
| 59 |
+
if current_message:
|
| 60 |
+
current_message += '\n\n' + part
|
| 61 |
+
else:
|
| 62 |
+
current_message = part
|
| 63 |
+
|
| 64 |
+
# Add the last message
|
| 65 |
+
if current_message:
|
| 66 |
+
messages.append(current_message.strip())
|
| 67 |
+
|
| 68 |
+
return messages
|