.
This commit is contained in:
11
src/capa_de_integracion/routers/__init__.py
Normal file
11
src/capa_de_integracion/routers/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""Routers module."""
|
||||
|
||||
from .conversation import router as conversation_router
|
||||
from .notification import router as notification_router
|
||||
from .quick_replies import router as quick_replies_router
|
||||
|
||||
__all__ = [
|
||||
"conversation_router",
|
||||
"notification_router",
|
||||
"quick_replies_router",
|
||||
]
|
||||
42
src/capa_de_integracion/routers/conversation.py
Normal file
42
src/capa_de_integracion/routers/conversation.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from ..models import ExternalConvRequestDTO, DetectIntentResponseDTO
|
||||
from ..services import ConversationManagerService
|
||||
from ..dependencies import get_conversation_manager
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/v1/dialogflow", tags=["conversation"])
|
||||
|
||||
|
||||
@router.post("/detect-intent", response_model=DetectIntentResponseDTO)
|
||||
async def detect_intent(
|
||||
request: ExternalConvRequestDTO,
|
||||
conversation_manager: ConversationManagerService = Depends(
|
||||
get_conversation_manager
|
||||
),
|
||||
) -> DetectIntentResponseDTO:
|
||||
"""
|
||||
Detect user intent and manage conversation.
|
||||
|
||||
Args:
|
||||
request: External conversation request from client
|
||||
|
||||
Returns:
|
||||
Dialogflow detect intent response
|
||||
"""
|
||||
try:
|
||||
logger.info("Received detect-intent request")
|
||||
response = await conversation_manager.manage_conversation(request)
|
||||
logger.info("Successfully processed detect-intent request")
|
||||
return response
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"Validation error: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing detect-intent: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
53
src/capa_de_integracion/routers/notification.py
Normal file
53
src/capa_de_integracion/routers/notification.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from ..models.notification import ExternalNotificationRequest
|
||||
from ..services.notification_manager import NotificationManagerService
|
||||
from ..dependencies import get_notification_manager
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/v1/dialogflow", tags=["notifications"])
|
||||
|
||||
|
||||
@router.post("/notification", status_code=200)
|
||||
async def process_notification(
|
||||
request: ExternalNotificationRequest,
|
||||
notification_manager: NotificationManagerService = Depends(
|
||||
get_notification_manager
|
||||
),
|
||||
) -> None:
|
||||
"""
|
||||
Process push notification from external system.
|
||||
|
||||
This endpoint receives notifications (e.g., "Your card was blocked") and:
|
||||
1. Stores them in Redis/Firestore
|
||||
2. Associates them with the user's conversation session
|
||||
3. Triggers a Dialogflow event
|
||||
|
||||
When the user later sends a message asking about the notification
|
||||
("Why was it blocked?"), the message filter will classify it as
|
||||
NOTIFICATION and route to the appropriate handler.
|
||||
|
||||
Args:
|
||||
request: External notification request with text, phone, and parameters
|
||||
|
||||
Returns:
|
||||
None (204 No Content)
|
||||
|
||||
Raises:
|
||||
HTTPException: 400 if validation fails, 500 for internal errors
|
||||
"""
|
||||
try:
|
||||
logger.info("Received notification request")
|
||||
await notification_manager.process_notification(request)
|
||||
logger.info("Successfully processed notification request")
|
||||
# Match Java behavior: process but don't return response body
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"Validation error: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing notification: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
81
src/capa_de_integracion/routers/quick_replies.py
Normal file
81
src/capa_de_integracion/routers/quick_replies.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from uuid import uuid4
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..models.quick_replies import QuickReplyScreen
|
||||
from ..services.quick_reply_content import QuickReplyContentService
|
||||
from ..services.redis_service import RedisService
|
||||
from ..services.firestore_service import FirestoreService
|
||||
from ..dependencies import get_redis_service, get_firestore_service, get_quick_reply_content_service
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/v1/quick-replies", tags=["quick-replies"])
|
||||
|
||||
class QuickReplyUser(BaseModel):
|
||||
telefono: str
|
||||
nombre: str
|
||||
|
||||
class QuickReplyScreenRequest(BaseModel):
|
||||
usuario: QuickReplyUser
|
||||
pantallaContexto: str
|
||||
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
class QuickReplyScreenResponse(BaseModel):
|
||||
responseId: str
|
||||
quick_replies: QuickReplyScreen
|
||||
|
||||
|
||||
@router.post("/screen")
|
||||
async def start_quick_reply_session(
|
||||
request: QuickReplyScreenRequest,
|
||||
redis_service: RedisService = Depends(get_redis_service),
|
||||
firestore_service: FirestoreService = Depends(get_firestore_service),
|
||||
quick_reply_content_service: QuickReplyContentService = Depends(get_quick_reply_content_service)
|
||||
) -> QuickReplyScreenResponse:
|
||||
"""
|
||||
Start a quick reply FAQ session for a specific screen.
|
||||
|
||||
Creates a conversation session with pantalla_contexto set,
|
||||
loads the quick reply questions for the screen, and returns them.
|
||||
|
||||
Args:
|
||||
request: Quick reply screen request
|
||||
|
||||
Returns:
|
||||
Detect intent response with quick reply questions
|
||||
"""
|
||||
try:
|
||||
telefono = request.usuario.telefono
|
||||
pantalla_contexto = request.pantallaContexto
|
||||
if not telefono or not telefono.strip():
|
||||
raise ValueError("Phone number is required")
|
||||
|
||||
session = await firestore_service.get_session_by_phone(telefono)
|
||||
if session:
|
||||
session_id = session.sessionId
|
||||
await firestore_service.update_pantalla_contexto(session_id, pantalla_contexto)
|
||||
session.pantallaContexto = pantalla_contexto
|
||||
else:
|
||||
session_id = str(uuid4())
|
||||
user_id = f"user_by_phone_{telefono.replace(' ', '').replace('-', '')}"
|
||||
session = await firestore_service.create_session(session_id, user_id, telefono, pantalla_contexto)
|
||||
|
||||
|
||||
# Cache session
|
||||
await redis_service.save_session(session)
|
||||
logger.info(f"Created quick reply session {session_id} for screen: {pantalla_contexto}")
|
||||
|
||||
# Load quick replies
|
||||
quick_replies = await quick_reply_content_service.get_quick_replies(pantalla_contexto)
|
||||
return QuickReplyScreenResponse(responseId=session_id, quick_replies=quick_replies)
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"Validation error: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error starting quick reply session: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
Reference in New Issue
Block a user