Fred808 commited on
Commit
95a4538
·
verified ·
1 Parent(s): 2ec352b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -430,14 +430,10 @@ def match_dishes(user_input: str, threshold: int = 70) -> list:
430
  return list(set(matched_dishes))
431
 
432
  def get_dish_price(dish: str) -> int:
433
- """
434
- Look up the price for a given dish from the updated menu_items list.
435
- The match is case-insensitive.
436
- """
437
  for item in menu_items:
438
  if item["name"].lower() == dish.lower():
439
  return item["price"]
440
- return 0 # or raise an exception if dish not found
441
 
442
  def send_email_notification(order_details):
443
  # Construct the email payload. Adjust values as needed.
@@ -735,17 +731,15 @@ async def process_order_flow(user_id: str, message: str) -> str:
735
  order_id = f"ORD-{int(time.time())}"
736
  state.data["order_id"] = order_id
737
 
738
- # Calculate total price based on single-dish or multi-dish order
739
  if "orders" in state.data:
740
  total_price = 0
741
  for dish, qty in state.data["orders"].items():
742
  total_price += get_dish_price(dish) * qty
743
- # Add shipping cost (assumed once for the entire order)
744
  total_price += state.data.get("shipping_cost", 0)
745
  dish_summary = ", ".join(state.data["orders"].keys())
746
  quantity_total = sum(state.data["orders"].values())
747
  else:
748
- # Single-dish order
749
  dish_summary = state.data.get("dish", "")
750
  quantity_total = state.data.get("quantity", 1)
751
  total_price = (quantity_total * get_dish_price(dish_summary)) + state.data.get("shipping_cost", 0)
@@ -768,25 +762,24 @@ async def process_order_flow(user_id: str, message: str) -> str:
768
  asyncio.create_task(save_order())
769
  asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
770
 
771
- order_details = (
772
- f"New Order Received:\n"
773
- f"Order ID: {order_id}\n"
774
- f"Dish(es): {dish_summary}\n"
775
- f"Quantity: {quantity_total}\n"
776
- f"Total Price: N{total_price}\n"
777
- f"Phone: {state.data.get('phone_number', '')}\n"
778
- f"Delivery Address: {state.data.get('address', 'Not Provided')}\n"
779
- f"Extras: {state.data.get('extras', 'None')}\n"
780
- f"Status: Pending Payment"
781
- )
782
- email_sent = send_email_notification(order_details)
783
- if email_sent:
784
  print("Email notification sent successfully.")
785
  else:
786
  print("Failed to send email notification.")
787
 
788
  # Generate Paystack payment link (amount in kobo)
789
- email_for_paystack = "customer@example.com" # Replace with user's email if available
790
  payment_data = create_paystack_payment_link(email_for_paystack, total_price * 100, order_id)
791
  state.reset()
792
  if user_id in user_state:
@@ -810,8 +803,10 @@ async def process_order_flow(user_id: str, message: str) -> str:
810
  "you may opt to pay via bank transfer to Account Number 1433042821, Access Bank, Angelo Food Court 2 "
811
  "and send your payment screenshot to this chatbot.")
812
  elif message.lower() in ["no", "n"]:
813
- # Instead of immediately canceling, re-prompt for confirmation.
814
- return "Please confirm your order by typing 'yes' to confirm or 'no' to cancel."
 
 
815
 
816
  return ""
817
 
 
430
  return list(set(matched_dishes))
431
 
432
  def get_dish_price(dish: str) -> int:
 
 
 
 
433
  for item in menu_items:
434
  if item["name"].lower() == dish.lower():
435
  return item["price"]
436
+ return 0 # or raise an error if dish not found
437
 
438
  def send_email_notification(order_details):
439
  # Construct the email payload. Adjust values as needed.
 
731
  order_id = f"ORD-{int(time.time())}"
732
  state.data["order_id"] = order_id
733
 
734
+ # Calculate total price based on whether it's a single-dish or multi-dish order.
735
  if "orders" in state.data:
736
  total_price = 0
737
  for dish, qty in state.data["orders"].items():
738
  total_price += get_dish_price(dish) * qty
 
739
  total_price += state.data.get("shipping_cost", 0)
740
  dish_summary = ", ".join(state.data["orders"].keys())
741
  quantity_total = sum(state.data["orders"].values())
742
  else:
 
743
  dish_summary = state.data.get("dish", "")
744
  quantity_total = state.data.get("quantity", 1)
745
  total_price = (quantity_total * get_dish_price(dish_summary)) + state.data.get("shipping_cost", 0)
 
762
  asyncio.create_task(save_order())
763
  asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
764
 
765
+ # Prepare order details as a dictionary for the email notification.
766
+ order_details = {
767
+ "order_id": order_id,
768
+ "dish": dish_summary,
769
+ "quantity": quantity_total,
770
+ "price": total_price,
771
+ "phone_number": state.data.get("phone_number", ""),
772
+ "address": state.data.get("address", "Not Provided"),
773
+ "extras": state.data.get("extras", "None")
774
+ }
775
+ email_response = send_email_notification(order_details)
776
+ if email_response:
 
777
  print("Email notification sent successfully.")
778
  else:
779
  print("Failed to send email notification.")
780
 
781
  # Generate Paystack payment link (amount in kobo)
782
+ email_for_paystack = "customer@example.com" # Replace as needed
783
  payment_data = create_paystack_payment_link(email_for_paystack, total_price * 100, order_id)
784
  state.reset()
785
  if user_id in user_state:
 
803
  "you may opt to pay via bank transfer to Account Number 1433042821, Access Bank, Angelo Food Court 2 "
804
  "and send your payment screenshot to this chatbot.")
805
  elif message.lower() in ["no", "n"]:
806
+ # Instead of canceling immediately, re-prompt for confirmation.
807
+ return "Please confirm your order by typing 'yes' to confirm your order or 'no' to cancel it."
808
+ else:
809
+ return "I didn't understand that. Please type 'yes' to confirm your order or 'no' to cancel it."
810
 
811
  return ""
812