Update app.py
Browse files
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 |
-
|
| 364 |
-
if found_dish
|
| 365 |
-
|
| 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 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
else:
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 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:
|