Fred808 commited on
Commit
ebec99c
·
verified ·
1 Parent(s): 2f9f81f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -28
app.py CHANGED
@@ -512,46 +512,28 @@ async def process_order_flow(user_id: str, message: str) -> str:
512
  if not state or state.flow != "order":
513
  matched = match_dishes(message)
514
  if matched:
515
- # Multiple candidate dishes
516
- if len(matched) > 1:
517
- state = ConversationState()
518
- state.flow = "order"
519
- state.update_last_active()
520
- user_state[user_id] = state
521
- state.data["candidate_dishes"] = matched
522
- dish_options = ", ".join(matched)
523
- return (f"We found multiple dishes in your request: {dish_options}. "
524
- "Please specify which one you'd like to order or type 'both' if you'd like all.")
525
- else:
526
- # Single dish
527
  found_dish = matched[0]
528
  state = ConversationState()
529
  state.flow = "order"
530
  state.update_last_active()
531
  user_state[user_id] = state
532
  state.data["dish"] = found_dish
533
- # Attempt to parse quantity, phone, address if user typed them all
534
- single_dish_parse = _parse_single_dish_line(message, found_dish)
535
- if single_dish_parse["quantity"]:
536
- state.data["quantity"] = single_dish_parse["quantity"]
537
  state.step = 3
538
  else:
539
- # If no quantity found, we still need quantity
540
  state.step = 2
541
-
542
- if single_dish_parse["phone"]:
543
- state.data["phone_number"] = single_dish_parse["phone"]
544
- if single_dish_parse["address"]:
545
- state.data["address"] = single_dish_parse["address"]
546
-
547
- # Now see how much info we have:
548
  if state.step == 2 and not state.data.get("quantity"):
549
- # We must ask for quantity
550
  return f"You selected {found_dish}. How many servings would you like?"
551
  elif state.step == 3:
552
- # We have a quantity. Check if we also have phone and address
553
  if state.data.get("phone_number") and state.data.get("address"):
554
- # We have everything needed to skip to step 5
555
  shipping_cost = calculate_shipping_cost(state.data["address"])
556
  state.data["shipping_cost"] = shipping_cost
557
  state.step = 5
@@ -562,9 +544,42 @@ async def process_order_flow(user_id: str, message: str) -> str:
562
  return "Thank you. Please provide your delivery address."
563
  else:
564
  return "Please provide your phone number and address."
 
 
 
 
 
 
 
 
 
 
 
565
  else:
566
- # No dish matched
567
  return "I couldn't identify the dish. Please type the dish name from our menu."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
 
569
  # 3) If state exists and we're at step 2: parse quantity details.
570
  if state and state.flow == "order" and state.step == 2:
 
512
  if not state or state.flow != "order":
513
  matched = match_dishes(message)
514
  if matched:
515
+ if len(matched) == 1:
516
+ # Single dish order
 
 
 
 
 
 
 
 
 
 
517
  found_dish = matched[0]
518
  state = ConversationState()
519
  state.flow = "order"
520
  state.update_last_active()
521
  user_state[user_id] = state
522
  state.data["dish"] = found_dish
523
+ single_parse = _parse_single_dish_line(message, found_dish)
524
+ if single_parse["quantity"]:
525
+ state.data["quantity"] = single_parse["quantity"]
 
526
  state.step = 3
527
  else:
 
528
  state.step = 2
529
+ if single_parse["phone"]:
530
+ state.data["phone_number"] = single_parse["phone"]
531
+ if single_parse["address"]:
532
+ state.data["address"] = single_parse["address"]
 
 
 
533
  if state.step == 2 and not state.data.get("quantity"):
 
534
  return f"You selected {found_dish}. How many servings would you like?"
535
  elif state.step == 3:
 
536
  if state.data.get("phone_number") and state.data.get("address"):
 
537
  shipping_cost = calculate_shipping_cost(state.data["address"])
538
  state.data["shipping_cost"] = shipping_cost
539
  state.step = 5
 
544
  return "Thank you. Please provide your delivery address."
545
  else:
546
  return "Please provide your phone number and address."
547
+ else:
548
+ # Multiple dishes detected – store all in candidate_dishes.
549
+ state = ConversationState()
550
+ state.flow = "order"
551
+ state.update_last_active()
552
+ user_state[user_id] = state
553
+ state.data["candidate_dishes"] = matched
554
+ state.step = 2
555
+ dish_options = ", ".join(matched)
556
+ return (f"We found multiple dishes in your request: {dish_options}. "
557
+ "Please specify which one you'd like to order or type 'both' if you'd like all.")
558
  else:
 
559
  return "I couldn't identify the dish. Please type the dish name from our menu."
560
+
561
+ # --- Candidate Dishes Clarification Branch ---
562
+ if state and state.flow == "order" and "candidate_dishes" in state.data:
563
+ normalized = message.strip().lower()
564
+ if normalized in ["both", "all"]:
565
+ state.data["dishes"] = state.data["candidate_dishes"]
566
+ del state.data["candidate_dishes"]
567
+ state.step = 2
568
+ dishes_str = ", ".join(state.data["dishes"])
569
+ return (f"You have selected: {dishes_str}. How many servings of each would you like? "
570
+ "(For example, '2 for Jollof Rice, 3 for Chicken Wings, 1 for Fish')")
571
+ else:
572
+ for dish in state.data["candidate_dishes"]:
573
+ if dish.lower() in normalized:
574
+ state.data["dish"] = dish
575
+ del state.data["candidate_dishes"]
576
+ state.step = 2
577
+ return f"You selected {dish}. How many servings would you like?"
578
+ dish_options = ", ".join(state.data["candidate_dishes"])
579
+ return (f"Please specify which one you'd like to order from: {dish_options} "
580
+ "(or type 'both' if you'd like to order all).")
581
+
582
+
583
 
584
  # 3) If state exists and we're at step 2: parse quantity details.
585
  if state and state.flow == "order" and state.step == 2: