Update app.py
Browse files
app.py
CHANGED
|
@@ -106,9 +106,33 @@ menu_items = [
|
|
| 106 |
{"name": "Jollof Rice", "description": "A spicy and flavorful rice dish", "price": 1500, "nutrition": "Calories: 300 kcal, Carbs: 50g, Protein: 10g, Fat: 5g"},
|
| 107 |
{"name": "Fried Rice", "description": "A savory rice dish with vegetables and meat", "price": 1200, "nutrition": "Calories: 350 kcal, Carbs: 55g, Protein: 12g, Fat: 8g"},
|
| 108 |
{"name": "Chicken Wings", "description": "Crispy fried chicken wings", "price": 2000, "nutrition": "Calories: 400 kcal, Carbs: 20g, Protein: 25g, Fat: 15g"},
|
| 109 |
-
{"name": "Egusi Soup", "description": "A rich and hearty soup made with melon seeds", "price": 1000, "nutrition": "Calories: 250 kcal, Carbs: 15g, Protein: 8g, Fat: 10g"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
]
|
| 111 |
|
|
|
|
| 112 |
class ConversationState:
|
| 113 |
def __init__(self):
|
| 114 |
self.flow = None
|
|
@@ -384,13 +408,19 @@ def match_dishes(user_input: str, threshold: int = 70) -> list:
|
|
| 384 |
matched_dishes.append(dish_name)
|
| 385 |
return list(set(matched_dishes))
|
| 386 |
|
|
|
|
| 387 |
async def process_order_flow(user_id: str, message: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
state = user_state.get(user_id)
|
| 389 |
if state and state.is_expired():
|
| 390 |
state.reset()
|
| 391 |
del user_state[user_id]
|
| 392 |
state = None
|
| 393 |
|
|
|
|
| 394 |
if message.lower() in ["order", "menu"]:
|
| 395 |
state = ConversationState()
|
| 396 |
state.flow = "order"
|
|
@@ -401,6 +431,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 401 |
return "Sure! What dish would you like to order?"
|
| 402 |
return ""
|
| 403 |
|
|
|
|
| 404 |
if not state and "order" in message.lower():
|
| 405 |
state = ConversationState()
|
| 406 |
state.flow = "order"
|
|
@@ -409,236 +440,339 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 409 |
user_state[user_id] = state
|
| 410 |
return "Sure! What dish would you like to order?"
|
| 411 |
|
| 412 |
-
#
|
| 413 |
if not state or state.flow != "order":
|
| 414 |
-
|
| 415 |
-
if
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
state.data["quantity"] = quantity
|
| 427 |
-
state.step = 3
|
| 428 |
-
phone_pattern = r'(\+?\d{10,15})'
|
| 429 |
-
phone_match = re.search(phone_pattern, message)
|
| 430 |
-
address = None
|
| 431 |
-
if phone_match:
|
| 432 |
-
phone_number = phone_match.group(1)
|
| 433 |
-
address_start = phone_match.end()
|
| 434 |
-
address = message[address_start:].strip()
|
| 435 |
-
address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
|
| 436 |
-
if phone_match and address:
|
| 437 |
-
state.data["phone_number"] = phone_number
|
| 438 |
-
state.data["address"] = address
|
| 439 |
-
asyncio.create_task(update_user_profile(user_id, phone_number, address))
|
| 440 |
-
shipping_cost = calculate_shipping_cost(address)
|
| 441 |
-
state.data["shipping_cost"] = shipping_cost
|
| 442 |
-
state.step = 5
|
| 443 |
-
return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
|
| 444 |
-
f"Your delivery address is: {address}.\n"
|
| 445 |
-
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 446 |
-
elif phone_match:
|
| 447 |
-
state.data["phone_number"] = phone_match.group(1)
|
| 448 |
-
asyncio.create_task(update_user_profile(user_id, phone_number))
|
| 449 |
-
return "Thank you. Please provide your delivery address."
|
| 450 |
-
else:
|
| 451 |
-
return ("Please provide both your phone number and delivery address. "
|
| 452 |
-
"For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'.")
|
| 453 |
else:
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
if state.step == 1:
|
| 461 |
-
# Use fuzzy matching on the message in case of typos.
|
| 462 |
-
found_dish = match_dish(message)
|
| 463 |
-
numbers = re.findall(r'\d+', message)
|
| 464 |
-
if found_dish:
|
| 465 |
state.data["dish"] = found_dish
|
| 466 |
-
if
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
state.data["quantity"] = quantity
|
| 471 |
state.step = 3
|
| 472 |
-
return (f"You selected {found_dish} with {quantity} serving(s). "
|
| 473 |
-
"Please provide your phone number and delivery address.")
|
| 474 |
else:
|
|
|
|
| 475 |
state.step = 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
return f"You selected {found_dish}. How many servings would you like?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
else:
|
| 478 |
-
return "I couldn't
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
quantity
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
state.data["
|
| 512 |
-
asyncio.create_task(update_user_profile(user_id, phone_number))
|
| 513 |
-
return "Thank you. Please provide your delivery address."
|
| 514 |
-
else:
|
| 515 |
-
return ("Please provide both your phone number and delivery address. "
|
| 516 |
-
"For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'.")
|
| 517 |
-
|
| 518 |
-
# Steps 4, 5, 6, and 7 remain unchanged.
|
| 519 |
-
if state.step == 4:
|
| 520 |
-
state.data["address"] = message
|
| 521 |
-
asyncio.create_task(update_user_profile(user_id, address=message))
|
| 522 |
-
shipping_cost = calculate_shipping_cost(message)
|
| 523 |
-
state.data["shipping_cost"] = shipping_cost
|
| 524 |
-
state.step = 5
|
| 525 |
-
return (f"Thanks. Your delivery address is recorded as: {message}.\n"
|
| 526 |
-
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 527 |
-
|
| 528 |
-
if state.step == 5:
|
| 529 |
-
if message.lower() in ["yes", "y"]:
|
| 530 |
-
state.step = 6
|
| 531 |
-
return "Please list the extras you'd like (e.g., drinks, sides)."
|
| 532 |
-
elif message.lower() in ["no", "n"]:
|
| 533 |
-
state.data["extras"] = ""
|
| 534 |
-
state.step = 7
|
| 535 |
-
dish = state.data.get("dish", "")
|
| 536 |
-
quantity = state.data.get("quantity", 1)
|
| 537 |
-
phone = state.data.get("phone_number", "")
|
| 538 |
-
address = state.data.get("address", "")
|
| 539 |
-
shipping_cost = state.data.get("shipping_cost", 0)
|
| 540 |
-
price_per_serving = 1500
|
| 541 |
-
total_price = (quantity * price_per_serving) + shipping_cost
|
| 542 |
-
summary = (f"Order Summary:\nDish: {dish}\nQuantity: {quantity}\n"
|
| 543 |
-
f"Phone: {phone}\nAddress: {address}\n"
|
| 544 |
-
f"Shipping Cost: N{shipping_cost}\n"
|
| 545 |
-
f"Total Price: N{total_price}\n"
|
| 546 |
-
f"Extras: None\nConfirm order? (yes/no)")
|
| 547 |
-
return summary
|
| 548 |
else:
|
| 549 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
|
| 551 |
-
|
| 552 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
state.step = 7
|
|
|
|
|
|
|
|
|
|
| 554 |
dish = state.data.get("dish", "")
|
| 555 |
quantity = state.data.get("quantity", 1)
|
| 556 |
phone = state.data.get("phone_number", "")
|
| 557 |
address = state.data.get("address", "")
|
| 558 |
shipping_cost = state.data.get("shipping_cost", 0)
|
| 559 |
-
extras =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
price_per_serving = 1500
|
| 561 |
total_price = (quantity * price_per_serving) + shipping_cost
|
| 562 |
-
summary = (f"Order Summary:\
|
| 563 |
-
f"
|
|
|
|
|
|
|
|
|
|
| 564 |
f"Shipping Cost: N{shipping_cost}\n"
|
| 565 |
f"Total Price: N{total_price}\n"
|
| 566 |
-
f"Extras: {extras}\
|
|
|
|
| 567 |
return summary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
return ""
|
| 641 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
|
| 643 |
async def get_or_create_user_profile(user_id: str, phone_number: str = None) -> UserProfile:
|
| 644 |
async with async_session() as session:
|
|
|
|
| 106 |
{"name": "Jollof Rice", "description": "A spicy and flavorful rice dish", "price": 1500, "nutrition": "Calories: 300 kcal, Carbs: 50g, Protein: 10g, Fat: 5g"},
|
| 107 |
{"name": "Fried Rice", "description": "A savory rice dish with vegetables and meat", "price": 1200, "nutrition": "Calories: 350 kcal, Carbs: 55g, Protein: 12g, Fat: 8g"},
|
| 108 |
{"name": "Chicken Wings", "description": "Crispy fried chicken wings", "price": 2000, "nutrition": "Calories: 400 kcal, Carbs: 20g, Protein: 25g, Fat: 15g"},
|
| 109 |
+
{"name": "Egusi Soup", "description": "A rich and hearty soup made with melon seeds", "price": 1000, "nutrition": "Calories: 250 kcal, Carbs: 15g, Protein: 8g, Fat: 10g"},
|
| 110 |
+
{"name": "All Rice Except Native Rice", "description": "Any rice dish except native rice", "price": 1200, "nutrition": "Calories: 320 kcal, Carbs: 55g, Protein: 8g, Fat: 6g"},
|
| 111 |
+
{"name": "Native Rice", "description": "Traditional native-style rice", "price": 1500, "nutrition": "Calories: 350 kcal, Carbs: 60g, Protein: 10g, Fat: 7g"},
|
| 112 |
+
{"name": "Spaghetti Jollof", "description": "Jollof-style spaghetti dish", "price": 1200, "nutrition": "Calories: 400 kcal, Carbs: 65g, Protein: 10g, Fat: 8g"},
|
| 113 |
+
{"name": "Macaroni Jollof", "description": "Jollof-style macaroni dish", "price": 1200, "nutrition": "Calories: 380 kcal, Carbs: 60g, Protein: 9g, Fat: 7g"},
|
| 114 |
+
{"name": "White Rice with Vegetable Sauce and Chicken", "description": "White rice served with vegetable sauce and chicken", "price": 3700, "nutrition": "Calories: 450 kcal, Carbs: 60g, Protein: 15g, Fat: 10g"},
|
| 115 |
+
{"name": "Only Vegetable Sauce with Chicken", "description": "Vegetable sauce with chicken, no rice", "price": 2500, "nutrition": "Calories: 300 kcal, Carbs: 20g, Protein: 15g, Fat: 10g"},
|
| 116 |
+
{"name": "Chicken Drumstick", "description": "Fried or grilled chicken drumstick", "price": 2500, "nutrition": "Calories: 350 kcal, Carbs: 5g, Protein: 30g, Fat: 15g"},
|
| 117 |
+
{"name": "Chicken Wings", "description": "Crispy fried chicken wings", "price": 3960, "nutrition": "Calories: 450 kcal, Carbs: 20g, Protein: 30g, Fat: 20g"},
|
| 118 |
+
{"name": "Fried Chicken", "description": "Crispy deep-fried chicken", "price": 4140, "nutrition": "Calories: 500 kcal, Carbs: 15g, Protein: 35g, Fat: 25g"},
|
| 119 |
+
{"name": "Fried Turkey", "description": "Crispy deep-fried turkey", "price": 4000, "nutrition": "Calories: 480 kcal, Carbs: 10g, Protein: 40g, Fat: 20g"},
|
| 120 |
+
{"name": "Gizzard", "description": "Fried or grilled gizzard", "price": 3000, "nutrition": "Calories: 200 kcal, Carbs: 5g, Protein: 20g, Fat: 8g"},
|
| 121 |
+
{"name": "Fish", "description": "Fried or grilled fish", "price": 2500, "nutrition": "Calories: 250 kcal, Carbs: 5g, Protein: 25g, Fat: 10g"},
|
| 122 |
+
{"name": "Beef", "description": "Spicy fried or grilled beef", "price": 1200, "nutrition": "Calories: 300 kcal, Carbs: 2g, Protein: 28g, Fat: 15g"},
|
| 123 |
+
{"name": "Pepper Chicken", "description": "Spicy peppered chicken", "price": 1500, "nutrition": "Calories: 400 kcal, Carbs: 10g, Protein: 35g, Fat: 18g"},
|
| 124 |
+
{"name": "Pepper Chicken Big", "description": "Larger portion of spicy peppered chicken", "price": 2500, "nutrition": "Calories: 600 kcal, Carbs: 15g, Protein: 50g, Fat: 25g"},
|
| 125 |
+
{"name": "Salad", "description": "Fresh vegetable salad", "price": 800, "nutrition": "Calories: 100 kcal, Carbs: 15g, Protein: 3g, Fat: 5g"},
|
| 126 |
+
{"name": "Egg and Yam", "description": "Boiled yam served with eggs", "price": 1000, "nutrition": "Calories: 300 kcal, Carbs: 50g, Protein: 10g, Fat: 5g"},
|
| 127 |
+
{"name": "Moi Moi", "description": "Steamed bean pudding", "price": 1000, "nutrition": "Calories: 250 kcal, Carbs: 30g, Protein: 12g, Fat: 8g"},
|
| 128 |
+
{"name": "Plantain", "description": "Fried or boiled ripe plantain", "price": 1000, "nutrition": "Calories: 200 kcal, Carbs: 50g, Protein: 2g, Fat: 1g"},
|
| 129 |
+
{"name": "Soup with Goat Meat and Semo", "description": "Rich soup with goat meat served with semo", "price": 4600, "nutrition": "Calories: 500 kcal, Carbs: 60g, Protein: 30g, Fat: 15g"},
|
| 130 |
+
{"name": "Soup with Chicken and Semo", "description": "Soup served with chicken and semo", "price": 500, "nutrition": "Calories: 450 kcal, Carbs: 55g, Protein: 28g, Fat: 12g"},
|
| 131 |
+
{"name": "Extra Semo", "description": "Additional portion of semo", "price": 500, "nutrition": "Calories: 200 kcal, Carbs: 45g, Protein: 5g, Fat: 1g"},
|
| 132 |
+
{"name": "Banger with Catfish", "description": "Traditional dish with catfish", "price": 5000, "nutrition": "Calories: 600 kcal, Carbs: 50g, Protein: 35g, Fat: 15g"}
|
| 133 |
]
|
| 134 |
|
| 135 |
+
|
| 136 |
class ConversationState:
|
| 137 |
def __init__(self):
|
| 138 |
self.flow = None
|
|
|
|
| 408 |
matched_dishes.append(dish_name)
|
| 409 |
return list(set(matched_dishes))
|
| 410 |
|
| 411 |
+
|
| 412 |
async def process_order_flow(user_id: str, message: str) -> str:
|
| 413 |
+
"""
|
| 414 |
+
A unified approach to parse dish(es), quantity, phone, and address
|
| 415 |
+
whether the user provides them all at once or in multiple steps.
|
| 416 |
+
"""
|
| 417 |
state = user_state.get(user_id)
|
| 418 |
if state and state.is_expired():
|
| 419 |
state.reset()
|
| 420 |
del user_state[user_id]
|
| 421 |
state = None
|
| 422 |
|
| 423 |
+
# 1) If user says "menu" or "order", initialize the flow
|
| 424 |
if message.lower() in ["order", "menu"]:
|
| 425 |
state = ConversationState()
|
| 426 |
state.flow = "order"
|
|
|
|
| 431 |
return "Sure! What dish would you like to order?"
|
| 432 |
return ""
|
| 433 |
|
| 434 |
+
# If user says "order" but we have no state
|
| 435 |
if not state and "order" in message.lower():
|
| 436 |
state = ConversationState()
|
| 437 |
state.flow = "order"
|
|
|
|
| 440 |
user_state[user_id] = state
|
| 441 |
return "Sure! What dish would you like to order?"
|
| 442 |
|
| 443 |
+
# 2) If we aren't in the order flow yet, try to detect dish(es)
|
| 444 |
if not state or state.flow != "order":
|
| 445 |
+
matched = match_dishes(message)
|
| 446 |
+
if matched:
|
| 447 |
+
# Multiple candidate dishes
|
| 448 |
+
if len(matched) > 1:
|
| 449 |
+
state = ConversationState()
|
| 450 |
+
state.flow = "order"
|
| 451 |
+
state.update_last_active()
|
| 452 |
+
user_state[user_id] = state
|
| 453 |
+
state.data["candidate_dishes"] = matched
|
| 454 |
+
dish_options = ", ".join(matched)
|
| 455 |
+
return (f"We found multiple dishes in your request: {dish_options}. "
|
| 456 |
+
"Please specify which one you'd like to order or type 'both' if you'd like all.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
else:
|
| 458 |
+
# Single dish
|
| 459 |
+
found_dish = matched[0]
|
| 460 |
+
state = ConversationState()
|
| 461 |
+
state.flow = "order"
|
| 462 |
+
state.update_last_active()
|
| 463 |
+
user_state[user_id] = state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
state.data["dish"] = found_dish
|
| 465 |
+
# Attempt to parse quantity, phone, address if user typed them all
|
| 466 |
+
single_dish_parse = _parse_single_dish_line(message, found_dish)
|
| 467 |
+
if single_dish_parse["quantity"]:
|
| 468 |
+
state.data["quantity"] = single_dish_parse["quantity"]
|
|
|
|
| 469 |
state.step = 3
|
|
|
|
|
|
|
| 470 |
else:
|
| 471 |
+
# If no quantity found, we still need quantity
|
| 472 |
state.step = 2
|
| 473 |
+
|
| 474 |
+
if single_dish_parse["phone"]:
|
| 475 |
+
state.data["phone_number"] = single_dish_parse["phone"]
|
| 476 |
+
if single_dish_parse["address"]:
|
| 477 |
+
state.data["address"] = single_dish_parse["address"]
|
| 478 |
+
|
| 479 |
+
# Now see how much info we have:
|
| 480 |
+
if state.step == 2 and not state.data.get("quantity"):
|
| 481 |
+
# We must ask for quantity
|
| 482 |
return f"You selected {found_dish}. How many servings would you like?"
|
| 483 |
+
elif state.step == 3:
|
| 484 |
+
# We have a quantity. Check if we also have phone and address
|
| 485 |
+
if state.data.get("phone_number") and state.data.get("address"):
|
| 486 |
+
# We have everything needed to skip to step 5
|
| 487 |
+
shipping_cost = calculate_shipping_cost(state.data["address"])
|
| 488 |
+
state.data["shipping_cost"] = shipping_cost
|
| 489 |
+
state.step = 5
|
| 490 |
+
return (f"Thanks! Your phone number is recorded as: {state.data['phone_number']}.\n"
|
| 491 |
+
f"Your delivery address is: {state.data['address']}.\n"
|
| 492 |
+
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 493 |
+
elif state.data.get("phone_number") and not state.data.get("address"):
|
| 494 |
+
return "Thank you. Please provide your delivery address."
|
| 495 |
+
else:
|
| 496 |
+
return "Please provide your phone number and address."
|
| 497 |
+
else:
|
| 498 |
+
# No dish matched
|
| 499 |
+
return "I couldn't identify the dish. Please type the dish name from our menu."
|
| 500 |
+
|
| 501 |
+
# 3) If we have candidate dishes and user needs to clarify
|
| 502 |
+
if state and state.flow == "order" and "candidate_dishes" in state.data:
|
| 503 |
+
# If user typed "both" => they want all candidate dishes
|
| 504 |
+
if message.strip().lower() in ["both", "all"]:
|
| 505 |
+
state.data["dishes"] = state.data["candidate_dishes"]
|
| 506 |
+
del state.data["candidate_dishes"]
|
| 507 |
+
state.step = 2 # We'll parse "2 for Jollof Rice and 3 for Fried Rice"
|
| 508 |
+
dishes_str = ", ".join(state.data["dishes"])
|
| 509 |
+
return (f"You have selected: {dishes_str}. How many servings of each would you like? "
|
| 510 |
+
"(For example, '2 for Jollof Rice and 3 for Fried Rice')")
|
| 511 |
+
else:
|
| 512 |
+
# Maybe the user typed a single dish out of the candidates
|
| 513 |
+
for dish in state.data["candidate_dishes"]:
|
| 514 |
+
if dish.lower() in message.lower():
|
| 515 |
+
state.data["dish"] = dish
|
| 516 |
+
del state.data["candidate_dishes"]
|
| 517 |
+
state.step = 2
|
| 518 |
+
return f"You selected {dish}. How many servings would you like?"
|
| 519 |
+
# If still unclear
|
| 520 |
+
dish_options = ", ".join(state.data["candidate_dishes"])
|
| 521 |
+
return (f"Please specify which one you'd like to order from: {dish_options} "
|
| 522 |
+
"(or type 'both' if you'd like to order all).")
|
| 523 |
+
|
| 524 |
+
# 4) If step == 2 for multiple dishes => parse "2 for Jollof Rice and 3 for Fried Rice"
|
| 525 |
+
if state and state.flow == "order" and state.step == 2:
|
| 526 |
+
if "dishes" in state.data and len(state.data["dishes"]) > 1:
|
| 527 |
+
pairs = re.findall(r'(\d+)\s*for\s*([a-zA-Z\s]+)', message, flags=re.IGNORECASE)
|
| 528 |
+
if not pairs:
|
| 529 |
+
return ("I'm sorry, I didn't understand the quantity details. "
|
| 530 |
+
"Please specify like '2 for Jollof Rice and 3 for Fried Rice'.")
|
| 531 |
+
order_quantities = {}
|
| 532 |
+
for quantity, dish_text in pairs:
|
| 533 |
+
dish_text = dish_text.strip().lower()
|
| 534 |
+
for candidate in state.data["dishes"]:
|
| 535 |
+
if candidate.lower() in dish_text or dish_text in candidate.lower():
|
| 536 |
+
order_quantities[candidate] = int(quantity)
|
| 537 |
+
if order_quantities:
|
| 538 |
+
state.data["orders"] = order_quantities
|
| 539 |
+
state.step = 3
|
| 540 |
+
summary = "\n".join([f"{q} serving(s) of {d}" for d, q in order_quantities.items()])
|
| 541 |
+
return (f"Got it. You have ordered:\n{summary}\n"
|
| 542 |
+
"Please provide your phone number and delivery address.")
|
| 543 |
else:
|
| 544 |
+
return ("I'm sorry, I couldn't match those dishes. "
|
| 545 |
+
"Please try something like '2 for Jollof Rice and 3 for Fried Rice'.")
|
| 546 |
+
|
| 547 |
+
# Single-dish scenario at step 2 => user typed just a quantity
|
| 548 |
+
numbers = re.findall(r'\d+', message)
|
| 549 |
+
if not numbers:
|
| 550 |
+
return "Please enter a valid number for the quantity (e.g., 1, 2, 3)."
|
| 551 |
+
quantity = int(numbers[0])
|
| 552 |
+
if quantity <= 0:
|
| 553 |
+
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 554 |
+
state.data["quantity"] = quantity
|
| 555 |
+
state.step = 3
|
| 556 |
+
dish = state.data.get("dish", "")
|
| 557 |
+
return (f"Got it. {quantity} serving(s) of {dish}. "
|
| 558 |
+
"Please provide your phone number and delivery address.")
|
| 559 |
+
|
| 560 |
+
# 5) Step == 3 => parse phone & address for single or multiple dishes
|
| 561 |
+
if state and state.flow == "order" and state.step == 3:
|
| 562 |
+
phone_pattern = r'(\+?\d{10,15})'
|
| 563 |
+
phone_match = re.search(phone_pattern, message)
|
| 564 |
+
address = None
|
| 565 |
+
if phone_match:
|
| 566 |
+
phone_number = phone_match.group(1)
|
| 567 |
+
address_start = phone_match.end()
|
| 568 |
+
address = message[address_start:].strip()
|
| 569 |
+
address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
|
| 570 |
+
state.data["phone_number"] = phone_number
|
| 571 |
+
state.data["address"] = address
|
| 572 |
+
asyncio.create_task(update_user_profile(user_id, phone_number, address))
|
| 573 |
+
else:
|
| 574 |
+
# Maybe the user typed an address only, or partial info
|
| 575 |
+
# If the phone was previously stored, we just store the address
|
| 576 |
+
if "phone_number" in state.data:
|
| 577 |
+
state.data["address"] = message.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
else:
|
| 579 |
+
# If no phone number found yet, we need it
|
| 580 |
+
return ("Please provide both your phone number and address. "
|
| 581 |
+
"For example: '09162409591, 1, Iyana Isashi, Isashi, Lagos'.")
|
| 582 |
+
|
| 583 |
+
if "address" not in state.data:
|
| 584 |
+
# If we only found phone but no address, ask for address
|
| 585 |
+
return "Thank you. Please provide your delivery address."
|
| 586 |
+
|
| 587 |
+
# If we have phone + address, move to step 5 => ask extras
|
| 588 |
+
shipping_cost = calculate_shipping_cost(state.data["address"])
|
| 589 |
+
state.data["shipping_cost"] = shipping_cost
|
| 590 |
+
state.step = 5
|
| 591 |
+
return (f"Thanks! Your phone number is recorded as: {state.data['phone_number']}.\n"
|
| 592 |
+
f"Your delivery address is: {state.data['address']}.\n"
|
| 593 |
+
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 594 |
|
| 595 |
+
# ... existing code above ...
|
| 596 |
+
|
| 597 |
+
# Step 5: Ask user about extras
|
| 598 |
+
if state and state.flow == "order" and state.step == 5:
|
| 599 |
+
# If user wants extras
|
| 600 |
+
if message.lower() in ["yes", "y"]:
|
| 601 |
+
state.step = 6
|
| 602 |
+
return "Please list the extras you'd like (e.g., drinks, sides)."
|
| 603 |
+
|
| 604 |
+
# If user doesn't want extras
|
| 605 |
+
elif message.lower() in ["no", "n"]:
|
| 606 |
+
state.data["extras"] = ""
|
| 607 |
state.step = 7
|
| 608 |
+
# Summarize the order so far
|
| 609 |
+
# If you handled multiple dishes, you'd need to show all.
|
| 610 |
+
# For single dish, just show dish + quantity:
|
| 611 |
dish = state.data.get("dish", "")
|
| 612 |
quantity = state.data.get("quantity", 1)
|
| 613 |
phone = state.data.get("phone_number", "")
|
| 614 |
address = state.data.get("address", "")
|
| 615 |
shipping_cost = state.data.get("shipping_cost", 0)
|
| 616 |
+
extras = "None"
|
| 617 |
+
|
| 618 |
+
# If you used state.data["orders"] for multiple dishes,
|
| 619 |
+
# you'd build a summary from that dictionary.
|
| 620 |
+
|
| 621 |
price_per_serving = 1500
|
| 622 |
total_price = (quantity * price_per_serving) + shipping_cost
|
| 623 |
+
summary = (f"Order Summary:\n"
|
| 624 |
+
f"Dish: {dish}\n"
|
| 625 |
+
f"Quantity: {quantity}\n"
|
| 626 |
+
f"Phone: {phone}\n"
|
| 627 |
+
f"Address: {address}\n"
|
| 628 |
f"Shipping Cost: N{shipping_cost}\n"
|
| 629 |
f"Total Price: N{total_price}\n"
|
| 630 |
+
f"Extras: {extras}\n"
|
| 631 |
+
f"Confirm order? (yes/no)")
|
| 632 |
return summary
|
| 633 |
+
|
| 634 |
+
else:
|
| 635 |
+
# If user typed something else at step 5
|
| 636 |
+
return "Please respond with 'yes' or 'no' regarding extras."
|
| 637 |
+
|
| 638 |
+
# Step 6: If user typed "yes" for extras, we prompt them for a list
|
| 639 |
+
if state and state.flow == "order" and state.step == 6:
|
| 640 |
+
# The user has just listed extras
|
| 641 |
+
state.data["extras"] = message
|
| 642 |
+
state.step = 7
|
| 643 |
+
dish = state.data.get("dish", "")
|
| 644 |
+
quantity = state.data.get("quantity", 1)
|
| 645 |
+
phone = state.data.get("phone_number", "")
|
| 646 |
+
address = state.data.get("address", "")
|
| 647 |
+
shipping_cost = state.data.get("shipping_cost", 0)
|
| 648 |
+
extras = state.data.get("extras", "None")
|
| 649 |
+
|
| 650 |
+
price_per_serving = 1500
|
| 651 |
+
total_price = (quantity * price_per_serving) + shipping_cost
|
| 652 |
+
summary = (f"Order Summary:\n"
|
| 653 |
+
f"Dish: {dish}\n"
|
| 654 |
+
f"Quantity: {quantity}\n"
|
| 655 |
+
f"Phone: {phone}\n"
|
| 656 |
+
f"Address: {address}\n"
|
| 657 |
+
f"Shipping Cost: N{shipping_cost}\n"
|
| 658 |
+
f"Total Price: N{total_price}\n"
|
| 659 |
+
f"Extras: {extras}\n"
|
| 660 |
+
f"Confirm order? (yes/no)")
|
| 661 |
+
return summary
|
| 662 |
+
|
| 663 |
|
| 664 |
+
# Step 7: Order Confirmation and Payment Link Generation
|
| 665 |
+
if state and state.flow == "order" and state.step == 7:
|
| 666 |
+
if message.lower() in ["yes", "y"]:
|
| 667 |
+
order_id = f"ORD-{int(time.time())}"
|
| 668 |
+
state.data["order_id"] = order_id
|
| 669 |
+
price_per_serving = 1500
|
| 670 |
+
quantity = state.data.get("quantity", 1)
|
| 671 |
+
shipping_cost = state.data.get("shipping_cost", 0)
|
| 672 |
+
total_price = (quantity * price_per_serving) + shipping_cost
|
| 673 |
+
state.data["price"] = str(total_price)
|
| 674 |
+
|
| 675 |
+
async def save_order():
|
| 676 |
+
async with async_session() as session:
|
| 677 |
+
order = Order(
|
| 678 |
+
order_id=order_id,
|
| 679 |
+
user_id=user_id,
|
| 680 |
+
dish=state.data["dish"],
|
| 681 |
+
quantity=str(quantity),
|
| 682 |
+
price=str(total_price),
|
| 683 |
+
status="Pending Payment",
|
| 684 |
+
delivery_address=state.data.get("address", "")
|
| 685 |
+
)
|
| 686 |
+
session.add(order)
|
| 687 |
+
await session.commit()
|
| 688 |
+
asyncio.create_task(save_order())
|
| 689 |
+
asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
|
| 690 |
+
|
| 691 |
+
async def notify_management_order(order_details: dict):
|
| 692 |
+
message_body = (
|
| 693 |
+
f"New Order Received:\n"
|
| 694 |
+
f"Order ID: {order_details['order_id']}\n"
|
| 695 |
+
f"Dish: {order_details['dish']}\n"
|
| 696 |
+
f"Quantity: {order_details['quantity']}\n"
|
| 697 |
+
f"Total Price: {order_details['price']}\n"
|
| 698 |
+
f"Phone: {state.data.get('phone_number', '')}\n"
|
| 699 |
+
f"Delivery Address: {order_details.get('address', 'Not Provided')}\n"
|
| 700 |
+
f"Extras: {state.data.get('extras', 'None')}\n"
|
| 701 |
+
f"Status: Pending Payment"
|
| 702 |
+
)
|
| 703 |
+
await asyncio.to_thread(send_whatsapp_message, MANAGEMENT_WHATSAPP_NUMBER, message_body)
|
| 704 |
+
order_details = {
|
| 705 |
+
"order_id": order_id,
|
| 706 |
+
"dish": state.data["dish"],
|
| 707 |
+
"quantity": state.data["quantity"],
|
| 708 |
+
"price": state.data["price"],
|
| 709 |
+
"address": state.data.get("address", "")
|
| 710 |
+
}
|
| 711 |
+
asyncio.create_task(notify_management_order(order_details))
|
| 712 |
+
|
| 713 |
+
email = "customer@example.com" # or extract the email from the user's profile if available
|
| 714 |
+
# create_paystack_payment_link returns a dict with a key "status" and the link under "data.authorization_url"
|
| 715 |
+
payment_data = create_paystack_payment_link(email, total_price * 100, order_id)
|
| 716 |
+
dish_name = state.data.get("dish", "")
|
| 717 |
+
# Clean up state
|
| 718 |
+
state.reset()
|
| 719 |
+
if user_id in user_state:
|
| 720 |
+
del user_state[user_id]
|
| 721 |
+
if payment_data.get("status"):
|
| 722 |
+
payment_link = payment_data["data"]["authorization_url"]
|
| 723 |
+
return (f"Thank you for your order of {quantity} serving(s) of {dish_name}! "
|
| 724 |
+
f"Your Order ID is {order_id}.\n\n"
|
| 725 |
+
"Please complete your payment using one of the following options:\n"
|
| 726 |
+
f"1. Pay online via our Paystack link: {payment_link}\n"
|
| 727 |
+
"2. Alternatively, you can make a bank transfer to the following account:\n"
|
| 728 |
+
" Account Number: 1433042821\n"
|
| 729 |
+
" Bank: Access Bank\n"
|
| 730 |
+
" Account Name: Angelo Food Court 2\n\n"
|
| 731 |
+
"If you choose the bank transfer option, please send a screenshot of your payment confirmation to this chatbot.\n\n"
|
| 732 |
+
"You can track your order status using your Order ID.\n"
|
| 733 |
+
"Is there anything else you'd like to order?")
|
| 734 |
+
else:
|
| 735 |
+
return (f"Your order has been placed with Order ID {order_id}, "
|
| 736 |
+
"but we could not initialize online payment. Please try again later, or "
|
| 737 |
+
"you may opt to pay via bank transfer to Account Number 1433042821, Access Bank, Angelo Food Court 2 "
|
| 738 |
+
"and send your payment screenshot to this chatbot.")
|
| 739 |
+
|
| 740 |
+
else:
|
| 741 |
+
state.reset()
|
| 742 |
+
if user_id in user_state:
|
| 743 |
+
del user_state[user_id]
|
| 744 |
+
|
| 745 |
+
# Final fallback
|
| 746 |
return ""
|
| 747 |
|
| 748 |
+
|
| 749 |
+
|
| 750 |
+
|
| 751 |
+
def _parse_single_dish_line(message: str, dish_name: str) -> dict:
|
| 752 |
+
|
| 753 |
+
result = {"quantity": None, "phone": None, "address": None}
|
| 754 |
+
|
| 755 |
+
# 1) Parse quantity
|
| 756 |
+
numbers = re.findall(r'\d+', message)
|
| 757 |
+
if numbers:
|
| 758 |
+
# We'll assume the first numeric is quantity if no "for" pattern is used
|
| 759 |
+
result["quantity"] = int(numbers[0])
|
| 760 |
+
|
| 761 |
+
# 2) Parse phone
|
| 762 |
+
phone_pattern = r'(\+?\d{10,15})'
|
| 763 |
+
phone_match = re.search(phone_pattern, message)
|
| 764 |
+
address = None
|
| 765 |
+
if phone_match:
|
| 766 |
+
phone_number = phone_match.group(1)
|
| 767 |
+
address_start = phone_match.end()
|
| 768 |
+
address = message[address_start:].strip()
|
| 769 |
+
address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
|
| 770 |
+
result["phone"] = phone_number
|
| 771 |
+
if address:
|
| 772 |
+
result["address"] = address
|
| 773 |
+
|
| 774 |
+
return result
|
| 775 |
+
|
| 776 |
|
| 777 |
async def get_or_create_user_profile(user_id: str, phone_number: str = None) -> UserProfile:
|
| 778 |
async with async_session() as session:
|