zenaight
Enhance AI chat functionality with image request handling and intent classification updates
a93b33d | from fastapi import FastAPI, Request | |
| from fastapi.responses import JSONResponse | |
| # Import our modular components | |
| from config import supabase | |
| from database import get_or_create_user, update_user_activity, get_or_create_active_session, get_user_persona, get_or_create_user_intent | |
| from whatsapp import send_whatsapp_message, send_multiple_messages | |
| from ai_chat import process_message | |
| from api_routes import router | |
| # --- FastAPI App Setup --- | |
| app = FastAPI(title="PropBot", description="WhatsApp AI Bot with Supabase integration") | |
| # Include all API routes | |
| app.include_router(router) | |
| # --- WhatsApp Webhook Handler --- | |
| async def receive_message(req: Request): | |
| """Main webhook endpoint for receiving WhatsApp messages""" | |
| body = await req.json() | |
| print("Incoming webhook:", body) | |
| try: | |
| change = body["entry"][0]["changes"][0]["value"] | |
| contacts = change.get("contacts") | |
| messages = change.get("messages") | |
| if not contacts or not messages: | |
| return JSONResponse({"status": "ignored", "reason": "no contacts/messages"}) | |
| wa_id = contacts[0]["wa_id"] | |
| contact_name = contacts[0].get("profile", {}).get("name") | |
| msg = messages[0] | |
| if msg.get("type") == "text": | |
| user_message = msg.get("text", {}).get("body", "") | |
| wamid = msg.get("id") # Meta message ID | |
| # Get or create user in database | |
| user_info = await get_or_create_user(wa_id, contact_name) | |
| # Update user activity | |
| await update_user_activity(wa_id) | |
| # Get or create active session | |
| session = await get_or_create_active_session(wa_id) | |
| # Get user persona | |
| persona = await get_user_persona(wa_id) | |
| # Get or create user intent | |
| intent = await get_or_create_user_intent(session["id"], wa_id) | |
| # Process with AI including session memory | |
| ai_result = await process_message( | |
| user_message=user_message, | |
| user_info=user_info, | |
| session_id=session["id"], | |
| wa_id=wa_id, | |
| wamid=wamid, | |
| persona=persona, | |
| intent=intent | |
| ) | |
| ai_response = ai_result["response"] | |
| properties = ai_result.get("properties", []) | |
| image_messages = ai_result.get("image_messages") | |
| # Check if this is an image request | |
| classification = ai_result.get("classification", "") | |
| print(f"DEBUG - main.py classification: '{classification}'") | |
| if image_messages: | |
| # Send images | |
| await send_multiple_messages(wa_id, image_messages) | |
| else: | |
| # Send regular text response | |
| await send_whatsapp_message(wa_id, ai_response) | |
| return JSONResponse({ | |
| "status": "replied", | |
| "user_id": wa_id, | |
| "session_id": session["id"] | |
| }) | |
| except Exception as e: | |
| print("Error processing message:", e) | |
| return JSONResponse({"status": "error", "message": str(e)}) | |
| return JSONResponse({"status": "ignored"}) | |
| # --- Application Startup Event --- | |
| async def startup_event(): | |
| """Log startup information""" | |
| print("=" * 50) | |
| print("PropBot WhatsApp AI Bot Starting...") | |
| print(f"Supabase: {'✅ Connected' if supabase else '❌ Not configured'}") | |
| print("=" * 50) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |