Spaces:
Sleeping
Sleeping
| from loguru import logger | |
| from fastapi import APIRouter, Depends, HTTPException, Request | |
| from dependency_injector.wiring import inject, Provide | |
| from app.dependencies.containers import Container | |
| from app.dto.request import DialogflowWebhookRequest, TimeRequest | |
| from app.dto.response import ( | |
| DialogServiceResult, | |
| DialogflowResponseBuilder, | |
| ) | |
| from app.services.time_service import TimeService | |
| router = APIRouter(prefix="/time", tags=["Time"]) | |
| async def check_time_ambiguity( | |
| request: Request, service: TimeService = Depends(Provide[Container.time_service]) | |
| ): | |
| body = await request.json() | |
| body = DialogflowWebhookRequest(**body) | |
| params = body.get_parameters() | |
| data = TimeRequest(**params) | |
| if not data.time_select: | |
| logger.info(f"[check_time_ambiguity] Received data: {data}") | |
| logger.error("Time is empty") | |
| raise HTTPException(status_code=400, detail="Time is required") | |
| is_ambiguous = service.check_time_ambiguity(time=data.time_select) | |
| return ( | |
| DialogflowResponseBuilder() | |
| .set_parameters({"is_time_ambiguous": is_ambiguous}) | |
| .build() | |
| ) | |
| async def time_ambiguity_selection( | |
| request: Request, service: TimeService = Depends(Provide[Container.time_service]) | |
| ): | |
| body = await request.json() | |
| body = DialogflowWebhookRequest(**body) | |
| params = body.get_parameters() | |
| time = params.get("time_select") | |
| if not time: | |
| logger.error("Time selection is required") | |
| raise HTTPException(status_code=400, detail="Time selection is required") | |
| rs: DialogServiceResult = service.get_time_ambiguous_selection(time) | |
| return ( | |
| DialogflowResponseBuilder() | |
| .add_text(rs.text) | |
| .add_choice_chips(options=rs.options) | |
| .set_parameters(rs.parameters) | |
| .build() | |
| ) | |