from fastapi import APIRouter, Depends from sqlalchemy import select from sqlalchemy.orm import Session from .agent_message_service import SendAgentMessagePayload, get_template_by_slug, send_agent_template_message from .api_key_auth import current_api_user from .database import get_db from .models import AgentMessageTemplate, UserAccount router = APIRouter(prefix="/api/v1/agent", tags=["agent-messages"]) def _explorer_doc_for_template(template: AgentMessageTemplate) -> dict: return { "id": "send-agent-message", "method": "POST", "path": "/api/v1/agent/messages/send", "title": "Send agent WhatsApp message", "summary": ( "Send an approved agent completion template through Urmi. Urmi authenticates your API key " "and delivers the WhatsApp message from the Urmi AI business number on your behalf." ), "authentication": { "type": "api_key", "header": "Authorization", "format": "Bearer urmi_live_", "alternate_header": "X-Api-Key", }, "request_body": { "template": { "type": "string", "required": True, "description": "Subscribed agent template slug", "example": template.slug, }, "to": { "type": "string", "required": True, "description": "Recipient WhatsApp number with country code", "example": "919149154337", }, "customer_name": { "type": "string", "required": True, "description": "Recipient name shown in the template body", "example": "Suyash", }, "report_id": { "type": "string", "required": False, "description": "Report id suffix for the dynamic View Report button URL", "example": "run-abc123", }, }, "responses": { "200": { "description": "WhatsApp template accepted by Meta", "example": { "success": True, "template": template.slug, "to": "919149154337", "customer_name": "Suyash", "report_id": "run-abc123", "report_url": f"{template.button_url_base or ''}run-abc123", "wa_message_id": "wamid.HBg...", "dispatch_id": "00000000-0000-0000-0000-000000000001", }, }, "401": {"description": "Missing or invalid API key"}, "403": {"description": "Template not subscribed"}, "429": {"description": "Console test limit (console only)"}, "502": {"description": "Meta WhatsApp API error"}, }, "notes": [ "Subscribe to agent templates in Console → Agent → UrmiAI before calling this API.", "Urmi acts as the WhatsApp tech provider and sends from the Urmi AI WABA.", ], "available_templates": [ { "slug": template.slug, "name": template.name, "meta_template_name": template.meta_template_name, } ], } @router.get("/messages/docs/send") def send_agent_message_docs(db: Session = Depends(get_db)) -> dict: template = db.scalar( select(AgentMessageTemplate) .where(AgentMessageTemplate.is_active.is_(True)) .order_by(AgentMessageTemplate.created_at.asc()) ) if not template: return {"id": "send-agent-message", "method": "POST", "path": "/api/v1/agent/messages/send"} return _explorer_doc_for_template(template) @router.post("/messages/send") async def send_agent_message( payload: SendAgentMessagePayload, db: Session = Depends(get_db), user: UserAccount = Depends(current_api_user), ) -> dict: return await send_agent_template_message(db, user, payload, source="api")