Fred808 commited on
Commit
c694be8
·
verified ·
1 Parent(s): 2dcda45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -329,7 +329,7 @@ def process_order_flow(user_id: str, message: str) -> str:
329
  state.data["quantity"] = quantity
330
  state.update_last_active()
331
  user_state[user_id] = state
332
- return f"You selected {found_dish} with {quantity} serving(s). Please provide your phone number for order updates."
333
 
334
  if state and state.flow == "order":
335
  state.update_last_active()
@@ -350,7 +350,7 @@ def process_order_flow(user_id: str, message: str) -> str:
350
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
351
  state.data["quantity"] = quantity
352
  state.step = 3 # Move to phone number step
353
- return f"You selected {found_dish} with {quantity} serving(s). Please provide your phone number for order updates."
354
  else:
355
  state.step = 2 # Ask for quantity
356
  return f"You selected {found_dish}. How many servings would you like?"
@@ -367,17 +367,37 @@ def process_order_flow(user_id: str, message: str) -> str:
367
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
368
  state.data["quantity"] = quantity
369
  state.step = 3
370
- return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number for order updates."
371
 
372
- # --- Step 3: Requesting Phone Number ---
373
  if state.step == 3:
374
- phone_pattern = r'^\+?\d{10,15}$'
375
- if re.match(phone_pattern, message.strip()):
376
- state.data["phone_number"] = message.strip()
377
- state.step = 4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
  return "Thank you. Please provide your delivery address."
 
 
 
379
  else:
380
- return "Please enter a valid phone number (e.g., +234XXXXXXXXXX or 0XXXXXXXXXX)."
381
 
382
  # --- Step 4: Requesting Delivery Address ---
383
  if state.step == 4:
@@ -463,9 +483,6 @@ def process_order_flow(user_id: str, message: str) -> str:
463
  # Record the initial tracking update: Order Placed
464
  asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
465
 
466
-
467
-
468
-
469
  # Notify management of the new order via WhatsApp
470
  async def notify_management_order(order_details: dict):
471
  message_body = (
 
329
  state.data["quantity"] = quantity
330
  state.update_last_active()
331
  user_state[user_id] = state
332
+ return f"You selected {found_dish} with {quantity} serving(s). Please provide your phone number and delivery address."
333
 
334
  if state and state.flow == "order":
335
  state.update_last_active()
 
350
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
351
  state.data["quantity"] = quantity
352
  state.step = 3 # Move to phone number step
353
+ return f"You selected {found_dish} with {quantity} serving(s). Please provide your phone number and delivery address."
354
  else:
355
  state.step = 2 # Ask for quantity
356
  return f"You selected {found_dish}. How many servings would you like?"
 
367
  return "Please enter a valid quantity (e.g., 1, 2, 3)."
368
  state.data["quantity"] = quantity
369
  state.step = 3
370
+ return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number and delivery address."
371
 
372
+ # --- Step 3: Requesting Phone Number and Address ---
373
  if state.step == 3:
374
+ # Regular expression to extract phone number and address
375
+ phone_pattern = r'(\+?\d{10,15})'
376
+ address_pattern = r'address:\s*(.+)'
377
+
378
+ phone_match = re.search(phone_pattern, message)
379
+ address_match = re.search(address_pattern, message, re.IGNORECASE)
380
+
381
+ if phone_match and address_match:
382
+ phone_number = phone_match.group(1)
383
+ address = address_match.group(1)
384
+ state.data["phone_number"] = phone_number
385
+ state.data["address"] = address
386
+ # Calculate shipping cost based on the address
387
+ shipping_cost = calculate_shipping_cost(address)
388
+ state.data["shipping_cost"] = shipping_cost
389
+ state.step = 5
390
+ return (f"Thanks. Your phone number is recorded as: {phone_number}.\n"
391
+ f"Your delivery address is: {address}.\n"
392
+ f"Your delivery cost is N{shipping_cost}. Would you like to add any extras such as sides or drinks? (yes/no)")
393
+ elif phone_match:
394
+ state.data["phone_number"] = phone_match.group(1)
395
  return "Thank you. Please provide your delivery address."
396
+ elif address_match:
397
+ state.data["address"] = address_match.group(1)
398
+ return "Thank you. Please provide your phone number."
399
  else:
400
+ return "Please provide both your phone number and delivery address. For example: 'Phone: 08123456789, Address: 123 Main St, Lagos'."
401
 
402
  # --- Step 4: Requesting Delivery Address ---
403
  if state.step == 4:
 
483
  # Record the initial tracking update: Order Placed
484
  asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
485
 
 
 
 
486
  # Notify management of the new order via WhatsApp
487
  async def notify_management_order(order_details: dict):
488
  message_body = (