diff --git a/core/cli_api.py b/core/cli_api.py index dbf92d4..b4dc9b1 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -38,7 +38,7 @@ class Command(Enum): SERVER_INFO = os.path.join(SCRIPT_DIR, 'hysteria2', 'server_info.sh') BACKUP_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'backup.sh') RESTORE_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'restore.sh') - INSTALL_TELEGRAMBOT = os.path.join(SCRIPT_DIR, 'telegrambot', 'runbot.sh') + INSTALL_TELEGRAMBOT = os.path.join(SCRIPT_DIR, 'telegrambot', 'runbot.py') SHELL_SINGBOX = os.path.join(SCRIPT_DIR, 'singbox', 'singbox_shell.sh') SHELL_WEBPANEL = os.path.join(SCRIPT_DIR, 'webpanel', 'webpanel_shell.sh') INSTALL_NORMALSUB = os.path.join(SCRIPT_DIR, 'normalsub', 'normalsub.sh') @@ -470,12 +470,12 @@ def start_telegram_bot(token: str, adminid: str): '''Starts the Telegram bot.''' if not token or not adminid: raise InvalidInputError('Error: Both --token and --adminid are required for the start action.') - run_cmd(['bash', Command.INSTALL_TELEGRAMBOT.value, 'start', token, adminid]) + run_cmd(['python3', Command.INSTALL_TELEGRAMBOT.value, 'start', token, adminid]) def stop_telegram_bot(): '''Stops the Telegram bot.''' - run_cmd(['bash', Command.INSTALL_TELEGRAMBOT.value, 'stop']) + run_cmd(['python3', Command.INSTALL_TELEGRAMBOT.value, 'stop']) def start_singbox(domain: str, port: int): diff --git a/core/scripts/telegrambot/runbot.py b/core/scripts/telegrambot/runbot.py new file mode 100644 index 0000000..2279018 --- /dev/null +++ b/core/scripts/telegrambot/runbot.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import sys +import subprocess +from pathlib import Path + +core_scripts_dir = Path(__file__).resolve().parents[1] +if str(core_scripts_dir) not in sys.path: + sys.path.append(str(core_scripts_dir)) + +from paths import TELEGRAM_ENV + + + + +def update_env_file(api_token, admin_user_ids): + TELEGRAM_ENV.write_text(f"""API_TOKEN={api_token} +ADMIN_USER_IDS=[{admin_user_ids}] +""") + +def create_service_file(): + Path("/etc/systemd/system/hysteria-telegram-bot.service").write_text("""[Unit] +Description=Hysteria Telegram Bot +After=network.target + +[Service] +ExecStart=/bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && /etc/hysteria/hysteria2_venv/bin/python /etc/hysteria/core/scripts/telegrambot/tbot.py' +WorkingDirectory=/etc/hysteria/core/scripts/telegrambot +Restart=always + +[Install] +WantedBy=multi-user.target +""") + +def start_service(api_token, admin_user_ids): + if subprocess.run(["systemctl", "is-active", "--quiet", "hysteria-telegram-bot.service"]).returncode == 0: + print("The hysteria-telegram-bot.service is already running.") + return + + update_env_file(api_token, admin_user_ids) + create_service_file() + + subprocess.run(["systemctl", "daemon-reload"]) + subprocess.run(["systemctl", "enable", "hysteria-telegram-bot.service"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["systemctl", "start", "hysteria-telegram-bot.service"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + if subprocess.run(["systemctl", "is-active", "--quiet", "hysteria-telegram-bot.service"]).returncode == 0: + print("Hysteria bot setup completed. The service is now running.\n") + else: + print("Hysteria bot setup completed. The service failed to start.") + +def stop_service(): + subprocess.run(["systemctl", "stop", "hysteria-telegram-bot.service"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["systemctl", "disable", "hysteria-telegram-bot.service"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + TELEGRAM_ENV.unlink(missing_ok=True) + print("\nHysteria bot service stopped and disabled. .env file removed.") + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python3 runbot.py {start|stop} ") + sys.exit(1) + + action = sys.argv[1] + + if action == "start": + if len(sys.argv) != 4: + print("Usage: python3 runbot.py start ") + sys.exit(1) + start_service(sys.argv[2], sys.argv[3]) + elif action == "stop": + stop_service() + else: + print("Usage: python3 runbot.py {start|stop} ") + sys.exit(1) diff --git a/core/scripts/telegrambot/runbot.sh b/core/scripts/telegrambot/runbot.sh deleted file mode 100644 index f68396b..0000000 --- a/core/scripts/telegrambot/runbot.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/bash -source /etc/hysteria/core/scripts/utils.sh -define_colors - -update_env_file() { - local api_token=$1 - local admin_user_ids=$2 - - cat < /etc/hysteria/core/scripts/telegrambot/.env -API_TOKEN=$api_token -ADMIN_USER_IDS=[$admin_user_ids] -EOL -} - -create_service_file() { - cat < /etc/systemd/system/hysteria-telegram-bot.service -[Unit] -Description=Hysteria Telegram Bot -After=network.target - -[Service] -ExecStart=/bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && /etc/hysteria/hysteria2_venv/bin/python /etc/hysteria/core/scripts/telegrambot/tbot.py' -WorkingDirectory=/etc/hysteria/core/scripts/telegrambot -Restart=always - -[Install] -WantedBy=multi-user.target -EOL -} - -start_service() { - local api_token=$1 - local admin_user_ids=$2 - - if systemctl is-active --quiet hysteria-telegram-bot.service; then - echo "The hysteria-telegram-bot.service is already running." - return - fi - - update_env_file "$api_token" "$admin_user_ids" - create_service_file - - systemctl daemon-reload - systemctl enable hysteria-telegram-bot.service > /dev/null 2>&1 - systemctl start hysteria-telegram-bot.service > /dev/null 2>&1 - - if systemctl is-active --quiet hysteria-telegram-bot.service; then - echo -e "${green}Hysteria bot setup completed. The service is now running. ${NC}" - echo -e "\n\n" - else - echo "Hysteria bot setup completed. The service failed to start." - fi -} - -stop_service() { - systemctl stop hysteria-telegram-bot.service > /dev/null 2>&1 - systemctl disable hysteria-telegram-bot.service > /dev/null 2>&1 - - rm -f /etc/hysteria/core/scripts/telegrambot/.env - echo -e "\n" - - echo "Hysteria bot service stopped and disabled. .env file removed." -} - -case "$1" in - start) - start_service "$2" "$3" - ;; - stop) - stop_service - ;; - *) - echo "Usage: $0 {start|stop} " - exit 1 - ;; -esac - -define_colors