DreamStream-1 commited on
Commit
d2c7424
Β·
verified Β·
1 Parent(s): e904380

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -87
app.py CHANGED
@@ -1735,8 +1735,8 @@ async def process_incoming_message(from_number: str, msg: dict):
1735
  )
1736
  return
1737
 
 
1738
  # Check for "main" command - now works for both text and voice
1739
- # First check if it's a navigation command from voice (but not in AI chat mode)
1740
  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
1741
  mapped_navigation = process_intelligent_voice_command(message_body, current_state, user_context)
1742
  if mapped_navigation == 'main':
@@ -1751,7 +1751,7 @@ async def process_incoming_message(from_number: str, msg: dict):
1751
  )
1752
  return
1753
 
1754
- # Also check for text-based main commands
1755
  if message_body.lower() in ['main', 'menu', 'start', 'home', 'back']:
1756
  welcome_msg = generate_veterinary_welcome_message()
1757
  send_whatsjet_message(from_number, welcome_msg)
@@ -1763,87 +1763,7 @@ async def process_incoming_message(from_number: str, msg: dict):
1763
  )
1764
  return
1765
 
1766
- # 🎯 PRIORITY: Check for product names FIRST - works from ANY menu state
1767
- # This ensures users can say product names like "hydropex", "respira aid plus", etc. from any menu
1768
- logger.info(f"[Process] Checking for product name in message: '{message_body}' from state: {current_state}")
1769
- products = get_veterinary_product_matches(message_body)
1770
-
1771
- # --- NEW LOGIC: Check for exact match first ---
1772
- normalized_input = normalize(message_body).lower().strip()
1773
- exact_match = None
1774
- for product in products:
1775
- product_name = product.get('Product Name', '')
1776
- normalized_product_name = normalize(product_name).lower().strip()
1777
- if normalized_product_name == normalized_input:
1778
- exact_match = product
1779
- break
1780
-
1781
- if exact_match:
1782
- logger.info(f"[Process] Exact product match found: {exact_match.get('Product Name', 'Unknown')}")
1783
- context_manager.update_context(
1784
- from_number,
1785
- current_product=exact_match,
1786
- current_state='product_inquiry',
1787
- current_menu='product_inquiry',
1788
- current_menu_options=list(MENU_CONFIG['product_inquiry']['option_descriptions'].values())
1789
- )
1790
- # Only send one reply: image+caption if possible, else text
1791
- await send_product_image_with_caption(from_number, exact_match, user_context)
1792
- return
1793
-
1794
- # --- END NEW LOGIC ---
1795
-
1796
- if products:
1797
- logger.info(f"[Process] Product name detected: '{message_body}' -> Found {len(products)} products")
1798
- # If single product found, show it directly
1799
- if len(products) == 1:
1800
- selected_product = products[0]
1801
- product_name = selected_product.get('Product Name', 'Unknown')
1802
- logger.info(f"[Process] Single product found: {product_name}")
1803
- context_manager.update_context(
1804
- from_number,
1805
- current_product=selected_product,
1806
- current_state='product_inquiry',
1807
- current_menu='product_inquiry',
1808
- current_menu_options=list(MENU_CONFIG['product_inquiry']['option_descriptions'].values())
1809
- )
1810
- # Always send image+caption if possible, else text
1811
- logger.info(f"[DEBUG] Calling send_product_image_with_caption for: {product_name}")
1812
- await send_product_image_with_caption(from_number, selected_product, user_context)
1813
- return
1814
-
1815
- else:
1816
- # Enhanced "not found" response with veterinary suggestions
1817
- message = (
1818
- "❌ *Product Not Found*\n\n"
1819
- f"πŸ” *We couldn't find '{message_body}' in our veterinary database.*\n\n"
1820
- "πŸ’‘ *Try these alternatives:*\n"
1821
- "β€’ Check spelling (e.g., 'Hydropex' not 'Hydro pex')\n"
1822
- "β€’ Search by symptoms (e.g., 'respiratory', 'liver support')\n"
1823
- "β€’ Search by category (e.g., 'antibiotic', 'vitamin')\n"
1824
- "β€’ Search by species (e.g., 'poultry', 'livestock')\n\n"
1825
- "πŸ₯ *Popular Veterinary Products:*\n"
1826
- "β€’ Hydropex (Electrolyte supplement)\n"
1827
- "β€’ Heposel (Liver tonic)\n"
1828
- "β€’ Bromacid (Respiratory support)\n"
1829
- "β€’ Tribiotic (Antibiotic)\n"
1830
- "β€’ Symodex (Multivitamin)\n\n"
1831
- "πŸ’¬ *Type 'main' to return to main menu or try another search.*"
1832
- )
1833
-
1834
- # Translate response if needed
1835
- if reply_language == 'ur':
1836
- try:
1837
- translated_message = GoogleTranslator(source='auto', target='ur').translate(message)
1838
- send_whatsjet_message(from_number, translated_message)
1839
- except Exception as e:
1840
- logger.error(f"[AI] Translation error: {e}")
1841
- send_whatsjet_message(from_number, message)
1842
- else:
1843
- send_whatsjet_message(from_number, message)
1844
-
1845
- # Handle menu state-based processing
1846
- # Check if this is a menu selection
1847
  if current_state in ['main_menu', 'category_selection_menu', 'category_products_menu', 'all_products_menu', 'product_inquiry']:
1848
  # Validate menu selection
1849
  is_valid, error_msg = validate_menu_selection(message_body, current_state, user_context)
@@ -1932,17 +1852,96 @@ async def process_incoming_message(from_number: str, msg: dict):
1932
  send_whatsjet_message(from_number, "❌ No products available. Type 'main' to return to main menu.")
1933
  elif current_state == 'product_inquiry':
1934
  await handle_veterinary_product_followup(message_body, from_number)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1935
  else:
1936
- # Invalid menu selection
1937
- send_whatsjet_message(from_number, error_msg)
1938
- elif current_state == 'contact_request':
 
1939
  await handle_contact_request_response(from_number, message_body)
1940
  elif current_state == 'availability_request':
1941
  await handle_availability_request_response(from_number, message_body)
1942
  elif current_state == 'ai_chat_mode':
1943
  await handle_ai_chat_mode(from_number, message_body, reply_language)
1944
  else:
1945
- # Default: treat as general query with intelligent product inquiry
1946
  await handle_intelligent_product_inquiry(from_number, message_body, user_context, reply_language)
1947
 
1948
  except Exception as e:
 
1735
  )
1736
  return
1737
 
1738
+ # 🎯 PRIORITY 1: Navigation commands - work from ANY state
1739
  # Check for "main" command - now works for both text and voice
 
1740
  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
1741
  mapped_navigation = process_intelligent_voice_command(message_body, current_state, user_context)
1742
  if mapped_navigation == 'main':
 
1751
  )
1752
  return
1753
 
1754
+ # Also check for text-based navigation commands
1755
  if message_body.lower() in ['main', 'menu', 'start', 'home', 'back']:
1756
  welcome_msg = generate_veterinary_welcome_message()
1757
  send_whatsjet_message(from_number, welcome_msg)
 
1763
  )
1764
  return
1765
 
1766
+ # 🎯 PRIORITY 2: Menu selections - check if this is a valid menu selection for current state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1767
  if current_state in ['main_menu', 'category_selection_menu', 'category_products_menu', 'all_products_menu', 'product_inquiry']:
1768
  # Validate menu selection
1769
  is_valid, error_msg = validate_menu_selection(message_body, current_state, user_context)
 
1852
  send_whatsjet_message(from_number, "❌ No products available. Type 'main' to return to main menu.")
1853
  elif current_state == 'product_inquiry':
1854
  await handle_veterinary_product_followup(message_body, from_number)
