Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,6 @@ from bs4 import BeautifulSoup
|
|
| 9 |
from sqlalchemy import select
|
| 10 |
from fuzzywuzzy import fuzz
|
| 11 |
from pydantic import BaseModel
|
| 12 |
-
import smtplib
|
| 13 |
-
from email.mime.multipart import MIMEMultipart
|
| 14 |
-
from email.mime.text import MIMEText
|
| 15 |
|
| 16 |
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks, UploadFile, File, Form
|
| 17 |
from fastapi.responses import JSONResponse, StreamingResponse, RedirectResponse
|
|
@@ -411,36 +408,6 @@ def match_dishes(user_input: str, threshold: int = 70) -> list:
|
|
| 411 |
matched_dishes.append(dish_name)
|
| 412 |
return list(set(matched_dishes))
|
| 413 |
|
| 414 |
-
def get_dish_price(dish: str) -> int:
|
| 415 |
-
for item in menu_items:
|
| 416 |
-
if item["name"].lower() == dish.lower():
|
| 417 |
-
return item["price"]
|
| 418 |
-
return 0 # default if not found
|
| 419 |
-
|
| 420 |
-
def send_order_email(admin_emails, subject, body):
|
| 421 |
-
smtp_server = os.getenv("SMTP_SERVER", "smtp.gmail.com")
|
| 422 |
-
smtp_port = int(os.getenv("SMTP_PORT", 587))
|
| 423 |
-
smtp_username = os.getenv("SMTP_USERNAME", "yungdml31@gmail.com")
|
| 424 |
-
smtp_password = os.getenv("SMTP_PASSWORD", "uddvxabxotlvfewk")
|
| 425 |
-
sender_email = os.getenv("SENDER_EMAIL", "yungdml31@gmail.com")
|
| 426 |
-
|
| 427 |
-
msg = MIMEMultipart()
|
| 428 |
-
msg["From"] = sender_email
|
| 429 |
-
msg["To"] = ", ".join(admin_emails)
|
| 430 |
-
msg["Subject"] = subject
|
| 431 |
-
msg.attach(MIMEText(body, "plain"))
|
| 432 |
-
|
| 433 |
-
try:
|
| 434 |
-
server = smtplib.SMTP(smtp_server, smtp_port)
|
| 435 |
-
server.starttls()
|
| 436 |
-
server.login(smtp_username, smtp_password)
|
| 437 |
-
server.sendmail(sender_email, admin_emails, msg.as_string())
|
| 438 |
-
server.quit()
|
| 439 |
-
return True
|
| 440 |
-
except Exception as e:
|
| 441 |
-
print(f"Error sending email: {e}")
|
| 442 |
-
return False
|
| 443 |
-
|
| 444 |
|
| 445 |
async def process_order_flow(user_id: str, message: str) -> str:
|
| 446 |
"""
|
|
@@ -699,7 +666,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 699 |
if message.lower() in ["yes", "y"]:
|
| 700 |
order_id = f"ORD-{int(time.time())}"
|
| 701 |
state.data["order_id"] = order_id
|
| 702 |
-
price_per_serving =
|
| 703 |
quantity = state.data.get("quantity", 1)
|
| 704 |
shipping_cost = state.data.get("shipping_cost", 0)
|
| 705 |
total_price = (quantity * price_per_serving) + shipping_cost
|
|
@@ -721,29 +688,33 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 721 |
asyncio.create_task(save_order())
|
| 722 |
asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
|
| 723 |
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
email_sent = send_order_email(admin_emails, email_subject, email_body)
|
| 742 |
-
|
| 743 |
-
# Create Paystack payment link
|
| 744 |
-
email_for_paystack = "customer@example.com" # Replace with actual email if available
|
| 745 |
-
payment_data = create_paystack_payment_link(email_for_paystack, total_price * 100, order_id)
|
| 746 |
dish_name = state.data.get("dish", "")
|
|
|
|
| 747 |
state.reset()
|
| 748 |
if user_id in user_state:
|
| 749 |
del user_state[user_id]
|
|
@@ -765,12 +736,12 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 765 |
"but we could not initialize online payment. Please try again later, or "
|
| 766 |
"you may opt to pay via bank transfer to Account Number 1433042821, Access Bank, Angelo Food Court 2 "
|
| 767 |
"and send your payment screenshot to this chatbot.")
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
# Final fallback
|
| 775 |
return ""
|
| 776 |
|
|
|
|
| 9 |
from sqlalchemy import select
|
| 10 |
from fuzzywuzzy import fuzz
|
| 11 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks, UploadFile, File, Form
|
| 14 |
from fastapi.responses import JSONResponse, StreamingResponse, RedirectResponse
|
|
|
|
| 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 |
"""
|
|
|
|
| 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
|
|
|
|
| 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]
|
|
|
|
| 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 |
|