Update app.py
Browse files
app.py
CHANGED
|
@@ -384,19 +384,13 @@ def match_dishes(user_input: str, threshold: int = 70) -> list:
|
|
| 384 |
matched_dishes.append(dish_name)
|
| 385 |
return list(set(matched_dishes))
|
| 386 |
|
| 387 |
-
|
| 388 |
async def process_order_flow(user_id: str, message: str) -> str:
|
| 389 |
-
"""
|
| 390 |
-
A unified approach to parse dish(es), quantity, phone, and address
|
| 391 |
-
whether the user provides them all at once or in multiple steps.
|
| 392 |
-
"""
|
| 393 |
state = user_state.get(user_id)
|
| 394 |
if state and state.is_expired():
|
| 395 |
state.reset()
|
| 396 |
del user_state[user_id]
|
| 397 |
state = None
|
| 398 |
|
| 399 |
-
# 1) If user says "menu" or "order", initialize the flow
|
| 400 |
if message.lower() in ["order", "menu"]:
|
| 401 |
state = ConversationState()
|
| 402 |
state.flow = "order"
|
|
@@ -407,7 +401,6 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 407 |
return "Sure! What dish would you like to order?"
|
| 408 |
return ""
|
| 409 |
|
| 410 |
-
# If user says "order" but we have no state
|
| 411 |
if not state and "order" in message.lower():
|
| 412 |
state = ConversationState()
|
| 413 |
state.flow = "order"
|
|
@@ -416,281 +409,235 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 416 |
user_state[user_id] = state
|
| 417 |
return "Sure! What dish would you like to order?"
|
| 418 |
|
| 419 |
-
#
|
| 420 |
if not state or state.flow != "order":
|
| 421 |
-
|
| 422 |
-
if
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
else:
|
| 434 |
-
|
| 435 |
-
found_dish
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
state.data["dish"] = found_dish
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
|
|
|
| 445 |
state.step = 3
|
|
|
|
|
|
|
| 446 |
else:
|
| 447 |
-
# If no quantity found, we still need quantity
|
| 448 |
state.step = 2
|
| 449 |
-
|
| 450 |
-
if single_dish_parse["phone"]:
|
| 451 |
-
state.data["phone_number"] = single_dish_parse["phone"]
|
| 452 |
-
if single_dish_parse["address"]:
|
| 453 |
-
state.data["address"] = single_dish_parse["address"]
|
| 454 |
-
|
| 455 |
-
# Now see how much info we have:
|
| 456 |
-
if state.step == 2 and not state.data.get("quantity"):
|
| 457 |
-
# We must ask for quantity
|
| 458 |
return f"You selected {found_dish}. How many servings would you like?"
|
| 459 |
-
elif state.step == 3:
|
| 460 |
-
# We have a quantity. Check if we also have phone and address
|
| 461 |
-
if state.data.get("phone_number") and state.data.get("address"):
|
| 462 |
-
# We have everything needed to skip to step 5
|
| 463 |
-
shipping_cost = calculate_shipping_cost(state.data["address"])
|
| 464 |
-
state.data["shipping_cost"] = shipping_cost
|
| 465 |
-
state.step = 5
|
| 466 |
-
return (f"Thanks! Your phone number is recorded as: {state.data['phone_number']}.\n"
|
| 467 |
-
f"Your delivery address is: {state.data['address']}.\n"
|
| 468 |
-
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 469 |
-
elif state.data.get("phone_number") and not state.data.get("address"):
|
| 470 |
-
return "Thank you. Please provide your delivery address."
|
| 471 |
-
else:
|
| 472 |
-
return "Please provide your phone number and address."
|
| 473 |
-
else:
|
| 474 |
-
# No dish matched
|
| 475 |
-
return "I couldn't identify the dish. Please type the dish name from our menu."
|
| 476 |
-
|
| 477 |
-
# 3) If we have candidate dishes and user needs to clarify
|
| 478 |
-
if state and state.flow == "order" and "candidate_dishes" in state.data:
|
| 479 |
-
# If user typed "both" => they want all candidate dishes
|
| 480 |
-
if message.strip().lower() in ["both", "all"]:
|
| 481 |
-
state.data["dishes"] = state.data["candidate_dishes"]
|
| 482 |
-
del state.data["candidate_dishes"]
|
| 483 |
-
state.step = 2 # We'll parse "2 for Jollof Rice and 3 for Fried Rice"
|
| 484 |
-
dishes_str = ", ".join(state.data["dishes"])
|
| 485 |
-
return (f"You have selected: {dishes_str}. How many servings of each would you like? "
|
| 486 |
-
"(For example, '2 for Jollof Rice and 3 for Fried Rice')")
|
| 487 |
-
else:
|
| 488 |
-
# Maybe the user typed a single dish out of the candidates
|
| 489 |
-
for dish in state.data["candidate_dishes"]:
|
| 490 |
-
if dish.lower() in message.lower():
|
| 491 |
-
state.data["dish"] = dish
|
| 492 |
-
del state.data["candidate_dishes"]
|
| 493 |
-
state.step = 2
|
| 494 |
-
return f"You selected {dish}. How many servings would you like?"
|
| 495 |
-
# If still unclear
|
| 496 |
-
dish_options = ", ".join(state.data["candidate_dishes"])
|
| 497 |
-
return (f"Please specify which one you'd like to order from: {dish_options} "
|
| 498 |
-
"(or type 'both' if you'd like to order all).")
|
| 499 |
-
|
| 500 |
-
# 4) If step == 2 for multiple dishes => parse "2 for Jollof Rice and 3 for Fried Rice"
|
| 501 |
-
if state and state.flow == "order" and state.step == 2:
|
| 502 |
-
if "dishes" in state.data and len(state.data["dishes"]) > 1:
|
| 503 |
-
pairs = re.findall(r'(\d+)\s*for\s*([a-zA-Z\s]+)', message, flags=re.IGNORECASE)
|
| 504 |
-
if not pairs:
|
| 505 |
-
return ("I'm sorry, I didn't understand the quantity details. "
|
| 506 |
-
"Please specify like '2 for Jollof Rice and 3 for Fried Rice'.")
|
| 507 |
-
order_quantities = {}
|
| 508 |
-
for quantity, dish_text in pairs:
|
| 509 |
-
dish_text = dish_text.strip().lower()
|
| 510 |
-
for candidate in state.data["dishes"]:
|
| 511 |
-
if candidate.lower() in dish_text or dish_text in candidate.lower():
|
| 512 |
-
order_quantities[candidate] = int(quantity)
|
| 513 |
-
if order_quantities:
|
| 514 |
-
state.data["orders"] = order_quantities
|
| 515 |
-
state.step = 3
|
| 516 |
-
summary = "\n".join([f"{q} serving(s) of {d}" for d, q in order_quantities.items()])
|
| 517 |
-
return (f"Got it. You have ordered:\n{summary}\n"
|
| 518 |
-
"Please provide your phone number and delivery address.")
|
| 519 |
else:
|
| 520 |
-
return
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
state.data["
|
|
|
|
|
|
|
| 554 |
else:
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
"For example: '09162409591, 1, Iyana Isashi, Isashi, Lagos'.")
|
| 558 |
-
|
| 559 |
-
if "address" not in state.data:
|
| 560 |
-
# If we only found phone but no address, ask for address
|
| 561 |
-
return "Thank you. Please provide your delivery address."
|
| 562 |
-
|
| 563 |
-
# If we have phone + address, move to step 5 => ask extras
|
| 564 |
-
shipping_cost = calculate_shipping_cost(state.data["address"])
|
| 565 |
-
state.data["shipping_cost"] = shipping_cost
|
| 566 |
-
state.step = 5
|
| 567 |
-
return (f"Thanks! Your phone number is recorded as: {state.data['phone_number']}.\n"
|
| 568 |
-
f"Your delivery address is: {state.data['address']}.\n"
|
| 569 |
-
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 570 |
|
| 571 |
-
#
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
state.step =
|
| 578 |
-
return "
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 583 |
state.step = 7
|
| 584 |
-
# Summarize the order so far
|
| 585 |
-
# If you handled multiple dishes, you'd need to show all.
|
| 586 |
-
# For single dish, just show dish + quantity:
|
| 587 |
dish = state.data.get("dish", "")
|
| 588 |
quantity = state.data.get("quantity", 1)
|
| 589 |
phone = state.data.get("phone_number", "")
|
| 590 |
address = state.data.get("address", "")
|
| 591 |
shipping_cost = state.data.get("shipping_cost", 0)
|
| 592 |
-
extras = "
|
| 593 |
-
|
| 594 |
-
# If you used state.data["orders"] for multiple dishes,
|
| 595 |
-
# you'd build a summary from that dictionary.
|
| 596 |
-
|
| 597 |
price_per_serving = 1500
|
| 598 |
total_price = (quantity * price_per_serving) + shipping_cost
|
| 599 |
-
summary = (f"Order Summary:\n"
|
| 600 |
-
f"
|
| 601 |
-
f"Quantity: {quantity}\n"
|
| 602 |
-
f"Phone: {phone}\n"
|
| 603 |
-
f"Address: {address}\n"
|
| 604 |
f"Shipping Cost: N{shipping_cost}\n"
|
| 605 |
f"Total Price: N{total_price}\n"
|
| 606 |
-
f"Extras: {extras}\
|
| 607 |
-
f"Confirm order? (yes/no)")
|
| 608 |
return summary
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
state.step = 7
|
| 619 |
-
dish = state.data.get("dish", "")
|
| 620 |
-
quantity = state.data.get("quantity", 1)
|
| 621 |
-
phone = state.data.get("phone_number", "")
|
| 622 |
-
address = state.data.get("address", "")
|
| 623 |
-
shipping_cost = state.data.get("shipping_cost", 0)
|
| 624 |
-
extras = state.data.get("extras", "None")
|
| 625 |
-
|
| 626 |
-
price_per_serving = 1500
|
| 627 |
-
total_price = (quantity * price_per_serving) + shipping_cost
|
| 628 |
-
summary = (f"Order Summary:\n"
|
| 629 |
-
f"Dish: {dish}\n"
|
| 630 |
-
f"Quantity: {quantity}\n"
|
| 631 |
-
f"Phone: {phone}\n"
|
| 632 |
-
f"Address: {address}\n"
|
| 633 |
-
f"Shipping Cost: N{shipping_cost}\n"
|
| 634 |
-
f"Total Price: N{total_price}\n"
|
| 635 |
-
f"Extras: {extras}\n"
|
| 636 |
-
f"Confirm order? (yes/no)")
|
| 637 |
-
return summary
|
| 638 |
-
|
| 639 |
-
# Step 7: Confirm or cancel
|
| 640 |
-
if state and state.flow == "order" and state.step == 7:
|
| 641 |
-
if message.lower() in ["yes", "y"]:
|
| 642 |
-
# Save order, generate payment link, etc.
|
| 643 |
-
# ...
|
| 644 |
-
# Example:
|
| 645 |
-
order_id = f"ORD-{int(time.time())}"
|
| 646 |
-
# Create DB entry, etc.
|
| 647 |
-
state.reset()
|
| 648 |
-
if user_id in user_state:
|
| 649 |
-
del user_state[user_id]
|
| 650 |
-
return (f"Thank you! Your order ID is {order_id}. "
|
| 651 |
-
"Please complete payment at <some-link>.\n"
|
| 652 |
-
"Anything else you'd like to order?")
|
| 653 |
-
else:
|
| 654 |
-
# User canceled at confirmation
|
| 655 |
-
state.reset()
|
| 656 |
-
if user_id in user_state:
|
| 657 |
-
del user_state[user_id]
|
| 658 |
-
return "Order canceled. Let me know if you'd like to order again."
|
| 659 |
-
|
| 660 |
-
# Final fallback
|
| 661 |
-
return ""
|
| 662 |
-
|
| 663 |
-
|
| 664 |
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 694 |
|
| 695 |
|
| 696 |
async def get_or_create_user_profile(user_id: str, phone_number: str = None) -> UserProfile:
|
|
|
|
| 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 |
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 |
user_state[user_id] = state
|
| 410 |
return "Sure! What dish would you like to order?"
|
| 411 |
|
| 412 |
+
# Use fuzzy matching to detect a dish even with typos
|
| 413 |
if not state or state.flow != "order":
|
| 414 |
+
found_dish = match_dish(message)
|
| 415 |
+
if found_dish:
|
| 416 |
+
state = ConversationState()
|
| 417 |
+
state.flow = "order"
|
| 418 |
+
state.data["dish"] = found_dish
|
| 419 |
+
state.update_last_active()
|
| 420 |
+
user_state[user_id] = state
|
| 421 |
+
numbers = re.findall(r'\d+', message)
|
| 422 |
+
if numbers:
|
| 423 |
+
quantity = int(numbers[0])
|
| 424 |
+
if quantity <= 0:
|
| 425 |
+
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 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 |
+
state.step = 2
|
| 455 |
+
return f"You selected {found_dish}. How many servings would you like?"
|
| 456 |
+
|
| 457 |
+
# If state exists and we're already in order flow:
|
| 458 |
+
if state and state.flow == "order":
|
| 459 |
+
state.update_last_active()
|
| 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 numbers:
|
| 467 |
+
quantity = int(numbers[0])
|
| 468 |
+
if quantity <= 0:
|
| 469 |
+
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 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 identify the dish. Please type the dish name from our menu."
|
| 479 |
+
|
| 480 |
+
if state.step == 2:
|
| 481 |
+
numbers = re.findall(r'\d+', message)
|
| 482 |
+
if not numbers:
|
| 483 |
+
return "Please enter a valid number for the quantity (e.g., 1, 2, 3)."
|
| 484 |
+
quantity = int(numbers[0])
|
| 485 |
+
if quantity <= 0:
|
| 486 |
+
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 487 |
+
state.data["quantity"] = quantity
|
| 488 |
+
state.step = 3
|
| 489 |
+
return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number and delivery address."
|
| 490 |
+
|
| 491 |
+
if state.step == 3:
|
| 492 |
+
phone_pattern = r'(\+?\d{10,15})'
|
| 493 |
+
phone_match = re.search(phone_pattern, message)
|
| 494 |
+
address = None
|
| 495 |
+
if phone_match:
|
| 496 |
+
phone_number = phone_match.group(1)
|
| 497 |
+
address_start = phone_match.end()
|
| 498 |
+
address = message[address_start:].strip()
|
| 499 |
+
address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
|
| 500 |
+
if phone_match and address:
|
| 501 |
+
state.data["phone_number"] = phone_number
|
| 502 |
+
state.data["address"] = address
|
| 503 |
+
asyncio.create_task(update_user_profile(user_id, phone_number, address))
|
| 504 |
+
shipping_cost = calculate_shipping_cost(address)
|
| 505 |
+
state.data["shipping_cost"] = shipping_cost
|
| 506 |
+
state.step = 5
|
| 507 |
+
return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
|
| 508 |
+
f"Your delivery address is: {address}.\n"
|
| 509 |
+
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 510 |
+
elif phone_match:
|
| 511 |
+
state.data["phone_number"] = phone_match.group(1)
|
| 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 |
+
return "Please respond with 'yes' or 'no' regarding extras."
|
| 550 |
+
|
| 551 |
+
if state.step == 6:
|
| 552 |
+
state.data["extras"] = message
|
| 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 = state.data.get("extras", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
price_per_serving = 1500
|
| 561 |
total_price = (quantity * price_per_serving) + shipping_cost
|
| 562 |
+
summary = (f"Order Summary:\nDish: {dish}\nQuantity: {quantity}\n"
|
| 563 |
+
f"Phone: {phone}\nAddress: {address}\n"
|
|
|
|
|
|
|
|
|
|
| 564 |
f"Shipping Cost: N{shipping_cost}\n"
|
| 565 |
f"Total Price: N{total_price}\n"
|
| 566 |
+
f"Extras: {extras}\nConfirm order? (yes/no)")
|
|
|
|
| 567 |
return summary
|
| 568 |
+
|
| 569 |
+
if state.step == 7:
|
| 570 |
+
if message.lower() in ["yes", "y"]:
|
| 571 |
+
order_id = f"ORD-{int(time.time())}"
|
| 572 |
+
state.data["order_id"] = order_id
|
| 573 |
+
price_per_serving = 1500
|
| 574 |
+
quantity = state.data.get("quantity", 1)
|
| 575 |
+
total_price = quantity * price_per_serving # Removed shipping cost
|
| 576 |
+
state.data["price"] = str(total_price)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
+
async def save_order():
|
| 579 |
+
async with async_session() as session:
|
| 580 |
+
order = Order(
|
| 581 |
+
order_id=order_id,
|
| 582 |
+
user_id=user_id,
|
| 583 |
+
dish=state.data["dish"],
|
| 584 |
+
quantity=str(quantity),
|
| 585 |
+
price=str(total_price),
|
| 586 |
+
status="Pending Payment",
|
| 587 |
+
delivery_address=state.data.get("address", "")
|
| 588 |
+
)
|
| 589 |
+
session.add(order)
|
| 590 |
+
await session.commit()
|
| 591 |
+
|
| 592 |
+
# Await the save_order task to ensure the order is committed before proceeding.
|
| 593 |
+
await save_order()
|
| 594 |
+
|
| 595 |
+
asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
|
| 596 |
+
|
| 597 |
+
async def notify_management_order(order_details: dict):
|
| 598 |
+
message_body = (
|
| 599 |
+
f"New Order Received:\n"
|
| 600 |
+
f"Order ID: {order_details['order_id']}\n"
|
| 601 |
+
f"Dish: {order_details['dish']}\n"
|
| 602 |
+
f"Quantity: {order_details['quantity']}\n"
|
| 603 |
+
f"Total Price: {order_details['price']}\n"
|
| 604 |
+
f"Phone: {state.data.get('phone_number', '')}\n"
|
| 605 |
+
f"Delivery Address: {order_details.get('address', 'Not Provided')}\n"
|
| 606 |
+
f"Extras: {state.data.get('extras', 'None')}\n"
|
| 607 |
+
f"Status: Pending Payment"
|
| 608 |
+
)
|
| 609 |
+
await asyncio.to_thread(send_whatsapp_message, MANAGEMENT_WHATSAPP_NUMBER, message_body)
|
| 610 |
+
|
| 611 |
+
order_details = {
|
| 612 |
+
"order_id": order_id,
|
| 613 |
+
"dish": state.data["dish"],
|
| 614 |
+
"quantity": state.data["quantity"],
|
| 615 |
+
"price": state.data["price"],
|
| 616 |
+
"address": state.data.get("address", "")
|
| 617 |
+
}
|
| 618 |
+
asyncio.create_task(notify_management_order(order_details))
|
| 619 |
+
|
| 620 |
+
email = "customer@example.com"
|
| 621 |
+
payment_data = create_paystack_payment_link(email, total_price * 100, order_id)
|
| 622 |
+
dish_name = state.data.get("dish", "")
|
| 623 |
+
state.reset()
|
| 624 |
+
if user_id in user_state:
|
| 625 |
+
del user_state[user_id]
|
| 626 |
+
if payment_data.get("status"):
|
| 627 |
+
payment_link = payment_data["data"]["authorization_url"]
|
| 628 |
+
return (f"Thank you for your order of {quantity} serving(s) of {dish_name}! "
|
| 629 |
+
f"Your Order ID is {order_id}.\nPlease complete payment here: {payment_link}\n"
|
| 630 |
+
"You can track your order status using your Order ID.\n"
|
| 631 |
+
"Is there anything else you'd like to order?")
|
| 632 |
+
else:
|
| 633 |
+
return (f"Your order has been placed with Order ID {order_id}, "
|
| 634 |
+
"but we could not initialize payment. Please try again later.")
|
| 635 |
+
else:
|
| 636 |
+
state.reset()
|
| 637 |
+
if user_id in user_state:
|
| 638 |
+
del user_state[user_id]
|
| 639 |
+
return "Order canceled. Let me know if you'd like to try again."
|
| 640 |
+
return ""
|
| 641 |
|
| 642 |
|
| 643 |
async def get_or_create_user_profile(user_id: str, phone_number: str = None) -> UserProfile:
|