1855
+ return # Exit after handling menu selection
1856
+
1857
+ # 🎯 PRIORITY 3: Product names - works from ANY menu state
1858
+ # This ensures users can say product names like "hydropex", "respira aid plus", etc. from any menu
1859
+ logger.info(f"[Process] Checking for product name in message: '{message_body}' from state: {current_state}")
1860
+ products = get_veterinary_product_matches(message_body)
1861
+
1862
+ # --- NEW LOGIC: Check for exact match first ---
1863
+ normalized_input = normalize(message_body).lower().strip()
1864
+ exact_match = None
1865
+ for product in products:
1866
+ product_name = product.get('Product Name', '')
1867
+ normalized_product_name = normalize(product_name).lower().strip()
1868
+ if normalized_product_name == normalized_input:
1869
+ exact_match = product
1870
+ break
1871
+
1872
+ if exact_match:
1873
+ logger.info(f"[Process] Exact product match found: {exact_match.get('Product Name', 'Unknown')}")
1874
+ context_manager.update_context(
1875
+ from_number,
1876
+ current_product=exact_match,
1877
+ current_state='product_inquiry',
1878
+ current_menu='product_inquiry',
1879
+ current_menu_options=list(MENU_CONFIG['product_inquiry']['option_descriptions'].values())
1880
+ )
1881
+ # Only send one reply: image+caption if possible, else text
1882
+ await send_product_image_with_caption(from_number, exact_match, user_context)
1883
+ return
1884
+
1885
+ # --- END NEW LOGIC ---
1886
+
1887
+ if products:
1888
+ logger.info(f"[Process] Product name detected: '{message_body}' -> Found {len(products)} products")
1889
+ # If single product found, show it directly
1890
+ if len(products) == 1:
1891
+ selected_product = products[0]
1892
+ product_name = selected_product.get('Product Name', 'Unknown')
1893
+ logger.info(f"[Process] Single product found: {product_name}")
1894
+ context_manager.update_context(
1895
+ from_number,
1896
+ current_product=selected_product,
1897
+ current_state='product_inquiry',
1898
+ current_menu='product_inquiry',
1899
+ current_menu_options=list(MENU_CONFIG['product_inquiry']['option_descriptions'].values())
1900
+ )
1901
+ # Always send image+caption if possible, else text
1902
+ logger.info(f"[DEBUG] Calling send_product_image_with_caption for: {product_name}")
1903
+ await send_product_image_with_caption(from_number, selected_product, user_context)
1904
+ return
1905
+
1906
+ else:
1907
+ # Enhanced "not found" response with veterinary suggestions
1908
+ message = (
1909
+ "❌ *Product Not Found*\n\n"
1910
+ f"πŸ” *We couldn't find '{message_body}' in our veterinary database.*\n\n"
1911
+ "πŸ’‘ *Try these alternatives:*\n"
1912
+ "β€’ Check spelling (e.g., 'Hydropex' not 'Hydro pex')\n"
1913
+ "β€’ Search by symptoms (e.g., 'respiratory', 'liver support')\n"
1914
+ "β€’ Search by category (e.g., 'antibiotic', 'vitamin')\n"
1915
+ "β€’ Search by species (e.g., 'poultry', 'livestock')\n\n"
1916
+ "πŸ₯ *Popular Veterinary Products:*\n"
1917
+ "β€’ Hydropex (Electrolyte supplement)\n"
1918
+ "β€’ Heposel (Liver tonic)\n"
1919
+ "β€’ Bromacid (Respiratory support)\n"
1920
+ "β€’ Tribiotic (Antibiotic)\n"
1921
+ "β€’ Symodex (Multivitamin)\n\n"
1922
+ "πŸ’¬ *Type 'main' to return to main menu or try another search.*"
1923
+ )
1924
+
1925
+ # Translate response if needed
1926
+ if reply_language == 'ur':
1927
+ try:
1928
+ translated_message = GoogleTranslator(source='auto', target='ur').translate(message)
1929
+ send_whatsjet_message(from_number, translated_message)
1930
+ except Exception as e:
1931
+ logger.error(f"[AI] Translation error: {e}")
1932
+ send_whatsjet_message(from_number, message)
1933
  else:
1934
+ send_whatsjet_message(from_number, message)
1935
+
1936
+ # 🎯 PRIORITY 4: Handle other states (contact_request, availability_request, ai_chat_mode)
1937
+ if current_state == 'contact_request':
1938
  await handle_contact_request_response(from_number, message_body)
1939
  elif current_state == 'availability_request':
1940
  await handle_availability_request_response(from_number, message_body)
1941
  elif current_state == 'ai_chat_mode':
1942
  await handle_ai_chat_mode(from_number, message_body, reply_language)
1943
  else:
1944
+ # 🎯 PRIORITY 5: Default: treat as general query with intelligent product inquiry
1945
  await handle_intelligent_product_inquiry(from_number, message_body, user_context, reply_language)
1946
 
1947
  except Exception as e: