File size: 3,723 Bytes
90f252d e8bd6cd 90f252d 53c8a1f a35f267 a93b33d 90f252d 851cd13 90f252d f8c1312 2d92890 90f252d 2d92890 90f252d 5e663c0 90f252d 5e663c0 1a0d052 e8bd6cd 5e663c0 fc61824 90f252d e8bd6cd 851cd13 5e663c0 fc61824 5e663c0 fdeebb1 a758b0c 851cd13 a758b0c b7a94e7 53c8a1f a758b0c a35f267 a758b0c b7a94e7 53c8a1f a758b0c 90f252d a35f267 a93b33d a35f267 b418dc7 69d3b80 a35f267 851cd13 a758b0c 6d9a771 5e663c0 fc61824 5e663c0 e8bd6cd 90f252d f8c1312 90f252d 3f945f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | 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 ---
@app.post("/webhook")
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 ---
@app.on_event("startup")
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) |