Update app.py
Browse files
app.py
CHANGED
|
@@ -252,6 +252,22 @@ async def log_order_tracking(order_id: str, status: str, message: str = None):
|
|
| 252 |
session.add(tracking_entry)
|
| 253 |
await session.commit()
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
def calculate_shipping_cost(address: str) -> int:
|
| 256 |
address_lower = address.lower()
|
| 257 |
for area, cost in TOWN_SHIPPING_COSTS.items():
|
|
@@ -380,13 +396,9 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 380 |
user_state[user_id] = state
|
| 381 |
return "Sure! What dish would you like to order?"
|
| 382 |
|
|
|
|
| 383 |
if not state or state.flow != "order":
|
| 384 |
-
|
| 385 |
-
found_dish = None
|
| 386 |
-
for dish in dish_candidates:
|
| 387 |
-
if dish.lower() in message.lower():
|
| 388 |
-
found_dish = dish
|
| 389 |
-
break
|
| 390 |
if found_dish:
|
| 391 |
state = ConversationState()
|
| 392 |
state.flow = "order"
|
|
@@ -417,7 +429,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 417 |
state.step = 5
|
| 418 |
return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
|
| 419 |
f"Your delivery address is: {address}.\n"
|
| 420 |
-
f"Your delivery cost is N{shipping_cost}. Would you like to add
|
| 421 |
elif phone_match:
|
| 422 |
state.data["phone_number"] = phone_match.group(1)
|
| 423 |
asyncio.create_task(update_user_profile(user_id, phone_number))
|
|
@@ -428,16 +440,13 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 428 |
else:
|
| 429 |
state.step = 2
|
| 430 |
return f"You selected {found_dish}. How many servings would you like?"
|
| 431 |
-
|
|
|
|
| 432 |
if state and state.flow == "order":
|
| 433 |
state.update_last_active()
|
| 434 |
if state.step == 1:
|
| 435 |
-
|
| 436 |
-
found_dish =
|
| 437 |
-
for dish in dish_candidates:
|
| 438 |
-
if dish.lower() in message.lower():
|
| 439 |
-
found_dish = dish
|
| 440 |
-
break
|
| 441 |
numbers = re.findall(r'\d+', message)
|
| 442 |
if found_dish:
|
| 443 |
state.data["dish"] = found_dish
|
|
@@ -454,7 +463,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 454 |
return f"You selected {found_dish}. How many servings would you like?"
|
| 455 |
else:
|
| 456 |
return "I couldn't identify the dish. Please type the dish name from our menu."
|
| 457 |
-
|
| 458 |
if state.step == 2:
|
| 459 |
numbers = re.findall(r'\d+', message)
|
| 460 |
if not numbers:
|
|
@@ -465,7 +474,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 465 |
state.data["quantity"] = quantity
|
| 466 |
state.step = 3
|
| 467 |
return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number and delivery address."
|
| 468 |
-
|
| 469 |
if state.step == 3:
|
| 470 |
phone_pattern = r'(\+?\d{10,15})'
|
| 471 |
phone_match = re.search(phone_pattern, message)
|
|
@@ -484,7 +493,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 484 |
state.step = 5
|
| 485 |
return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
|
| 486 |
f"Your delivery address is: {address}.\n"
|
| 487 |
-
f"Your delivery cost is N{shipping_cost}. Would you like
|
| 488 |
elif phone_match:
|
| 489 |
state.data["phone_number"] = phone_match.group(1)
|
| 490 |
asyncio.create_task(update_user_profile(user_id, phone_number))
|
|
@@ -492,7 +501,8 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 492 |
else:
|
| 493 |
return ("Please provide both your phone number and delivery address. "
|
| 494 |
"For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'.")
|
| 495 |
-
|
|
|
|
| 496 |
if state.step == 4:
|
| 497 |
state.data["address"] = message
|
| 498 |
asyncio.create_task(update_user_profile(user_id, address=message))
|
|
@@ -500,12 +510,12 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 500 |
state.data["shipping_cost"] = shipping_cost
|
| 501 |
state.step = 5
|
| 502 |
return (f"Thanks. Your delivery address is recorded as: {message}.\n"
|
| 503 |
-
f"Your delivery cost is N{shipping_cost}. Would you like
|
| 504 |
-
|
| 505 |
if state.step == 5:
|
| 506 |
if message.lower() in ["yes", "y"]:
|
| 507 |
state.step = 6
|
| 508 |
-
return "Please list the extras you
|
| 509 |
elif message.lower() in ["no", "n"]:
|
| 510 |
state.data["extras"] = ""
|
| 511 |
state.step = 7
|
|
@@ -523,8 +533,8 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 523 |
f"Extras: None\nConfirm order? (yes/no)")
|
| 524 |
return summary
|
| 525 |
else:
|
| 526 |
-
return "Please respond with 'yes' or 'no'
|
| 527 |
-
|
| 528 |
if state.step == 6:
|
| 529 |
state.data["extras"] = message
|
| 530 |
state.step = 7
|
|
@@ -542,7 +552,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 542 |
f"Total Price: N{total_price}\n"
|
| 543 |
f"Extras: {extras}\nConfirm order? (yes/no)")
|
| 544 |
return summary
|
| 545 |
-
|
| 546 |
if state.step == 7:
|
| 547 |
if message.lower() in ["yes", "y"]:
|
| 548 |
order_id = f"ORD-{int(time.time())}"
|
|
@@ -552,7 +562,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 552 |
shipping_cost = state.data.get("shipping_cost", 0)
|
| 553 |
total_price = (quantity * price_per_serving) + shipping_cost
|
| 554 |
state.data["price"] = str(total_price)
|
| 555 |
-
|
| 556 |
async def save_order():
|
| 557 |
async with async_session() as session:
|
| 558 |
order = Order(
|
|
@@ -569,7 +579,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 569 |
await session.commit()
|
| 570 |
asyncio.create_task(save_order())
|
| 571 |
asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
|
| 572 |
-
|
| 573 |
async def notify_management_order(order_details: dict):
|
| 574 |
message_body = (
|
| 575 |
f"New Order Received:\n"
|
|
@@ -591,7 +601,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 591 |
"address": state.data.get("address", "")
|
| 592 |
}
|
| 593 |
asyncio.create_task(notify_management_order(order_details))
|
| 594 |
-
|
| 595 |
email = "customer@example.com"
|
| 596 |
payment_data = create_paystack_payment_link(email, total_price * 100, order_id)
|
| 597 |
dish_name = state.data.get("dish", "")
|
|
|
|
| 252 |
session.add(tracking_entry)
|
| 253 |
await session.commit()
|
| 254 |
|
| 255 |
+
from fuzzywuzzy import fuzz
|
| 256 |
+
|
| 257 |
+
def match_dish(user_input: str, threshold: int = 80) -> str:
|
| 258 |
+
best_match = None
|
| 259 |
+
best_score = 0
|
| 260 |
+
for item in menu_items:
|
| 261 |
+
dish_name = item["name"]
|
| 262 |
+
score = fuzz.ratio(user_input.lower(), dish_name.lower())
|
| 263 |
+
if score > best_score:
|
| 264 |
+
best_score = score
|
| 265 |
+
best_match = dish_name
|
| 266 |
+
if best_score >= threshold:
|
| 267 |
+
return best_match
|
| 268 |
+
return None
|
| 269 |
+
|
| 270 |
+
|
| 271 |
def calculate_shipping_cost(address: str) -> int:
|
| 272 |
address_lower = address.lower()
|
| 273 |
for area, cost in TOWN_SHIPPING_COSTS.items():
|
|
|
|
| 396 |
user_state[user_id] = state
|
| 397 |
return "Sure! What dish would you like to order?"
|
| 398 |
|
| 399 |
+
# If no state exists or not in order flow, try to detect a dish using fuzzy matching.
|
| 400 |
if not state or state.flow != "order":
|
| 401 |
+
found_dish = match_dish(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
if found_dish:
|
| 403 |
state = ConversationState()
|
| 404 |
state.flow = "order"
|
|
|
|
| 429 |
state.step = 5
|
| 430 |
return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
|
| 431 |
f"Your delivery address is: {address}.\n"
|
| 432 |
+
f"Your delivery cost is N{shipping_cost}. Would you like to add extras (yes/no)?")
|
| 433 |
elif phone_match:
|
| 434 |
state.data["phone_number"] = phone_match.group(1)
|
| 435 |
asyncio.create_task(update_user_profile(user_id, phone_number))
|
|
|
|
| 440 |
else:
|
| 441 |
state.step = 2
|
| 442 |
return f"You selected {found_dish}. How many servings would you like?"
|
| 443 |
+
|
| 444 |
+
# If state exists and we're already in order flow:
|
| 445 |
if state and state.flow == "order":
|
| 446 |
state.update_last_active()
|
| 447 |
if state.step == 1:
|
| 448 |
+
# Use fuzzy matching on the message in case of typos.
|
| 449 |
+
found_dish = match_dish(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
numbers = re.findall(r'\d+', message)
|
| 451 |
if found_dish:
|
| 452 |
state.data["dish"] = found_dish
|
|
|
|
| 463 |
return f"You selected {found_dish}. How many servings would you like?"
|
| 464 |
else:
|
| 465 |
return "I couldn't identify the dish. Please type the dish name from our menu."
|
| 466 |
+
|
| 467 |
if state.step == 2:
|
| 468 |
numbers = re.findall(r'\d+', message)
|
| 469 |
if not numbers:
|
|
|
|
| 474 |
state.data["quantity"] = quantity
|
| 475 |
state.step = 3
|
| 476 |
return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number and delivery address."
|
| 477 |
+
|
| 478 |
if state.step == 3:
|
| 479 |
phone_pattern = r'(\+?\d{10,15})'
|
| 480 |
phone_match = re.search(phone_pattern, message)
|
|
|
|
| 493 |
state.step = 5
|
| 494 |
return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
|
| 495 |
f"Your delivery address is: {address}.\n"
|
| 496 |
+
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 497 |
elif phone_match:
|
| 498 |
state.data["phone_number"] = phone_match.group(1)
|
| 499 |
asyncio.create_task(update_user_profile(user_id, phone_number))
|
|
|
|
| 501 |
else:
|
| 502 |
return ("Please provide both your phone number and delivery address. "
|
| 503 |
"For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'.")
|
| 504 |
+
|
| 505 |
+
# Steps 4, 5, 6, and 7 remain unchanged.
|
| 506 |
if state.step == 4:
|
| 507 |
state.data["address"] = message
|
| 508 |
asyncio.create_task(update_user_profile(user_id, address=message))
|
|
|
|
| 510 |
state.data["shipping_cost"] = shipping_cost
|
| 511 |
state.step = 5
|
| 512 |
return (f"Thanks. Your delivery address is recorded as: {message}.\n"
|
| 513 |
+
f"Your delivery cost is N{shipping_cost}. Would you like extras (yes/no)?")
|
| 514 |
+
|
| 515 |
if state.step == 5:
|
| 516 |
if message.lower() in ["yes", "y"]:
|
| 517 |
state.step = 6
|
| 518 |
+
return "Please list the extras you'd like (e.g., drinks, sides)."
|
| 519 |
elif message.lower() in ["no", "n"]:
|
| 520 |
state.data["extras"] = ""
|
| 521 |
state.step = 7
|
|
|
|
| 533 |
f"Extras: None\nConfirm order? (yes/no)")
|
| 534 |
return summary
|
| 535 |
else:
|
| 536 |
+
return "Please respond with 'yes' or 'no' regarding extras."
|
| 537 |
+
|
| 538 |
if state.step == 6:
|
| 539 |
state.data["extras"] = message
|
| 540 |
state.step = 7
|
|
|
|
| 552 |
f"Total Price: N{total_price}\n"
|
| 553 |
f"Extras: {extras}\nConfirm order? (yes/no)")
|
| 554 |
return summary
|
| 555 |
+
|
| 556 |
if state.step == 7:
|
| 557 |
if message.lower() in ["yes", "y"]:
|
| 558 |
order_id = f"ORD-{int(time.time())}"
|
|
|
|
| 562 |
shipping_cost = state.data.get("shipping_cost", 0)
|
| 563 |
total_price = (quantity * price_per_serving) + shipping_cost
|
| 564 |
state.data["price"] = str(total_price)
|
| 565 |
+
|
| 566 |
async def save_order():
|
| 567 |
async with async_session() as session:
|
| 568 |
order = Order(
|
|
|
|
| 579 |
await session.commit()
|
| 580 |
asyncio.create_task(save_order())
|
| 581 |
asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
|
| 582 |
+
|
| 583 |
async def notify_management_order(order_details: dict):
|
| 584 |
message_body = (
|
| 585 |
f"New Order Received:\n"
|
|
|
|
| 601 |
"address": state.data.get("address", "")
|
| 602 |
}
|
| 603 |
asyncio.create_task(notify_management_order(order_details))
|
| 604 |
+
|
| 605 |
email = "customer@example.com"
|
| 606 |
payment_data = create_paystack_payment_link(email, total_price * 100, order_id)
|
| 607 |
dish_name = state.data.get("dish", "")
|