Fred808 commited on
Commit
776356e
·
verified ·
1 Parent(s): 2db4821

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -42
app.py CHANGED
@@ -391,6 +391,30 @@ async def process_order_flow(user_id: str, message: str) -> str:
391
  del user_state[user_id]
392
  state = None
393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  # Trigger initial order flow when user types "order" or "menu"
395
  if message.lower() in ["order", "menu"]:
396
  state = ConversationState()
@@ -402,7 +426,6 @@ async def process_order_flow(user_id: str, message: str) -> str:
402
  return "Sure! What dish would you like to order?"
403
  return ""
404
 
405
- # If no state exists but message contains "order", start a new order flow.
406
  if not state and "order" in message.lower():
407
  state = ConversationState()
408
  state.flow = "order"
@@ -411,49 +434,12 @@ async def process_order_flow(user_id: str, message: str) -> str:
411
  user_state[user_id] = state
412
  return "Sure! What dish would you like to order?"
413
 
414
- # If we're in order flow and we've already stored candidate dishes,
415
- # then check if the user clarified selection.
416
- if state and state.flow == "order" and "candidate_dishes" in state.data:
417
- # Accept "both" (or "all") as selecting all candidate dishes.
418
- if message.strip().lower() in ["both", "all"]:
419
- state.data["dishes"] = state.data["candidate_dishes"]
420
- # Clear candidate_dishes now that selection is made.
421
- del state.data["candidate_dishes"]
422
- state.step = 2 # Ask for quantity details.
423
- dishes_str = ", ".join(state.data["dishes"])
424
- return f"You have selected: {dishes_str}. How many servings of each would you like? " \
425
- f"(For example, '2 for Jollof Rice and 3 for Fried Rice')"
426
- else:
427
- # If the user specifies one dish from the candidate list, use that.
428
- for dish in state.data["candidate_dishes"]:
429
- if dish.lower() in message.lower():
430
- state.data["dish"] = dish
431
- del state.data["candidate_dishes"]
432
- # Proceed to extract quantity
433
- numbers = re.findall(r'\d+', message)
434
- if numbers:
435
- quantity = int(numbers[0])
436
- if quantity <= 0:
437
- return "Please enter a valid quantity (e.g., 1, 2, 3)."
438
- state.data["quantity"] = quantity
439
- state.step = 3
440
- # Continue with contact details extraction...
441
- return (f"You selected {dish} with {quantity} serving(s). "
442
- "Please provide your phone number and delivery address.")
443
- else:
444
- state.step = 2
445
- return f"You selected {dish}. How many servings would you like?"
446
- # If no candidate dish is clearly specified, ask for clarification again.
447
- dish_options = ", ".join(state.data["candidate_dishes"])
448
- return f"Please specify which one you'd like to order from: {dish_options} " \
449
- f"(or type 'both' if you'd like to order all)."
450
-
451
- # When not already in order flow, try to detect dish(es) using fuzzy matching.
452
  if not state or state.flow != "order":
453
  matched_dishes = match_dishes(message)
454
  if matched_dishes:
455
  if len(matched_dishes) > 1:
456
- # Store candidate dishes in state for later clarification.
457
  state = ConversationState()
458
  state.flow = "order"
459
  state.update_last_active()
@@ -470,7 +456,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
470
  state.update_last_active()
471
  user_state[user_id] = state
472
 
473
- # Extract quantity (e.g. "Jollof of 2 portions")
474
  numbers = re.findall(r'\d+', message)
475
  if numbers:
476
  quantity = int(numbers[0])
@@ -478,7 +464,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
478
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
479
  state.data["quantity"] = quantity
480
  state.step = 3
481
- # Extract phone and address if provided.
482
  phone_pattern = r'(\+?\d{10,15})'
483
  phone_match = re.search(phone_pattern, message)
484
  address = None
@@ -510,6 +496,40 @@ async def process_order_flow(user_id: str, message: str) -> str:
510
  return f"You selected {found_dish}. How many servings would you like?"
511
  else:
512
  return "I couldn't identify the dish. Please type the dish name from our menu."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513
  if state.step == 2:
514
  numbers = re.findall(r'\d+', message)
515
  if not numbers:
 
391
  del user_state[user_id]
392
  state = None
393
 
