feat(api): add endpoint to get telegram backup interval

This commit is contained in:
ReturnFI
2025-09-18 20:39:12 +00:00
parent c6b02dfbfa
commit 88659a4e7d
3 changed files with 42 additions and 2 deletions

View File

@ -14,6 +14,7 @@ CONFIG_FILE = '/etc/hysteria/config.json'
CONFIG_ENV_FILE = '/etc/hysteria/.configs.env' CONFIG_ENV_FILE = '/etc/hysteria/.configs.env'
WEBPANEL_ENV_FILE = '/etc/hysteria/core/scripts/webpanel/.env' WEBPANEL_ENV_FILE = '/etc/hysteria/core/scripts/webpanel/.env'
NORMALSUB_ENV_FILE = '/etc/hysteria/core/scripts/normalsub/.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" NODES_JSON_PATH = "/etc/hysteria/nodes.json"
@ -581,6 +582,26 @@ def stop_telegram_bot():
'''Stops the Telegram bot.''' '''Stops the Telegram bot.'''
run_cmd(['python3', Command.INSTALL_TELEGRAMBOT.value, 'stop']) 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): def set_telegram_bot_backup_interval(backup_interval: int):
'''Sets the backup interval for the Telegram bot.''' '''Sets the backup interval for the Telegram bot.'''
if backup_interval is None: if backup_interval is None:

View File

@ -1,6 +1,6 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from ..schema.response import DetailResponse from ..schema.response import DetailResponse
from ..schema.config.telegram import StartInputBody, SetIntervalInputBody from ..schema.config.telegram import StartInputBody, SetIntervalInputBody, BackupIntervalResponse
import cli_api import cli_api
router = APIRouter() router = APIRouter()
@ -40,6 +40,21 @@ async def telegram_stop_api():
raise HTTPException(status_code=400, detail=f'Error: {str(e)}') 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') @router.post('/backup-interval', response_model=DetailResponse, summary='Set Telegram Bot Backup Interval')
async def telegram_set_interval_api(body: SetIntervalInputBody): async def telegram_set_interval_api(body: SetIntervalInputBody):
""" """

View File

@ -10,3 +10,7 @@ class StartInputBody(BaseModel):
class SetIntervalInputBody(BaseModel): class SetIntervalInputBody(BaseModel):
backup_interval: int backup_interval: int
class BackupIntervalResponse(BaseModel):
backup_interval: Optional[int] = None