Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1720,27 +1720,21 @@ async def process_incoming_message(from_number: str, msg: dict):
|
|
1720 |
if not message_body:
|
1721 |
return
|
1722 |
|
1723 |
-
# Check for greetings
|
1724 |
if is_greeting(message_body):
|
1725 |
-
|
1726 |
-
|
1727 |
-
|
1728 |
-
|
1729 |
-
|
1730 |
-
|
1731 |
-
|
1732 |
-
|
1733 |
-
|
1734 |
-
|
1735 |
-
|
1736 |
-
from_number,
|
1737 |
-
current_state='main_menu',
|
1738 |
-
current_menu='main_menu',
|
1739 |
-
current_menu_options=list(MENU_CONFIG['main_menu']['option_descriptions'].values())
|
1740 |
-
)
|
1741 |
-
return
|
1742 |
|
1743 |
-
# π― PRIORITY
|
1744 |
# Check for "main" command - now works for both text and voice
|
1745 |
if current_state != 'main_menu' and current_state != 'ai_chat_mode': # Only check for main if not already in main menu and not in AI chat mode
|
1746 |
mapped_navigation = process_intelligent_voice_command(message_body, current_state, user_context)
|
@@ -1768,7 +1762,7 @@ async def process_incoming_message(from_number: str, msg: dict):
|
|
1768 |
)
|
1769 |
return
|
1770 |
|
1771 |
-
# π― PRIORITY
|
1772 |
if current_state == 'contact_request':
|
1773 |
await handle_contact_request_response(from_number, message_body)
|
1774 |
return
|
@@ -1779,7 +1773,7 @@ async def process_incoming_message(from_number: str, msg: dict):
|
|
1779 |
await handle_ai_chat_mode(from_number, message_body, reply_language)
|
1780 |
return
|
1781 |
|
1782 |
-
# π― PRIORITY
|
1783 |
if current_state in ['main_menu', 'category_selection_menu', 'category_products_menu', 'all_products_menu', 'product_inquiry', 'intelligent_products_menu']:
|
1784 |
# Validate menu selection
|
1785 |
is_valid, error_msg = validate_menu_selection(message_body, current_state, user_context)
|
@@ -1886,21 +1880,21 @@ async def process_incoming_message(from_number: str, msg: dict):
|
|
1886 |
return
|
1887 |
return # Exit after handling menu selection
|
1888 |
|
1889 |
-
# π― PRIORITY
|
1890 |
query_lower = message_body.lower().strip()
|
1891 |
if any(keyword in query_lower for keyword in ['apex', 'company', 'about', 'who', 'what is']):
|
1892 |
# Use OpenAI for company information
|
1893 |
await handle_general_query_with_ai(from_number, message_body, user_context, reply_language)
|
1894 |
return
|
1895 |
|
1896 |
-
# π― PRIORITY
|
1897 |
product_question_keywords = ['mode of action', 'dosage', 'administration', 'composition', 'indications', 'precautions', 'storage', 'how to use', 'side effects']
|
1898 |
if any(keyword in query_lower for keyword in product_question_keywords):
|
1899 |
# Use OpenAI for product-specific questions
|
1900 |
await handle_general_query_with_ai(from_number, message_body, user_context, reply_language)
|
1901 |
return
|
1902 |
|
1903 |
-
# π― PRIORITY
|
1904 |
# This ensures users can say product names like "hydropex", "respira aid plus", etc. from any menu
|
1905 |
logger.info(f"[Process] Checking for product name in message: '{message_body}' from state: {current_state}")
|
1906 |
products = get_veterinary_product_matches(message_body)
|
@@ -1995,7 +1989,7 @@ async def process_incoming_message(from_number: str, msg: dict):
|
|
1995 |
# Simple one-liner for wrong queries
|
1996 |
send_whatsjet_message(from_number, "β Please correct your question or type 'main' to go to main menu.")
|
1997 |
|
1998 |
-
# π― PRIORITY
|
1999 |
await handle_intelligent_product_inquiry(from_number, message_body, user_context, reply_language)
|
2000 |
|
2001 |
except Exception as e:
|
@@ -2369,7 +2363,7 @@ def is_greeting(text):
|
|
2369 |
'night': ['night', 'nite', 'nyt'],
|
2370 |
'how_are_you': ['how are you', 'how r u', 'how are u', 'how r you', 'howru', 'howru'],
|
2371 |
'whats_up': ['whats up', 'whats up', 'what is up', 'wassup', 'wassup', 'sup', 'sup'],
|
2372 |
-
'assalamu_alaikum': ['assalamu alaikum', 'assalam alaikum', 'assalamu alaikom', 'assalam alaikom', 'asalamu alaikum', 'asalam alaikum'],
|
2373 |
'salam': ['salam', 'salaam', 'assalam', 'assalaam', 'salaam alaikum', 'salaam alaikom'],
|
2374 |
'adaab': ['adaab', 'adaab arz hai', 'adaab arz', 'adaab arz karta hun'],
|
2375 |
'namaste': ['namaste', 'namaskar', 'pranam', 'pranaam'],
|
@@ -2393,7 +2387,7 @@ def is_greeting(text):
|
|
2393 |
r'^(morning|afternoon|evening|night|mornin|morn|aftrnoon|aftr|evnin|evn|nite|nyt)\s*$',
|
2394 |
r'^(how\s+(are\s+)?(you|u|r\s+u)|howru|howru)\s*$',
|
2395 |
r'^(whats?\s+up|wassup|sup)\s*$',
|
2396 |
-
r'^(assalamu?\s+alaik(um|om)|asalamu?\s+alaik(um|om)|salaam\s+alaik(um|om))\s*$',
|
2397 |
r'^(salam|salaam|assalam|assalaam)\s*$',
|
2398 |
r'^(adaab(?:\s+arz(?:\s+(hai|karta\s+hun))?)?)\s*$',
|
2399 |
r'^(namaste|namaskar|pranam|pranaam)\s*$',
|
|
|
1720 |
if not message_body:
|
1721 |
return
|
1722 |
|
1723 |
+
# π― PRIORITY 1: Check for greetings FIRST (before any other processing)
|
1724 |
if is_greeting(message_body):
|
1725 |
+
logger.info(f"[Process] Greeting detected: '{message_body}' -> showing welcome message")
|
1726 |
+
# Always show welcome message for greetings, regardless of current state
|
1727 |
+
welcome_msg = generate_veterinary_welcome_message()
|
1728 |
+
send_whatsjet_message(from_number, welcome_msg)
|
1729 |
+
context_manager.update_context(
|
1730 |
+
from_number,
|
1731 |
+
current_state='main_menu',
|
1732 |
+
current_menu='main_menu',
|
1733 |
+
current_menu_options=list(MENU_CONFIG['main_menu']['option_descriptions'].values())
|
1734 |
+
)
|
1735 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
1736 |
|
1737 |
+
# π― PRIORITY 2: Navigation commands - work from ANY state
|
1738 |
# Check for "main" command - now works for both text and voice
|
1739 |
if current_state != 'main_menu' and current_state != 'ai_chat_mode': # Only check for main if not already in main menu and not in AI chat mode
|
1740 |
mapped_navigation = process_intelligent_voice_command(message_body, current_state, user_context)
|
|
|
1762 |
)
|
1763 |
return
|
1764 |
|
1765 |
+
# π― PRIORITY 3: State-specific handling (contact_request, availability_request, ai_chat_mode)
|
1766 |
if current_state == 'contact_request':
|
1767 |
await handle_contact_request_response(from_number, message_body)
|
1768 |
return
|
|
|
1773 |
await handle_ai_chat_mode(from_number, message_body, reply_language)
|
1774 |
return
|
1775 |
|
1776 |
+
# π― PRIORITY 4: Menu selections - check if this is a valid menu selection for current state
|
1777 |
if current_state in ['main_menu', 'category_selection_menu', 'category_products_menu', 'all_products_menu', 'product_inquiry', 'intelligent_products_menu']:
|
1778 |
# Validate menu selection
|
1779 |
is_valid, error_msg = validate_menu_selection(message_body, current_state, user_context)
|
|
|
1880 |
return
|
1881 |
return # Exit after handling menu selection
|
1882 |
|
1883 |
+
# π― PRIORITY 5: Check for company/about queries first (before product search)
|
1884 |
query_lower = message_body.lower().strip()
|
1885 |
if any(keyword in query_lower for keyword in ['apex', 'company', 'about', 'who', 'what is']):
|
1886 |
# Use OpenAI for company information
|
1887 |
await handle_general_query_with_ai(from_number, message_body, user_context, reply_language)
|
1888 |
return
|
1889 |
|
1890 |
+
# π― PRIORITY 6: Check for product-specific questions (mode of action, dosage, etc.)
|
1891 |
product_question_keywords = ['mode of action', 'dosage', 'administration', 'composition', 'indications', 'precautions', 'storage', 'how to use', 'side effects']
|
1892 |
if any(keyword in query_lower for keyword in product_question_keywords):
|
1893 |
# Use OpenAI for product-specific questions
|
1894 |
await handle_general_query_with_ai(from_number, message_body, user_context, reply_language)
|
1895 |
return
|
1896 |
|
1897 |
+
# π― PRIORITY 7: Product names - works from ANY menu state
|
1898 |
# This ensures users can say product names like "hydropex", "respira aid plus", etc. from any menu
|
1899 |
logger.info(f"[Process] Checking for product name in message: '{message_body}' from state: {current_state}")
|
1900 |
products = get_veterinary_product_matches(message_body)
|
|
|
1989 |
# Simple one-liner for wrong queries
|
1990 |
send_whatsjet_message(from_number, "β Please correct your question or type 'main' to go to main menu.")
|
1991 |
|
1992 |
+
# π― PRIORITY 8: Default: treat as general query with intelligent product inquiry
|
1993 |
await handle_intelligent_product_inquiry(from_number, message_body, user_context, reply_language)
|
1994 |
|
1995 |
except Exception as e:
|
|
|
2363 |
'night': ['night', 'nite', 'nyt'],
|
2364 |
'how_are_you': ['how are you', 'how r u', 'how are u', 'how r you', 'howru', 'howru'],
|
2365 |
'whats_up': ['whats up', 'whats up', 'what is up', 'wassup', 'wassup', 'sup', 'sup'],
|
2366 |
+
'assalamu_alaikum': ['assalamu alaikum', 'assalam alaikum', 'assalamu alaikom', 'assalam alaikom', 'asalamu alaikum', 'asalam alaikum', 'as-salamu alaykum', 'as salam alaykum', 'assalamu alaykum', 'assalam alaykum'],
|
2367 |
'salam': ['salam', 'salaam', 'assalam', 'assalaam', 'salaam alaikum', 'salaam alaikom'],
|
2368 |
'adaab': ['adaab', 'adaab arz hai', 'adaab arz', 'adaab arz karta hun'],
|
2369 |
'namaste': ['namaste', 'namaskar', 'pranam', 'pranaam'],
|
|
|
2387 |
r'^(morning|afternoon|evening|night|mornin|morn|aftrnoon|aftr|evnin|evn|nite|nyt)\s*$',
|
2388 |
r'^(how\s+(are\s+)?(you|u|r\s+u)|howru|howru)\s*$',
|
2389 |
r'^(whats?\s+up|wassup|sup)\s*$',
|
2390 |
+
r'^(assalamu?\s+alaik(um|om)|asalamu?\s+alaik(um|om)|salaam\s+alaik(um|om)|as-salamu?\s+alaykum|as\s+salam\s+alaykum)\s*$',
|
2391 |
r'^(salam|salaam|assalam|assalaam)\s*$',
|
2392 |
r'^(adaab(?:\s+arz(?:\s+(hai|karta\s+hun))?)?)\s*$',
|
2393 |
r'^(namaste|namaskar|pranam|pranaam)\s*$',
|