Fred808 commited on
Commit
56b88aa
·
verified ·
1 Parent(s): 5240003

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -42
app.py CHANGED
@@ -335,7 +335,8 @@ async def update_user_profile_with_order(user_id: str, order_id: str):
335
 
336
  async def process_order_flow(user_id: str, message: str) -> str:
337
  state = user_state.get(user_id)
338
-
 
339
  if state and state.is_expired():
340
  state.reset()
341
  state.step = 1
@@ -344,7 +345,8 @@ async def process_order_flow(user_id: str, message: str) -> str:
344
  if message.lower() == "order":
345
  return "Sure! What dish would you like to order?"
346
  return ""
347
-
 
348
  if not state and "order" in message.lower():
349
  state = ConversationState()
350
  state.flow = "order"
@@ -352,7 +354,9 @@ async def process_order_flow(user_id: str, message: str) -> str:
352
  state.update_last_active()
353
  user_state[user_id] = state
354
  return "Sure! What dish would you like to order?"
355
-
 
 
356
  if not state or state.flow != "order":
357
  dish_candidates = [item["name"] for item in menu_items]
358
  found_dish = None
@@ -360,50 +364,60 @@ async def process_order_flow(user_id: str, message: str) -> str:
360
  if dish.lower() in message.lower():
361
  found_dish = dish
362
  break
363
- numbers = re.findall(r'\d+', message)
364
- if found_dish and numbers:
365
- quantity = int(numbers[0])
366
- if quantity <= 0:
367
- return "Please enter a valid quantity (e.g., 1, 2, 3)."
368
  state = ConversationState()
369
  state.flow = "order"
370
- state.step = 3
371
  state.data["dish"] = found_dish
372
- state.data["quantity"] = quantity
373
  state.update_last_active()
374
  user_state[user_id] = state
375
 
376
- phone_pattern = r'(\+?\d{10,15})'
377
- phone_match = re.search(phone_pattern, message)
378
- address = None
379
- if phone_match:
380
- phone_number = phone_match.group(1)
381
- address_start = phone_match.end()
382
- address = message[address_start:].strip()
383
- address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
384
-
385
- if phone_match and address:
386
- state.data["phone_number"] = phone_number
387
- state.data["address"] = address
388
- await update_user_profile(user_id, phone_number, address)
389
- shipping_cost = calculate_shipping_cost(address)
390
- state.data["shipping_cost"] = shipping_cost
391
- state.step = 5
392
- return (
393
- f"Thanks! Your phone number is recorded as: {phone_number}.\n"
394
- f"Your delivery address is: {address}.\n"
395
- f"Your delivery cost is N{shipping_cost}. Would you like to add any extras such as sides or drinks? (yes/no)"
396
- )
397
- elif phone_match:
398
- state.data["phone_number"] = phone_match.group(1)
399
- await update_user_profile(user_id, phone_number)
400
- return "Thank you. Please provide your delivery address."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401
  else:
402
- return (
403
- "Please provide both your phone number and delivery address. For example: "
404
- "'09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'."
405
- )
406
-
407
  if state and state.flow == "order":
408
  state.update_last_active()
409
  if state.step == 1:
@@ -432,8 +446,6 @@ async def process_order_flow(user_id: str, message: str) -> str:
432
  else:
433
  return "I couldn't identify the dish. Please type the dish name from our menu."
434
 
435
-
436
-
437
  if state.step == 2:
438
  numbers = re.findall(r'\d+', message)
439
  if not numbers:
 
335
 
336
  async def process_order_flow(user_id: str, message: str) -> str:
337
  state = user_state.get(user_id)
338
+
339
+ # Reset state if expired.
340
  if state and state.is_expired():
341
  state.reset()
342
  state.step = 1
 
345
  if message.lower() == "order":
346
  return "Sure! What dish would you like to order?"
347
  return ""
348
+
349
+ # If no state and the user explicitly types "order", start the order flow.
350
  if not state and "order" in message.lower():
351
  state = ConversationState()
352
  state.flow = "order"
 
354
  state.update_last_active()
355
  user_state[user_id] = state
356
  return "Sure! What dish would you like to order?"
357
+
358
+ # If there's no state or the current state is not in order flow,
359
+ # try to detect a dish name from the message.
360
  if not state or state.flow != "order":
361
  dish_candidates = [item["name"] for item in menu_items]
362
  found_dish = None
 
364
  if dish.lower() in message.lower():
365
  found_dish = dish
366
  break
367
+
368
+ if found_dish:
369
+ # Initialize a new order state.
 
 
370
  state = ConversationState()
371
  state.flow = "order"
 
372
  state.data["dish"] = found_dish
 
373
  state.update_last_active()
374
  user_state[user_id] = state
375
 
376
+ numbers = re.findall(r'\d+', message)
377
+ if numbers:
378
+ quantity = int(numbers[0])
379
+ if quantity <= 0:
380
+ return "Please enter a valid quantity (e.g., 1, 2, 3)."
381
+ state.data["quantity"] = quantity
382
+ state.step = 3
383
+
384
+ # Check if phone number and address are provided in the same message.
385
+ phone_pattern = r'(\+?\d{10,15})'
386
+ phone_match = re.search(phone_pattern, message)
387
+ address = None
388
+ if phone_match:
389
+ phone_number = phone_match.group(1)
390
+ address_start = phone_match.end()
391
+ address = message[address_start:].strip()
392
+ address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
393
+
394
+ if phone_match and address:
395
+ state.data["phone_number"] = phone_number
396
+ state.data["address"] = address
397
+ await update_user_profile(user_id, phone_number, address)
398
+ shipping_cost = calculate_shipping_cost(address)
399
+ state.data["shipping_cost"] = shipping_cost
400
+ state.step = 5
401
+ return (
402
+ f"Thanks! Your phone number is recorded as: {phone_number}.\n"
403
+ f"Your delivery address is: {address}.\n"
404
+ f"Your delivery cost is N{shipping_cost}. Would you like to add any extras such as sides or drinks? (yes/no)"
405
+ )
406
+ elif phone_match:
407
+ state.data["phone_number"] = phone_match.group(1)
408
+ await update_user_profile(user_id, phone_number)
409
+ return "Thank you. Please provide your delivery address."
410
+ else:
411
+ return (
412
+ "Please provide both your phone number and delivery address. For example: "
413
+ "'08152409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'."
414
+ )
415
  else:
416
+ # No quantity provided – ask for number of servings.
417
+ state.step = 2
418
+ return f"You selected {found_dish}. How many servings would you like?"
419
+
420
+ # If state exists and is already in order flow, handle step 1 (dish selection).
421
  if state and state.flow == "order":
422
  state.update_last_active()
423
  if state.step == 1:
 
446
  else:
447
  return "I couldn't identify the dish. Please type the dish name from our menu."
448
 
 
 
449
  if state.step == 2:
450
  numbers = re.findall(r'\d+', message)
451
  if not numbers: