feat(api/telegram): add and refactor backup interval handling

This commit is contained in:
ReturnFI
2025-09-18 12:14:51 +00:00
parent 9f82486785
commit 6bc9358453
4 changed files with 35 additions and 12 deletions

View File

@ -1,6 +1,6 @@
from fastapi import APIRouter, HTTPException
from ..schema.response import DetailResponse
from ..schema.config.telegram import StartInputBody
from ..schema.config.telegram import StartInputBody, SetIntervalInputBody
import cli_api
router = APIRouter()
@ -12,13 +12,13 @@ async def telegram_start_api(body: StartInputBody):
Starts the Telegram bot.
Args:
body (StartInputBody): The data containing the Telegram bot token and admin ID.
body (StartInputBody): The data containing the Telegram bot token, admin ID, and optional backup interval in hours.
Returns:
DetailResponse: The response containing the result of the action.
"""
try:
cli_api.start_telegram_bot(body.token, body.admin_id)
cli_api.start_telegram_bot(body.token, body.admin_id, body.backup_interval)
return DetailResponse(detail='Telegram bot started successfully.')
except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
@ -39,3 +39,20 @@ async def telegram_stop_api():
except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
@router.post('/backup-interval', response_model=DetailResponse, summary='Set Telegram Bot Backup Interval')
async def telegram_set_interval_api(body: SetIntervalInputBody):
"""
Sets the automatic backup interval for the Telegram bot.
Args:
body (SetIntervalInputBody): The data containing the backup interval in hours.
Returns:
DetailResponse: The response containing the result of the action.
"""
try:
cli_api.set_telegram_bot_backup_interval(body.backup_interval)
return DetailResponse(detail=f'Telegram bot backup interval set to {body.backup_interval} hours successfully.')
except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')

View File

@ -1,6 +1,12 @@
from pydantic import BaseModel
from typing import Optional
class StartInputBody(BaseModel):
token: str
admin_id: str
backup_interval: Optional[int] = None
class SetIntervalInputBody(BaseModel):
backup_interval: int