Update app.py
Browse files
app.py
CHANGED
|
@@ -591,6 +591,17 @@ async def chatbot_response(request: Request, background_tasks: BackgroundTasks):
|
|
| 591 |
elif sentiment_score > 0.3:
|
| 592 |
sentiment_modifier = "Great to hear from you! "
|
| 593 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
# --- Order Flow Handling ---
|
| 595 |
order_response = process_order_flow(user_id, user_message)
|
| 596 |
if order_response:
|
|
@@ -806,19 +817,28 @@ async def payment_callback(request: Request):
|
|
| 806 |
return JSONResponse(content={"message": "Order updated successfully."})
|
| 807 |
else:
|
| 808 |
raise HTTPException(status_code=404, detail="Order not found.")
|
| 809 |
-
|
| 810 |
-
# --- New Endpoint: Order Tracking ---
|
| 811 |
@app.get("/track_order/{order_id}")
|
| 812 |
async def track_order(order_id: str):
|
|
|
|
|
|
|
|
|
|
| 813 |
async with async_session() as session:
|
| 814 |
result = await session.execute(
|
| 815 |
-
|
| 816 |
.where(OrderTracking.order_id == order_id)
|
| 817 |
.order_by(OrderTracking.timestamp)
|
| 818 |
)
|
| 819 |
-
tracking_updates = result.
|
| 820 |
if tracking_updates:
|
| 821 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
else:
|
| 823 |
raise HTTPException(status_code=404, detail="No tracking information found for this order.")
|
| 824 |
|
|
|
|
| 591 |
elif sentiment_score > 0.3:
|
| 592 |
sentiment_modifier = "Great to hear from you! "
|
| 593 |
|
| 594 |
+
# --- Order Tracking Handling ---
|
| 595 |
+
order_id_match = re.search(r"ord-\d+", user_message.lower())
|
| 596 |
+
if order_id_match:
|
| 597 |
+
order_id = order_id_match.group(0)
|
| 598 |
+
try:
|
| 599 |
+
# Call the /track_order endpoint
|
| 600 |
+
tracking_response = await track_order(order_id)
|
| 601 |
+
return JSONResponse(content={"response": tracking_response})
|
| 602 |
+
except HTTPException as e:
|
| 603 |
+
return JSONResponse(content={"response": f"⚠️ {e.detail}"})
|
| 604 |
+
|
| 605 |
# --- Order Flow Handling ---
|
| 606 |
order_response = process_order_flow(user_id, user_message)
|
| 607 |
if order_response:
|
|
|
|
| 817 |
return JSONResponse(content={"message": "Order updated successfully."})
|
| 818 |
else:
|
| 819 |
raise HTTPException(status_code=404, detail="Order not found.")
|
| 820 |
+
|
|
|
|
| 821 |
@app.get("/track_order/{order_id}")
|
| 822 |
async def track_order(order_id: str):
|
| 823 |
+
"""
|
| 824 |
+
Fetch order tracking details for a given order ID.
|
| 825 |
+
"""
|
| 826 |
async with async_session() as session:
|
| 827 |
result = await session.execute(
|
| 828 |
+
select(OrderTracking)
|
| 829 |
.where(OrderTracking.order_id == order_id)
|
| 830 |
.order_by(OrderTracking.timestamp)
|
| 831 |
)
|
| 832 |
+
tracking_updates = result.scalars().all()
|
| 833 |
if tracking_updates:
|
| 834 |
+
response = []
|
| 835 |
+
for update in tracking_updates:
|
| 836 |
+
response.append({
|
| 837 |
+
"status": update.status,
|
| 838 |
+
"message": update.message,
|
| 839 |
+
"timestamp": update.timestamp.isoformat(),
|
| 840 |
+
})
|
| 841 |
+
return JSONResponse(content=response)
|
| 842 |
else:
|
| 843 |
raise HTTPException(status_code=404, detail="No tracking information found for this order.")
|
| 844 |
|