Update app.py
Browse files
app.py
CHANGED
|
@@ -275,6 +275,34 @@ def calculate_shipping_cost(address: str) -> int:
|
|
| 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.
|
|
@@ -369,39 +397,47 @@ def process_order_flow(user_id: str, message: str) -> str:
|
|
| 369 |
state.step = 3
|
| 370 |
return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number and delivery address."
|
| 371 |
|
| 372 |
-
# --- Step 3:
|
| 373 |
if state.step == 3:
|
| 374 |
-
#
|
| 375 |
phone_pattern = r'(\+?\d{10,15})'
|
| 376 |
-
address_pattern = r'address:\s*(.+)'
|
| 377 |
-
|
| 378 |
phone_match = re.search(phone_pattern, message)
|
| 379 |
-
address_match = re.search(address_pattern, message, re.IGNORECASE)
|
| 380 |
|
| 381 |
-
|
|
|
|
|
|
|
| 382 |
phone_number = phone_match.group(1)
|
| 383 |
-
address
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
state.data["phone_number"] = phone_number
|
| 385 |
state.data["address"] = address
|
|
|
|
|
|
|
| 386 |
# Calculate shipping cost based on the address
|
| 387 |
shipping_cost = calculate_shipping_cost(address)
|
| 388 |
state.data["shipping_cost"] = shipping_cost
|
| 389 |
state.step = 5
|
| 390 |
-
return (f"Thanks
|
| 391 |
f"Your delivery address is: {address}.\n"
|
| 392 |
f"Your delivery cost is N{shipping_cost}. Would you like to add any extras such as sides or drinks? (yes/no)")
|
| 393 |
elif phone_match:
|
| 394 |
state.data["phone_number"] = phone_match.group(1)
|
|
|
|
|
|
|
| 395 |
return "Thank you. Please provide your delivery address."
|
| 396 |
-
elif address_match:
|
| 397 |
-
state.data["address"] = address_match.group(1)
|
| 398 |
-
return "Thank you. Please provide your phone number."
|
| 399 |
else:
|
| 400 |
-
return "Please provide both your phone number and delivery address. For example: '
|
| 401 |
|
| 402 |
# --- Step 4: Requesting Delivery Address ---
|
| 403 |
if state.step == 4:
|
| 404 |
state.data["address"] = message
|
|
|
|
|
|
|
| 405 |
# Calculate shipping cost based on the address
|
| 406 |
shipping_cost = calculate_shipping_cost(message)
|
| 407 |
state.data["shipping_cost"] = shipping_cost
|
|
@@ -527,7 +563,6 @@ def process_order_flow(user_id: str, message: str) -> str:
|
|
| 527 |
return "Order canceled. Let me know if you'd like to try again."
|
| 528 |
|
| 529 |
return ""
|
| 530 |
-
|
| 531 |
# --- User Profile Functions ---
|
| 532 |
async def get_or_create_user_profile(user_id: str, phone_number: str = None) -> UserProfile:
|
| 533 |
async with async_session() as session:
|
|
|
|
| 275 |
return cost
|
| 276 |
return TOWN_SHIPPING_COSTS["default"] # Default shipping cost for unknown areas
|
| 277 |
|
| 278 |
+
# ... (rest of your imports and setup)
|
| 279 |
+
|
| 280 |
+
async def update_user_profile(user_id: str, phone_number: str = None, address: str = None) -> UserProfile:
|
| 281 |
+
"""
|
| 282 |
+
Update or create a user profile with the provided phone number and address.
|
| 283 |
+
"""
|
| 284 |
+
async with async_session() as session:
|
| 285 |
+
result = await session.execute(
|
| 286 |
+
select(UserProfile).where(UserProfile.user_id == user_id)
|
| 287 |
+
)
|
| 288 |
+
profile = result.scalars().first()
|
| 289 |
+
if profile is None:
|
| 290 |
+
profile = UserProfile(
|
| 291 |
+
user_id=user_id,
|
| 292 |
+
phone_number=phone_number,
|
| 293 |
+
address=address,
|
| 294 |
+
last_interaction=datetime.utcnow()
|
| 295 |
+
)
|
| 296 |
+
else:
|
| 297 |
+
if phone_number:
|
| 298 |
+
profile.phone_number = phone_number
|
| 299 |
+
if address:
|
| 300 |
+
profile.address = address
|
| 301 |
+
profile.last_interaction = datetime.utcnow()
|
| 302 |
+
session.add(profile)
|
| 303 |
+
await session.commit()
|
| 304 |
+
return profile
|
| 305 |
+
|
| 306 |
def process_order_flow(user_id: str, message: str) -> str:
|
| 307 |
"""
|
| 308 |
Implements an FSM-based order flow with shipping cost calculation.
|
|
|
|
| 397 |
state.step = 3
|
| 398 |
return f"Got it. {quantity} serving(s) of {state.data.get('dish')}. Please provide your phone number and delivery address."
|
| 399 |
|
| 400 |
+
# --- Step 3: Parse Phone Number and Address from Message ---
|
| 401 |
if state.step == 3:
|
| 402 |
+
# Extract phone number using regex
|
| 403 |
phone_pattern = r'(\+?\d{10,15})'
|
|
|
|
|
|
|
| 404 |
phone_match = re.search(phone_pattern, message)
|
|
|
|
| 405 |
|
| 406 |
+
# Extract address (assume it follows the phone number, separated by a comma)
|
| 407 |
+
address = None
|
| 408 |
+
if phone_match:
|
| 409 |
phone_number = phone_match.group(1)
|
| 410 |
+
# Assume the address starts after the phone number
|
| 411 |
+
address_start = phone_match.end()
|
| 412 |
+
address = message[address_start:].strip()
|
| 413 |
+
# Remove any leading/trailing commas or spaces
|
| 414 |
+
address = re.sub(r'^[,\s]+|[,\s]+$', '', address)
|
| 415 |
+
|
| 416 |
+
if phone_match and address:
|
| 417 |
state.data["phone_number"] = phone_number
|
| 418 |
state.data["address"] = address
|
| 419 |
+
# Save phone number and address to the user's profile
|
| 420 |
+
asyncio.create_task(update_user_profile(user_id, phone_number, address))
|
| 421 |
# Calculate shipping cost based on the address
|
| 422 |
shipping_cost = calculate_shipping_cost(address)
|
| 423 |
state.data["shipping_cost"] = shipping_cost
|
| 424 |
state.step = 5
|
| 425 |
+
return (f"Thanks! Your phone number is recorded as: {phone_number}.\n"
|
| 426 |
f"Your delivery address is: {address}.\n"
|
| 427 |
f"Your delivery cost is N{shipping_cost}. Would you like to add any extras such as sides or drinks? (yes/no)")
|
| 428 |
elif phone_match:
|
| 429 |
state.data["phone_number"] = phone_match.group(1)
|
| 430 |
+
# Save phone number to the user's profile
|
| 431 |
+
asyncio.create_task(update_user_profile(user_id, phone_number))
|
| 432 |
return "Thank you. Please provide your delivery address."
|
|
|
|
|
|
|
|
|
|
| 433 |
else:
|
| 434 |
+
return "Please provide both your phone number and delivery address. For example: '09162409591, 1, Iyana Isashi, Isashi, Ojo, Lagos'."
|
| 435 |
|
| 436 |
# --- Step 4: Requesting Delivery Address ---
|
| 437 |
if state.step == 4:
|
| 438 |
state.data["address"] = message
|
| 439 |
+
# Save address to the user's profile
|
| 440 |
+
asyncio.create_task(update_user_profile(user_id, address=message))
|
| 441 |
# Calculate shipping cost based on the address
|
| 442 |
shipping_cost = calculate_shipping_cost(message)
|
| 443 |
state.data["shipping_cost"] = shipping_cost
|
|
|
|
| 563 |
return "Order canceled. Let me know if you'd like to try again."
|
| 564 |
|
| 565 |
return ""
|
|
|
|
| 566 |
# --- User Profile Functions ---
|
| 567 |
async def get_or_create_user_profile(user_id: str, phone_number: str = None) -> UserProfile:
|
| 568 |
async with async_session() as session:
|