Initial Python rewrite
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",
|
||||
]
|
||||
43
src/capa_de_integracion/routers/conversation.py
Normal file
43
src/capa_de_integracion/routers/conversation.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import logging
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from capa_de_integracion.dependencies import get_conversation_manager
|
||||
from capa_de_integracion.models import ConversationRequest, DetectIntentResponse
|
||||
from capa_de_integracion.services import ConversationManagerService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/v1/dialogflow", tags=["conversation"])
|
||||
|
||||
|
||||
@router.post("/detect-intent")
|
||||
async def detect_intent(
|
||||
request: ConversationRequest,
|
||||
conversation_manager: Annotated[ConversationManagerService, Depends(
|
||||
get_conversation_manager,
|
||||
)],
|
||||
) -> DetectIntentResponse:
|
||||
"""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: {e!s}", exc_info=True)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing detect-intent: {e!s}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
54
src/capa_de_integracion/routers/notification.py
Normal file
54
src/capa_de_integracion/routers/notification.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import logging
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from capa_de_integracion.dependencies import get_notification_manager
|
||||
from capa_de_integracion.models.notification import ExternalNotificationRequest
|
||||
from capa_de_integracion.services.notification_manager import NotificationManagerService
|
||||
|
||||
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: Annotated[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: {e!s}", exc_info=True)
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing notification: {e!s}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
101
src/capa_de_integracion/routers/quick_replies.py
Normal file
101
src/capa_de_integracion/routers/quick_replies.py
Normal file
@@ -0,0 +1,101 @@
|
||||
import logging
|
||||
from typing import Annotated
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from capa_de_integracion.dependencies import (
|
||||
get_firestore_service,
|
||||
get_quick_reply_content_service,
|
||||
get_redis_service,
|
||||
)
|
||||
from capa_de_integracion.models.quick_replies import QuickReplyScreen
|
||||
from capa_de_integracion.services.firestore_service import FirestoreService
|
||||
from capa_de_integracion.services.quick_reply_content import QuickReplyContentService
|
||||
from capa_de_integracion.services.redis_service import RedisService
|
||||
|
||||
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: Annotated[RedisService, Depends(get_redis_service)],
|
||||
firestore_service: Annotated[FirestoreService, Depends(get_firestore_service)],
|
||||
quick_reply_content_service: Annotated[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():
|
||||
msg = "Phone number is required"
|
||||
raise ValueError(msg)
|
||||
|
||||
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