Fred808 commited on
Commit
39af76e
·
verified ·
1 Parent(s): dfd8121

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -2
app.py CHANGED
@@ -303,6 +303,8 @@ async def update_user_profile(user_id: str, phone_number: str = None, address: s
303
  await session.commit()
304
  return profile
305
 
 
 
306
  def process_order_flow(user_id: str, message: str) -> str:
307
  """
308
  Implements an FSM-based order flow with shipping cost calculation.
@@ -335,7 +337,7 @@ def process_order_flow(user_id: str, message: str) -> str:
335
  user_state[user_id] = state
336
  return "Sure! What dish would you like to order?"
337
 
338
- # --- New Logic: Parse Dish and Quantity in a Single Message ---
339
  if not state or state.flow != "order":
340
  # Check if the message contains a dish and quantity
341
  dish_candidates = [item["name"] for item in menu_items]
@@ -357,7 +359,38 @@ def process_order_flow(user_id: str, message: str) -> str:
357
  state.data["quantity"] = quantity
358
  state.update_last_active()
359
  user_state[user_id] = state
360
- return f"You selected {found_dish} with {quantity} serving(s). Please provide your phone number and delivery address."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
 
362
  if state and state.flow == "order":
363
  state.update_last_active()
@@ -563,6 +596,7 @@ def process_order_flow(user_id: str, message: str) -> str:
563
  return "Order canceled. Let me know if you'd like to try again."
564
 
565
  return ""
 
566
  # --- User Profile Functions ---
567
  async def get_or_create_user_profile(user_id: str, phone_number: str = None) -> UserProfile:
568
  async with async_session() as session:
 
303
  await session.commit()
304
  return profile
305
 
306
+ # ... (rest of your imports and setup)
307
+
308
  def process_order_flow(user_id: str, message: str) -> str:
309
  """
310
  Implements an FSM-based order flow with shipping cost calculation.
 
337
  user_state[user_id] = state
338
  return "Sure! What dish would you like to order?"
339
 
340
+ # --- New Logic: Parse Dish, Quantity, Phone Number, and Address in a Single Message ---
341
  if not state or state.flow != "order":
342
  # Check if the message contains a dish and quantity
343
  dish_candidates = [item["name"] for item in menu_items]
 
359
  state.data["quantity"] = quantity
360
  state.update_last_active()
361
  user_state[user_id] = state
362
+
363
+ # Extract phone number and address from the message
364
+ phone_pattern = r'(\+?\d{10,15})'
365
+ phone_match = re.search(phone_pattern, message)
366
+ address = None
367
+ if phone_match:
368
+ phone_number = phone_match.group(1)
369
+ # Assume the address starts after the phone number
370
+ address_start = phone_match.end()
371
+ address = message[address_start:].strip()
372
+ # Remove any leading/trailing commas or spaces
373
+ address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
374
+
375
+ if phone_match and address:
376
+ state.data["phone_number"] = phone_number
377
+ state.data["address"] = address
378
+ # Save phone number and address to the user's profile
379
+ asyncio.create_task(update_user_profile(user_id, phone_number, address))
380
+ # Calculate shipping cost based on the address
381
+ shipping_cost = calculate_shipping_cost(address)
382
+ state.data["shipping_cost"] = shipping_cost
383
+ state.step = 5
384
+ return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
385
+ f"Your delivery address is: {address}.\n"
386
+ f"Your delivery cost is N{shipping_cost}. Would you like to add any extras such as sides or drinks? (yes/no)")
387
+ elif phone_match:
388
+ state.data["phone_number"] = phone_match.group(1)
389
+ # Save phone number to the user's profile
390
+ asyncio.create_task(update_user_profile(user_id, phone_number))
391
+ return "Thank you. Please provide your delivery address."
392
+ else:
393
+ return "Please provide both your phone number and delivery address. For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'."
394
 
395
  if state and state.flow == "order":
396
  state.update_last_active()
 
596
  return "Order canceled. Let me know if you'd like to try again."
597
 
598
  return ""
599
+
600
  # --- User Profile Functions ---
601
  async def get_or_create_user_profile(user_id: str, phone_number: str = None) -> UserProfile:
602
  async with async_session() as session: