Fred808 commited on
Commit
3e3931e
·
verified ·
1 Parent(s): a37b1ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -105
app.py CHANGED
@@ -391,29 +391,6 @@ async def process_order_flow(user_id: str, message: str) -> str:
391
  del user_state[user_id]
392
  state = None
393
 
394
- # --- Step A: Already expecting quantity details? ---
395
- if state and state.flow == "order" and state.step == 2:
396
- # Expecting a message like "2 for Jollof Rice and 3 for Fried Rice"
397
- pairs = re.findall(r'(\d+)\s*for\s*([a-zA-Z\s]+)', message, flags=re.IGNORECASE)
398
- if pairs:
399
- order_quantities = {}
400
- # Use candidate dishes if available; otherwise, a single selected dish.
401
- candidate_dishes = state.data.get("dishes")
402
- if not candidate_dishes:
403
- candidate_dishes = [state.data.get("dish")]
404
- for quantity, dish_text in pairs:
405
- dish_text = dish_text.strip().lower()
406
- for candidate in candidate_dishes:
407
- if candidate.lower() in dish_text or dish_text in candidate.lower():
408
- order_quantities[candidate] = int(quantity)
409
- if order_quantities:
410
- state.data["orders"] = order_quantities
411
- state.step = 3 # Proceed to collecting contact details
412
- order_summary = "\n".join([f"{q} serving(s) of {d}" for d, q in order_quantities.items()])
413
- return f"Got it. You have ordered:\n{order_summary}\nPlease provide your phone number and delivery address."
414
- return "I'm sorry, I didn't understand the quantity details. Please specify like '2 for Jollof Rice and 3 for Fried Rice'."
415
-
416
- # --- Step B: Trigger initial order flow ---
417
  if message.lower() in ["order", "menu"]:
418
  state = ConversationState()
419
  state.flow = "order"
@@ -432,100 +409,84 @@ async def process_order_flow(user_id: str, message: str) -> str:
432
  user_state[user_id] = state
433
  return "Sure! What dish would you like to order?"
434
 
435
- # --- Step C: Identify dish(es) if not already in order flow ---
436
  if not state or state.flow != "order":
437
- matched_dishes = match_dishes(message)
438
- if matched_dishes:
439
- # If multiple dishes are detected, ask for clarification.
440
- if len(matched_dishes) > 1:
441
- state = ConversationState()
442
- state.flow = "order"
443
- state.update_last_active()
444
- state.data["candidate_dishes"] = matched_dishes
445
- user_state[user_id] = state
446
- dish_options = ", ".join(matched_dishes)
447
- return (f"We found multiple dishes in your request: {dish_options}. "
448
- "Please specify which one you'd like to order or type 'both' if you'd like all.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
  else:
450
- # Single dish case.
451
- found_dish = matched_dishes[0]
452
- state = ConversationState()
453
- state.flow = "order"
 
 
 
 
 
 
 
454
  state.data["dish"] = found_dish
455
- state.update_last_active()
456
- user_state[user_id] = state
457
- # Attempt to extract quantity (e.g. "Jollof Rice of 2 portions")
458
- numbers = re.findall(r'\d+', message)
459
  if numbers:
460
  quantity = int(numbers[0])
461
  if quantity <= 0:
462
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
463
  state.data["quantity"] = quantity
464
  state.step = 3
465
- # Proceed to contact details extraction
466
- phone_pattern = r'(\+?\d{10,15})'
467
- phone_match = re.search(phone_pattern, message)
468
- address = None
469
- if phone_match:
470
- phone_number = phone_match.group(1)
471
- address_start = phone_match.end()
472
- address = message[address_start:].strip()
473
- address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
474
- if phone_match and address:
475
- state.data["phone_number"] = phone_number
476
- state.data["address"] = address
477
- asyncio.create_task(update_user_profile(user_id, phone_number, address))
478
- shipping_cost = calculate_shipping_cost(address)
479
- state.data["shipping_cost"] = shipping_cost
480
- state.step = 5
481
- return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
482
- f"Your delivery address is: {address}.\n"
483
- f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
484
- elif phone_match:
485
- state.data["phone_number"] = phone_match.group(1)
486
- asyncio.create_task(update_user_profile(user_id, phone_match.group(1)))
487
- return "Thank you. Please provide your delivery address."
488
- else:
489
- return (f"You selected {found_dish} with {quantity} serving(s). "
490
- "Please provide both your phone number and delivery address. "
491
- "For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'.")
492
  else:
493
  state.step = 2
494
  return f"You selected {found_dish}. How many servings would you like?"
495
- else:
496
- return "I couldn't identify the dish. Please type the dish name from our menu."
497
-
498
- # --- Step D: Clarification if candidate dishes exist (multiple matches) ---
499
- if state and state.flow == "order" and "candidate_dishes" in state.data:
500
- if message.strip().lower() in ["both", "all"]:
501
- # User chooses to order all candidate dishes.
502
- state.data["dishes"] = state.data["candidate_dishes"]
503
- del state.data["candidate_dishes"]
504
- state.step = 2 # Now expect quantity details for each dish.
505
- dishes_str = ", ".join(state.data["dishes"])
506
- return f"You have selected: {dishes_str}. How many servings of each would you like? " \
507
- f"(For example, '2 for Jollof Rice and 3 for Fried Rice')"
508
- else:
509
- # Check if the message specifies one of the candidate dishes.
510
- for dish in state.data["candidate_dishes"]:
511
- if dish.lower() in message.lower():
512
- state.data["dish"] = dish
513
- del state.data["candidate_dishes"]
514
- numbers = re.findall(r'\d+', message)
515
- if numbers:
516
- quantity = int(numbers[0])
517
- if quantity <= 0:
518
- return "Please enter a valid quantity (e.g., 1, 2, 3)."
519
- state.data["quantity"] = quantity
520
- state.step = 3
521
- return (f"You selected {dish} with {quantity} serving(s). "
522
- "Please provide your phone number and delivery address.")
523
- else:
524
- state.step = 2
525
- return f"You selected {dish}. How many servings would you like?"
526
- dish_options = ", ".join(state.data["candidate_dishes"])
527
- return f"Please specify which one you'd like to order from: {dish_options} " \
528
- f"(or type 'both' if you'd like to order all)."
529
 
530
  if state.step == 3:
531
  phone_pattern = r'(\+?\d{10,15})'
 
391
  del user_state[user_id]
392
  state = None
393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  if message.lower() in ["order", "menu"]:
395
  state = ConversationState()
396
  state.flow = "order"
 
409
  user_state[user_id] = state
410
  return "Sure! What dish would you like to order?"
411
 
412
+ # Use fuzzy matching to detect a dish even with typos
413
  if not state or state.flow != "order":
414
+ found_dish = match_dish(message)
415
+ if found_dish:
416
+ state = ConversationState()
417
+ state.flow = "order"
418
+ state.data["dish"] = found_dish
419
+ state.update_last_active()
420
+ user_state[user_id] = state
421
+ numbers = re.findall(r'\d+', message)
422
+ if numbers:
423
+ quantity = int(numbers[0])
424
+ if quantity <= 0:
425
+ return "Please enter a valid quantity (e.g., 1, 2, 3)."
426
+ state.data["quantity"] = quantity
427
+ state.step = 3
428
+ phone_pattern = r'(\+?\d{10,15})'
429
+ phone_match = re.search(phone_pattern, message)
430
+ address = None
431
+ if phone_match:
432
+ phone_number = phone_match.group(1)
433
+ address_start = phone_match.end()
434
+ address = message[address_start:].strip()
435
+ address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
436
+ if phone_match and address:
437
+ state.data["phone_number"] = phone_number
438
+ state.data["address"] = address
439
+ asyncio.create_task(update_user_profile(user_id, phone_number, address))
440
+ shipping_cost = calculate_shipping_cost(address)
441
+ state.data["shipping_cost"] = shipping_cost
442
+ state.step = 5
443
+ return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
444
+ f"Your delivery address is: {address}.\n"
445
+ f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
446
+ elif phone_match:
447
+ state.data["phone_number"] = phone_match.group(1)
448
+ asyncio.create_task(update_user_profile(user_id, phone_number))
449
+ return "Thank you. Please provide your delivery address."
450
+ else:
451
+ return ("Please provide both your phone number and delivery address. "
452
+ "For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'.")
453
  else:
454
+ state.step = 2
455
+ return f"You selected {found_dish}. How many servings would you like?"
456
+
457
+ # If state exists and we're already in order flow:
458
+ if state and state.flow == "order":
459
+ state.update_last_active()
460
+ if state.step == 1:
461
+ # Use fuzzy matching on the message in case of typos.
462
+ found_dish = match_dish(message)
463
+ numbers = re.findall(r'\d+', message)
464
+ if found_dish:
465
  state.data["dish"] = found_dish
 
 
 
 
466
  if numbers:
467
  quantity = int(numbers[0])
468
  if quantity <= 0:
469
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
470
  state.data["quantity"] = quantity
471
  state.step = 3
472
+ return (f"You selected {found_dish} with {quantity} serving(s). "
473
+ "Please provide your phone number and delivery address.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474
  else:
475
  state.step = 2
476
  return f"You selected {found_dish}. How many servings would you like?"
477
+ else:
478
+ return "I couldn't identify the dish. Please type the dish name from our menu."
479
+
480
+ if state.step == 2:
481
+ numbers = re.findall(r'\d+', message)
482
+ if not numbers:
483
+ return "Please enter a valid number for the quantity (e.g., 1, 2, 3)."
484
+ quantity = int(numbers[0])
485
+ if quantity <= 0:
486
+ return "Please enter a valid quantity (e.g., 1, 2, 3)."
487
+ state.data["quantity"] = quantity
488
+ state.step = 3
489
+ return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number and delivery address."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
490
 
491
  if state.step == 3:
492
  phone_pattern = r'(\+?\d{10,15})'