From 7c19393cd59ee0c4f7c75dbe2e13bff433186aa6 Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Wed, 7 Aug 2024 13:14:06 +0330 Subject: [PATCH] Create runbot.sh --- core/scripts/telegrambot/runbot.sh | 83 ++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 core/scripts/telegrambot/runbot.sh diff --git a/core/scripts/telegrambot/runbot.sh b/core/scripts/telegrambot/runbot.sh new file mode 100644 index 0000000..6c96243 --- /dev/null +++ b/core/scripts/telegrambot/runbot.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +install_dependencies() { + echo "Installing dependencies from /etc/hysteria/requirements.txt..." + if ! pip3 install -r /etc/hysteria/requirements.txt; then + echo "Error: Failed to install dependencies. Please check the requirements file and try again." + exit 1 + fi + echo "Dependencies installed successfully." +} + +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-bot.service +[Unit] +Description=Hysteria Telegram Bot +After=network.target + +[Service] +ExecStart=/usr/bin/python3 /etc/hysteria/core/scripts/telegrambot/tbot.py +WorkingDirectory=/etc/hysteria/core/scripts/telegrambot +Restart=always +User=root +Group=root + +[Install] +WantedBy=multi-user.target +EOL +} + +start_service() { + local api_token=$1 + local admin_user_ids=$2 + + if systemctl is-active --quiet hysteria-bot.service; then + echo "The hysteria-bot.service is already running." + return + fi + + install_dependencies + update_env_file "$api_token" "$admin_user_ids" + create_service_file + + systemctl daemon-reload + systemctl enable hysteria-bot.service + systemctl start hysteria-bot.service + + if systemctl is-active --quiet hysteria-bot.service; then + echo "Hysteria bot setup completed. The service is now running." + else + echo "Hysteria bot setup completed. The service failed to start." + fi +} + +stop_service() { + systemctl stop hysteria-bot.service + systemctl disable hysteria-bot.service + + rm -f /etc/hysteria/core/scripts/telegrambot/.env + 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