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

@ -514,8 +514,8 @@ def warp_status():
@click.option('--action', '-a', required=True, help='Action to perform: start, stop, or set_backup_interval', type=click.Choice(['start', 'stop', 'set_backup_interval'], case_sensitive=False))
@click.option('--token', '-t', required=False, help='Token for running the telegram bot (for start)', type=str)
@click.option('--adminid', '-aid', required=False, help='Telegram admins ID for running the telegram bot (for start)', type=str)
@click.option('--interval', '-i', required=False, help='Automatic backup interval in hours (for start and set_backup_interval)', type=str)
def telegram(action: str, token: str, adminid: str, interval: str):
@click.option('--interval', '-i', required=False, help='Automatic backup interval in hours (for start and set_backup_interval)', type=int)
def telegram(action: str, token: str, adminid: str, interval: int):
"""Manage the Telegram bot service."""
try:
if action == 'start':
@ -527,7 +527,7 @@ def telegram(action: str, token: str, adminid: str, interval: str):
cli_api.stop_telegram_bot()
click.echo(f'Telegram bot stopped successfully.')
elif action == 'set_backup_interval':
if not interval:
if interval is None:
raise click.UsageError('Error: --interval is required for the set_backup_interval action.')
cli_api.set_telegram_bot_backup_interval(interval)
click.echo(f'Telegram bot backup interval set to {interval} hours.')

View File

@ -566,14 +566,14 @@ def warp_status() -> str | None:
return run_cmd(['python3', Command.STATUS_WARP.value])
def start_telegram_bot(token: str, adminid: str, backup_interval: str = None):
def start_telegram_bot(token: str, adminid: str, backup_interval: Optional[int] = None):
'''Starts the Telegram bot.'''
if not token or not adminid:
raise InvalidInputError('Error: Both --token and --adminid are required for the start action.')
command = ['python3', Command.INSTALL_TELEGRAMBOT.value, 'start', token, adminid]
if backup_interval:
command.append(backup_interval)
if backup_interval is not None:
command.append(str(backup_interval))
run_cmd(command)
@ -581,11 +581,11 @@ def stop_telegram_bot():
'''Stops the Telegram bot.'''
run_cmd(['python3', Command.INSTALL_TELEGRAMBOT.value, 'stop'])
def set_telegram_bot_backup_interval(backup_interval: str):
def set_telegram_bot_backup_interval(backup_interval: int):
'''Sets the backup interval for the Telegram bot.'''
if not backup_interval:
if backup_interval is None:
raise InvalidInputError('Error: Backup interval is required.')
run_cmd(['python3', Command.INSTALL_TELEGRAMBOT.value, 'set_backup_interval', backup_interval])
run_cmd(['python3', Command.INSTALL_TELEGRAMBOT.value, 'set_backup_interval', str(backup_interval)])
def start_singbox(domain: str, port: int):

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