From 6bc935845339e1349516daea054d7b0789d74110 Mon Sep 17 00:00:00 2001 From: ReturnFI <151555003+ReturnFI@users.noreply.github.com> Date: Thu, 18 Sep 2025 12:14:51 +0000 Subject: [PATCH] feat(api/telegram): add and refactor backup interval handling --- core/cli.py | 6 ++--- core/cli_api.py | 12 +++++----- .../routers/api/v1/config/telegram.py | 23 ++++++++++++++++--- .../routers/api/v1/schema/config/telegram.py | 6 +++++ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/core/cli.py b/core/cli.py index 38d1878..222cdd3 100644 --- a/core/cli.py +++ b/core/cli.py @@ -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.') diff --git a/core/cli_api.py b/core/cli_api.py index 509ad33..ef386c7 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -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): diff --git a/core/scripts/webpanel/routers/api/v1/config/telegram.py b/core/scripts/webpanel/routers/api/v1/config/telegram.py index b1bf70e..e688615 100644 --- a/core/scripts/webpanel/routers/api/v1/config/telegram.py +++ b/core/scripts/webpanel/routers/api/v1/config/telegram.py @@ -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)}') \ No newline at end of file diff --git a/core/scripts/webpanel/routers/api/v1/schema/config/telegram.py b/core/scripts/webpanel/routers/api/v1/schema/config/telegram.py index 77e2508..5bf1172 100644 --- a/core/scripts/webpanel/routers/api/v1/schema/config/telegram.py +++ b/core/scripts/webpanel/routers/api/v1/schema/config/telegram.py @@ -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 \ No newline at end of file