394
+ # If we're already expecting quantity details (step 2), parse that.
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
+ # Get candidate dishes from either multiple selection or a single dish.
401
+ candidate_dishes = state.data.get("dishes")
402
+ if not candidate_dishes:
403
+ # If only one dish was selected, candidate_dishes is the single dish.
404
+ candidate_dishes = [state.data.get("dish")]
405
+ for quantity, dish_text in pairs:
406
+ dish_text = dish_text.strip().lower()
407
+ for candidate in candidate_dishes:
408
+ # If candidate matches the text (case-insensitive substring match)
409
+ if candidate.lower() in dish_text or dish_text in candidate.lower():
410
+ order_quantities[candidate] = int(quantity)
411
+ if order_quantities:
412
+ state.data["orders"] = order_quantities
413
+ state.step = 3 # Move to next step (e.g. collecting contact details)
414
+ order_summary = "\n".join([f"{q} serving(s) of {d}" for d, q in order_quantities.items()])
415
+ return f"Got it. You have ordered:\n{order_summary}\nPlease provide your phone number and delivery address."
416
+ return "I'm sorry, I didn't understand the quantity details. Please specify like '2 for Jollof Rice and 3 for Fried Rice'."
417
+
418
  # Trigger initial order flow when user types "order" or "menu"
419
  if message.lower() in ["order", "menu"]:
420
  state = ConversationState()
 
426
  return "Sure! What dish would you like to order?"
427
  return ""
428
 
 
429
  if not state and "order" in message.lower():
430
  state = ConversationState()
431
  state.flow = "order"
 
434
  user_state[user_id] = state
435
  return "Sure! What dish would you like to order?"
436
 
437
+ # If not already in order flow, try to detect dish(es) using fuzzy matching.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
  if not state or state.flow != "order":
439
  matched_dishes = match_dishes(message)
440
  if matched_dishes:
441
  if len(matched_dishes) > 1:
442
+ # Store candidate dishes and ask user to clarify.
443
  state = ConversationState()
444
  state.flow = "order"
445
  state.update_last_active()
 
456
  state.update_last_active()
457
  user_state[user_id] = state
458
 
459
+ # Extract quantity if provided (e.g. "Jollof of 2 portions")
460
  numbers = re.findall(r'\d+', message)
461
  if numbers:
462
  quantity = int(numbers[0])
 
464
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
465
  state.data["quantity"] = quantity
466
  state.step = 3
467
+ # Proceed with extracting phone and address (see below)
468
  phone_pattern = r'(\+?\d{10,15})'
469
  phone_match = re.search(phone_pattern, message)
470
  address = None
 
496
  return f"You selected {found_dish}. How many servings would you like?"
497
  else:
498
  return "I couldn't identify the dish. Please type the dish name from our menu."
499
+
500
+ # If we already have candidate dishes (from a multiple match), check the user clarification.
501
+ if state and state.flow == "order" and "candidate_dishes" in state.data:
502
+ if message.strip().lower() in ["both", "all"]:
503
+ # User selects all candidate dishes.
504
+ state.data["dishes"] = state.data["candidate_dishes"]
505
+ del state.data["candidate_dishes"]
506
+ state.step = 2 # Now expect quantity details for each dish.
507
+ dishes_str = ", ".join(state.data["dishes"])
508
+ return f"You have selected: {dishes_str}. How many servings of each would you like? " \
509
+ f"(For example, '2 for Jollof Rice and 3 for Fried Rice')"
510
+ else:
511
+ # Check if the message specifies one of the candidate dishes.
512
+ for dish in state.data["candidate_dishes"]:
513
+ if dish.lower() in message.lower():
514
+ state.data["dish"] = dish
515
+ del state.data["candidate_dishes"]
516
+ # Try to extract quantity.
517
+ numbers = re.findall(r'\d+', message)
518
+ if numbers:
519
+ quantity = int(numbers[0])
520
+ if quantity <= 0:
521
+ return "Please enter a valid quantity (e.g., 1, 2, 3)."
522
+ state.data["quantity"] = quantity
523
+ state.step = 3
524
+ return (f"You selected {dish} with {quantity} serving(s). "
525
+ "Please provide your phone number and delivery address.")
526
+ else:
527
+ state.step = 2
528
+ return f"You selected {dish}. How many servings would you like?"
529
+ # If still unclear, ask for clarification again.
530
+ dish_options = ", ".join(state.data["candidate_dishes"])
531
+ return f"Please specify which one you'd like to order from: {dish_options} " \
532
+ f"(or type 'both' if you'd like to order all)."
533
  if state.step == 2:
534
  numbers = re.findall(r'\d+', message)
535
  if not numbers: