Update app.py
Browse files
app.py
CHANGED
@@ -33,6 +33,16 @@ WHATSAPP_PHONE_NUMBER_ID = os.getenv("WHATSAPP_PHONE_NUMBER_ID", "default_value"
|
|
33 |
WHATSAPP_ACCESS_TOKEN = os.getenv("WHATSAPP_ACCESS_TOKEN", "default_value")
|
34 |
MANAGEMENT_WHATSAPP_NUMBER = os.getenv("MANAGEMENT_WHATSAPP_NUMBER", "default_value")
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# --- Database Setup ---
|
37 |
Base = declarative_base()
|
38 |
|
@@ -255,16 +265,19 @@ async def log_order_tracking(order_id: str, status: str, message: str = None):
|
|
255 |
await session.commit()
|
256 |
|
257 |
# --- Advanced Internal Flow: Order Processing & Payment Integration ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
def process_order_flow(user_id: str, message: str) -> str:
|
259 |
"""
|
260 |
-
Implements an FSM-based order flow
|
261 |
-
- Step 1: Expects the user to mention a dish name (optionally with quantity)
|
262 |
-
- Step 2: Asks for quantity if not provided
|
263 |
-
- Step 3: Requests the user's phone number if not already provided
|
264 |
-
- Step 4: Requests the delivery address and validates that it includes "OJO LGA, Lagos"
|
265 |
-
- Step 5: Asks if they would like to add any extras (e.g., sides or drinks)
|
266 |
-
- Step 6: Captures extras details if applicable
|
267 |
-
- Step 7: Asks for order confirmation and finalizes the order
|
268 |
"""
|
269 |
# Retrieve or initialize conversation state
|
270 |
state = user_state.get(user_id)
|
@@ -361,17 +374,19 @@ def process_order_flow(user_id: str, message: str) -> str:
|
|
361 |
if re.match(phone_pattern, message.strip()):
|
362 |
state.data["phone_number"] = message.strip()
|
363 |
state.step = 4
|
364 |
-
return "Thank you. Please provide your delivery address.
|
365 |
else:
|
366 |
return "Please enter a valid phone number (e.g., +234XXXXXXXXXX or 0XXXXXXXXXX)."
|
367 |
|
368 |
-
# --- Step 4: Requesting Delivery Address
|
369 |
if state.step == 4:
|
370 |
-
if "ojo lga, lagos" not in message.lower():
|
371 |
-
return "We only deliver to OJO LGA, Lagos. Please provide a valid delivery address that includes 'OJO LGA, Lagos'."
|
372 |
state.data["address"] = message
|
|
|
|
|
|
|
373 |
state.step = 5
|
374 |
-
return f"Thanks. Your delivery address is recorded as: {message}
|
|
|
375 |
|
376 |
# --- Step 5: Asking if They Want Extras ---
|
377 |
if state.step == 5:
|
@@ -385,8 +400,14 @@ def process_order_flow(user_id: str, message: str) -> str:
|
|
385 |
quantity = state.data.get("quantity", 1)
|
386 |
phone = state.data.get("phone_number", "")
|
387 |
address = state.data.get("address", "")
|
|
|
|
|
|
|
388 |
summary = (f"Order Summary:\nDish: {dish}\nQuantity: {quantity}\n"
|
389 |
-
f"Phone: {phone}\nAddress: {address}\
|
|
|
|
|
|
|
390 |
return summary
|
391 |
else:
|
392 |
return "Please respond with 'yes' or 'no'. Would you like to add any extras to your order? (yes/no)"
|
@@ -399,9 +420,15 @@ def process_order_flow(user_id: str, message: str) -> str:
|
|
399 |
quantity = state.data.get("quantity", 1)
|
400 |
phone = state.data.get("phone_number", "")
|
401 |
address = state.data.get("address", "")
|
|
|
402 |
extras = state.data.get("extras", "")
|
|
|
|
|
403 |
summary = (f"Order Summary:\nDish: {dish}\nQuantity: {quantity}\n"
|
404 |
-
f"Phone: {phone}\nAddress: {address}\
|
|
|
|
|
|
|
405 |
return summary
|
406 |
|
407 |
# --- Step 7: Order Confirmation & Finalization ---
|
@@ -409,12 +436,13 @@ def process_order_flow(user_id: str, message: str) -> str:
|
|
409 |
if message.lower() in ["yes", "y"]:
|
410 |
order_id = f"ORD-{int(time.time())}"
|
411 |
state.data["order_id"] = order_id
|
412 |
-
price_per_serving = 1500 #
|
413 |
quantity = state.data.get("quantity", 1)
|
414 |
-
|
|
|
415 |
state.data["price"] = str(total_price)
|
416 |
|
417 |
-
# Save the order asynchronously (including delivery_address, phone, extras)
|
418 |
async def save_order():
|
419 |
async with async_session() as session:
|
420 |
order = Order(
|
@@ -424,7 +452,8 @@ def process_order_flow(user_id: str, message: str) -> str:
|
|
424 |
quantity=str(quantity),
|
425 |
price=str(total_price),
|
426 |
status="Pending Payment",
|
427 |
-
delivery_address=state.data.get("address", "")
|
|
|
428 |
)
|
429 |
session.add(order)
|
430 |
await session.commit()
|
|
|
33 |
WHATSAPP_ACCESS_TOKEN = os.getenv("WHATSAPP_ACCESS_TOKEN", "default_value")
|
34 |
MANAGEMENT_WHATSAPP_NUMBER = os.getenv("MANAGEMENT_WHATSAPP_NUMBER", "default_value")
|
35 |
|
36 |
+
# Synthetic town prices for shipping costs
|
37 |
+
TOWN_SHIPPING_COSTS = {
|
38 |
+
"lasu gate": 1000, # N1,000 for LASU Gate
|
39 |
+
"ojo": 800, # N800 for Ojo
|
40 |
+
"ajangbadi": 1200, # N1,200 for Ajangbadi
|
41 |
+
"iba": 900, # N900 for Iba
|
42 |
+
"okokomaiko": 1500, # N1,500 for Okokomaiko
|
43 |
+
"default": 1000 # Default shipping cost for unknown areas
|
44 |
+
}
|
45 |
+
|
46 |
# --- Database Setup ---
|
47 |
Base = declarative_base()
|
48 |
|
|
|
265 |
await session.commit()
|
266 |
|
267 |
# --- Advanced Internal Flow: Order Processing & Payment Integration ---
|
268 |
+
def calculate_shipping_cost(address: str) -> int:
|
269 |
+
"""
|
270 |
+
Calculate shipping cost based on the user's address.
|
271 |
+
"""
|
272 |
+
address_lower = address.lower()
|
273 |
+
for area, cost in TOWN_SHIPPING_COSTS.items():
|
274 |
+
if area in address_lower:
|
275 |
+
return cost
|
276 |
+
return TOWN_SHIPPING_COSTS["default"] # Default shipping cost for unknown areas
|
277 |
+
|
278 |
def process_order_flow(user_id: str, message: str) -> str:
|
279 |
"""
|
280 |
+
Implements an FSM-based order flow with shipping cost calculation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
"""
|
282 |
# Retrieve or initialize conversation state
|
283 |
state = user_state.get(user_id)
|
|
|
374 |
if re.match(phone_pattern, message.strip()):
|
375 |
state.data["phone_number"] = message.strip()
|
376 |
state.step = 4
|
377 |
+
return "Thank you. Please provide your delivery address."
|
378 |
else:
|
379 |
return "Please enter a valid phone number (e.g., +234XXXXXXXXXX or 0XXXXXXXXXX)."
|
380 |
|
381 |
+
# --- Step 4: Requesting Delivery Address ---
|
382 |
if state.step == 4:
|
|
|
|
|
383 |
state.data["address"] = message
|
384 |
+
# Calculate shipping cost based on the address
|
385 |
+
shipping_cost = calculate_shipping_cost(message)
|
386 |
+
state.data["shipping_cost"] = shipping_cost
|
387 |
state.step = 5
|
388 |
+
return (f"Thanks. Your delivery address is recorded as: {message}.\n"
|
389 |
+
f"Your delivery cost is N{shipping_cost}. Would you like to add any extras such as sides or drinks? (yes/no)")
|
390 |
|
391 |
# --- Step 5: Asking if They Want Extras ---
|
392 |
if state.step == 5:
|
|
|
400 |
quantity = state.data.get("quantity", 1)
|
401 |
phone = state.data.get("phone_number", "")
|
402 |
address = state.data.get("address", "")
|
403 |
+
shipping_cost = state.data.get("shipping_cost", 0)
|
404 |
+
price_per_serving = 1500 # Fixed price per serving
|
405 |
+
total_price = (quantity * price_per_serving) + shipping_cost
|
406 |
summary = (f"Order Summary:\nDish: {dish}\nQuantity: {quantity}\n"
|
407 |
+
f"Phone: {phone}\nAddress: {address}\n"
|
408 |
+
f"Shipping Cost: N{shipping_cost}\n"
|
409 |
+
f"Total Price: N{total_price}\n"
|
410 |
+
f"Extras: None\nConfirm order? (yes/no)")
|
411 |
return summary
|
412 |
else:
|
413 |
return "Please respond with 'yes' or 'no'. Would you like to add any extras to your order? (yes/no)"
|
|
|
420 |
quantity = state.data.get("quantity", 1)
|
421 |
phone = state.data.get("phone_number", "")
|
422 |
address = state.data.get("address", "")
|
423 |
+
shipping_cost = state.data.get("shipping_cost", 0)
|
424 |
extras = state.data.get("extras", "")
|
425 |
+
price_per_serving = 1500 # Fixed price per serving
|
426 |
+
total_price = (quantity * price_per_serving) + shipping_cost
|
427 |
summary = (f"Order Summary:\nDish: {dish}\nQuantity: {quantity}\n"
|
428 |
+
f"Phone: {phone}\nAddress: {address}\n"
|
429 |
+
f"Shipping Cost: N{shipping_cost}\n"
|
430 |
+
f"Total Price: N{total_price}\n"
|
431 |
+
f"Extras: {extras}\nConfirm order? (yes/no)")
|
432 |
return summary
|
433 |
|
434 |
# --- Step 7: Order Confirmation & Finalization ---
|
|
|
436 |
if message.lower() in ["yes", "y"]:
|
437 |
order_id = f"ORD-{int(time.time())}"
|
438 |
state.data["order_id"] = order_id
|
439 |
+
price_per_serving = 1500 # Fixed price per serving
|
440 |
quantity = state.data.get("quantity", 1)
|
441 |
+
shipping_cost = state.data.get("shipping_cost", 0)
|
442 |
+
total_price = (quantity * price_per_serving) + shipping_cost
|
443 |
state.data["price"] = str(total_price)
|
444 |
|
445 |
+
# Save the order asynchronously (including delivery_address, phone, extras, shipping_cost)
|
446 |
async def save_order():
|
447 |
async with async_session() as session:
|
448 |
order = Order(
|
|
|
452 |
quantity=str(quantity),
|
453 |
price=str(total_price),
|
454 |
status="Pending Payment",
|
455 |
+
delivery_address=state.data.get("address", ""),
|
456 |
+
shipping_cost=str(shipping_cost) # Save shipping cost
|
457 |
)
|
458 |
session.add(order)
|
459 |
await session.commit()
|