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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -26
app.py CHANGED
@@ -508,33 +508,50 @@ async def process_order_flow(user_id: str, message: str) -> str:
508
  user_state[user_id] = state
509
  return "Sure! What dish would you like to order?"
510
 
511
- # 2) If we are not already in the order flow, detect dish(es) from the message.
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
- # Try to parse quantity, phone, address from the same message.
524
- single_parse = _parse_single_dish_line(message, found_dish)
525
- if single_parse["quantity"]:
526
- state.data["quantity"] = single_parse["quantity"]
527
  state.step = 3
528
  else:
 
529
  state.step = 2
530
- if single_parse["phone"]:
531
- state.data["phone_number"] = single_parse["phone"]
532
- if single_parse["address"]:
533
- state.data["address"] = single_parse["address"]
 
 
 
534
  if state.step == 2 and not state.data.get("quantity"):
 
535
  return f"You selected {found_dish}. How many servings would you like?"
536
  elif state.step == 3:
 
537
  if state.data.get("phone_number") and state.data.get("address"):
 
538
  shipping_cost = calculate_shipping_cost(state.data["address"])
539
  state.data["shipping_cost"] = shipping_cost
540
  state.step = 5
@@ -545,21 +562,9 @@ async def process_order_flow(user_id: str, message: str) -> str:
545
  return "Thank you. Please provide your delivery address."
546
  else:
547
  return "Please provide your phone number and address."
548
- else:
549
- # Multiple dishes detected – support unlimited dishes.
550
- state = ConversationState()
551
- state.flow = "order"
552
- state.update_last_active()
553
- user_state[user_id] = state
554
- state.data["dishes"] = matched # Store all matched dishes
555
- state.step = 2
556
- dish_options = ", ".join(matched)
557
- return (f"You have selected the following dishes: {dish_options}.\n"
558
- "How many servings of each would you like? "
559
- "For example: '2 for Jollof Rice, 3 for Chicken, 1 for Fish'")
560
- else:
561
- return "I couldn't identify the dish. Please type the dish name from our menu."
562
-
563
 
564
  # 3) If state exists and we're at step 2: parse quantity details.
565
  if state and state.flow == "order" and state.step == 2:
 
508
  user_state[user_id] = state
509
  return "Sure! What dish would you like to order?"
510
 
511
+ # 2) If we aren't in the order flow yet, try to detect dish(es)
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
  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: