.
This commit is contained in:
@@ -1,31 +1,32 @@
|
||||
import logging
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from ..models import ConversationRequest, DetectIntentResponse
|
||||
from ..services import ConversationManagerService
|
||||
from ..dependencies import get_conversation_manager
|
||||
|
||||
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", response_model=DetectIntentResponse)
|
||||
@router.post("/detect-intent")
|
||||
async def detect_intent(
|
||||
request: ConversationRequest,
|
||||
conversation_manager: ConversationManagerService = Depends(
|
||||
get_conversation_manager
|
||||
),
|
||||
conversation_manager: Annotated[ConversationManagerService, Depends(
|
||||
get_conversation_manager,
|
||||
)],
|
||||
) -> DetectIntentResponse:
|
||||
"""
|
||||
Detect user intent and manage conversation.
|
||||
"""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")
|
||||
@@ -34,9 +35,9 @@ async def detect_intent(
|
||||
return response
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"Validation error: {str(e)}", exc_info=True)
|
||||
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: {str(e)}", exc_info=True)
|
||||
logger.error(f"Error processing detect-intent: {e!s}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import logging
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from ..models.notification import ExternalNotificationRequest
|
||||
from ..services.notification_manager import NotificationManagerService
|
||||
from ..dependencies import get_notification_manager
|
||||
|
||||
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"])
|
||||
@@ -13,12 +14,11 @@ 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
|
||||
),
|
||||
notification_manager: Annotated[NotificationManagerService, Depends(
|
||||
get_notification_manager,
|
||||
)],
|
||||
) -> None:
|
||||
"""
|
||||
Process push notification from external system.
|
||||
"""Process push notification from external system.
|
||||
|
||||
This endpoint receives notifications (e.g., "Your card was blocked") and:
|
||||
1. Stores them in Redis/Firestore
|
||||
@@ -37,6 +37,7 @@ async def process_notification(
|
||||
|
||||
Raises:
|
||||
HTTPException: 400 if validation fails, 500 for internal errors
|
||||
|
||||
"""
|
||||
try:
|
||||
logger.info("Received notification request")
|
||||
@@ -45,9 +46,9 @@ async def process_notification(
|
||||
# Match Java behavior: process but don't return response body
|
||||
|
||||
except ValueError as e:
|
||||
logger.error(f"Validation error: {str(e)}", exc_info=True)
|
||||
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: {str(e)}", exc_info=True)
|
||||
logger.error(f"Error processing notification: {e!s}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from typing import Annotated
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
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,
|
||||
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"])
|
||||
@@ -38,14 +39,13 @@ class QuickReplyScreenResponse(BaseModel):
|
||||
@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
|
||||
),
|
||||
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.
|
||||
"""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.
|
||||
@@ -55,39 +55,41 @@ async def start_quick_reply_session(
|
||||
|
||||
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")
|
||||
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_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
|
||||
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}"
|
||||
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
|
||||
pantalla_contexto,
|
||||
)
|
||||
return QuickReplyScreenResponse(
|
||||
responseId=session_id, quick_replies=quick_replies
|
||||
responseId=session_id, quick_replies=quick_replies,
|
||||
)
|
||||
|
||||
except ValueError as e:
|
||||
|
||||
Reference in New Issue
Block a user