diff --git a/core/cli_api.py b/core/cli_api.py index ef386c7..01856f0 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -14,6 +14,7 @@ CONFIG_FILE = '/etc/hysteria/config.json' CONFIG_ENV_FILE = '/etc/hysteria/.configs.env' WEBPANEL_ENV_FILE = '/etc/hysteria/core/scripts/webpanel/.env' NORMALSUB_ENV_FILE = '/etc/hysteria/core/scripts/normalsub/.env' +TELEGRAM_ENV_FILE = '/etc/hysteria/core/scripts/telegrambot/.env' NODES_JSON_PATH = "/etc/hysteria/nodes.json" @@ -581,6 +582,26 @@ def stop_telegram_bot(): '''Stops the Telegram bot.''' run_cmd(['python3', Command.INSTALL_TELEGRAMBOT.value, 'stop']) +def get_telegram_bot_backup_interval() -> int | None: + '''Retrievels the current BACKUP_INTERVAL_HOUR for the Telegram Bot service from its .env file.''' + try: + if not os.path.exists(TELEGRAM_ENV_FILE): + return None + + env_vars = dotenv_values(TELEGRAM_ENV_FILE) + interval_str = env_vars.get('BACKUP_INTERVAL_HOUR') + + if interval_str: + try: + return int(float(interval_str)) + except (ValueError, TypeError): + return None + + return None + except Exception as e: + print(f"Error reading Telegram Bot .env file: {e}") + return None + def set_telegram_bot_backup_interval(backup_interval: int): '''Sets the backup interval for the Telegram bot.''' if backup_interval is None: diff --git a/core/scripts/webpanel/routers/api/v1/config/telegram.py b/core/scripts/webpanel/routers/api/v1/config/telegram.py index e688615..2e4afe2 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, SetIntervalInputBody +from ..schema.config.telegram import StartInputBody, SetIntervalInputBody, BackupIntervalResponse import cli_api router = APIRouter() @@ -40,6 +40,21 @@ async def telegram_stop_api(): raise HTTPException(status_code=400, detail=f'Error: {str(e)}') +@router.get('/backup-interval', response_model=BackupIntervalResponse, summary='Get Telegram Bot Backup Interval') +async def telegram_get_interval_api(): + """ + Gets the current automatic backup interval for the Telegram bot. + + Returns: + BackupIntervalResponse: The response containing the current interval in hours. + """ + try: + interval = cli_api.get_telegram_bot_backup_interval() + return BackupIntervalResponse(backup_interval=interval) + except Exception as e: + raise HTTPException(status_code=500, 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): """ 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 5bf1172..3dcdd93 100644 --- a/core/scripts/webpanel/routers/api/v1/schema/config/telegram.py +++ b/core/scripts/webpanel/routers/api/v1/schema/config/telegram.py @@ -9,4 +9,8 @@ class StartInputBody(BaseModel): class SetIntervalInputBody(BaseModel): - backup_interval: int \ No newline at end of file + backup_interval: int + + +class BackupIntervalResponse(BaseModel): + backup_interval: Optional[int] = None \ No newline at end of file