Update app.py
Browse files
app.py
CHANGED
|
@@ -365,40 +365,77 @@ async def chatbot_response(request: Request, background_tasks: BackgroundTasks):
|
|
| 365 |
sentiment_modifier = "Great to hear from you! "
|
| 366 |
|
| 367 |
# Check for specialized commands:
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
for item in menu_items:
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
"response": sentiment_modifier + "Here’s our delicious menu:",
|
| 381 |
-
"menu": menu_with_images,
|
| 382 |
-
"follow_up": "Would you like nutritional facts for any dish? Just type, for example, 'Nutritional facts for Jollof Rice'."
|
| 383 |
-
}
|
| 384 |
-
background_tasks.add_task(log_chat_to_db, user_id, "outbound", str(response_payload))
|
| 385 |
-
return JSONResponse(content=response_payload)
|
| 386 |
-
|
| 387 |
-
if "nutritional facts for" in user_message.lower():
|
| 388 |
-
dish_name = user_message.lower().replace("nutritional facts for", "").strip().title()
|
| 389 |
-
dish = next((item for item in menu_items if item["name"].lower() == dish_name.lower()), None)
|
| 390 |
-
if dish:
|
| 391 |
-
response_text = f"Nutritional facts for {dish['name']}:\n{dish['nutrition']}"
|
| 392 |
-
else:
|
| 393 |
-
response_text = f"Sorry, I couldn't find nutritional facts for {dish_name}."
|
| 394 |
background_tasks.add_task(log_chat_to_db, user_id, "outbound", response_text)
|
| 395 |
return JSONResponse(content={"response": sentiment_modifier + response_text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
-
|
| 398 |
-
order_response = process_order_flow(user_id, user_message)
|
| 399 |
-
if order_response:
|
| 400 |
-
background_tasks.add_task(log_chat_to_db, user_id, "outbound", order_response)
|
| 401 |
-
return JSONResponse(content={"response": sentiment_modifier + order_response})
|
| 402 |
|
| 403 |
# For context-aware conversation: store conversation context
|
| 404 |
conversation_context.setdefault(user_id, []).append({"timestamp": datetime.utcnow().isoformat(), "message": user_message})
|
|
|
|
| 365 |
sentiment_modifier = "Great to hear from you! "
|
| 366 |
|
| 367 |
# Check for specialized commands:
|
| 368 |
+
# Check for specialized commands:
|
| 369 |
+
if "menu" in user_message.lower():
|
| 370 |
+
# Return menu with images and options for selection
|
| 371 |
+
menu_with_images = []
|
| 372 |
+
for index, item in enumerate(menu_items, start=1):
|
| 373 |
+
image_url = google_image_scrape(item["name"])
|
| 374 |
+
menu_with_images.append({
|
| 375 |
+
"number": index, # Add a number for each dish
|
| 376 |
+
"name": item["name"],
|
| 377 |
+
"description": item["description"],
|
| 378 |
+
"price": item["price"],
|
| 379 |
+
"image_url": image_url
|
| 380 |
+
})
|
| 381 |
+
response_payload = {
|
| 382 |
+
"response": sentiment_modifier + "Here’s our delicious menu:",
|
| 383 |
+
"menu": menu_with_images,
|
| 384 |
+
"follow_up": (
|
| 385 |
+
"To order, type the *number* or *name* of the dish you'd like. "
|
| 386 |
+
"For example, type '1' or 'Jollof Rice' to order Jollof Rice.\n\n"
|
| 387 |
+
"You can also ask for nutritional facts by typing, for example, 'Nutritional facts for Jollof Rice'."
|
| 388 |
+
)
|
| 389 |
+
}
|
| 390 |
+
background_tasks.add_task(log_chat_to_db, user_id, "outbound", str(response_payload))
|
| 391 |
+
return JSONResponse(content=response_payload)
|
| 392 |
+
|
| 393 |
+
# Handle dish selection for ordering
|
| 394 |
+
if any(item["name"].lower() in user_message.lower() for item in menu_items) or \
|
| 395 |
+
any(str(index) == user_message.strip() for index, item in enumerate(menu_items, start=1)):
|
| 396 |
+
# Extract the selected dish
|
| 397 |
+
selected_dish = None
|
| 398 |
+
if user_message.strip().isdigit():
|
| 399 |
+
# User selected by number
|
| 400 |
+
dish_number = int(user_message.strip())
|
| 401 |
+
if 1 <= dish_number <= len(menu_items):
|
| 402 |
+
selected_dish = menu_items[dish_number - 1]["name"]
|
| 403 |
+
else:
|
| 404 |
+
# User selected by name
|
| 405 |
for item in menu_items:
|
| 406 |
+
if item["name"].lower() in user_message.lower():
|
| 407 |
+
selected_dish = item["name"]
|
| 408 |
+
break
|
| 409 |
+
|
| 410 |
+
if selected_dish:
|
| 411 |
+
# Trigger the order flow
|
| 412 |
+
user_state[user_id] = {"flow": "order", "step": 1, "data": {"dish": selected_dish}, "last_active": datetime.utcnow()}
|
| 413 |
+
response_text = f"You selected {selected_dish}. How many servings would you like?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
background_tasks.add_task(log_chat_to_db, user_id, "outbound", response_text)
|
| 415 |
return JSONResponse(content={"response": sentiment_modifier + response_text})
|
| 416 |
+
else:
|
| 417 |
+
response_text = "Sorry, I couldn't find that dish in the menu. Please try again."
|
| 418 |
+
background_tasks.add_task(log_chat_to_db, user_id, "outbound", response_text)
|
| 419 |
+
return JSONResponse(content={"response": sentiment_modifier + response_text})
|
| 420 |
+
|
| 421 |
+
# Handle nutritional facts request
|
| 422 |
+
if "nutritional facts for" in user_message.lower():
|
| 423 |
+
dish_name = user_message.lower().replace("nutritional facts for", "").strip().title()
|
| 424 |
+
dish = next((item for item in menu_items if item["name"].lower() == dish_name.lower()), None)
|
| 425 |
+
if dish:
|
| 426 |
+
response_text = f"Nutritional facts for {dish['name']}:\n{dish['nutrition']}"
|
| 427 |
+
else:
|
| 428 |
+
response_text = f"Sorry, I couldn't find nutritional facts for {dish_name}."
|
| 429 |
+
background_tasks.add_task(log_chat_to_db, user_id, "outbound", response_text)
|
| 430 |
+
return JSONResponse(content={"response": sentiment_modifier + response_text})
|
| 431 |
+
|
| 432 |
+
# Check if this is an order flow request
|
| 433 |
+
order_response = process_order_flow(user_id, user_message)
|
| 434 |
+
if order_response:
|
| 435 |
+
background_tasks.add_task(log_chat_to_db, user_id, "outbound", order_response)
|
| 436 |
+
return JSONResponse(content={"response": sentiment_modifier + order_response})
|
| 437 |
|
| 438 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
# For context-aware conversation: store conversation context
|
| 441 |
conversation_context.setdefault(user_id, []).append({"timestamp": datetime.utcnow().isoformat(), "message": user_message})
|