-
Notifications
You must be signed in to change notification settings - Fork 33
/
install.sh
176 lines (138 loc) · 5.5 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# Define text colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
RESET='\033[0m' # Reset text color
HIDY_BOT_ID="@HidyBotGroup"
# Function to display error messages and exit
function display_error_and_exit() {
echo -e "${RED}Error: $1${RESET}"
echo -e "${YELLOW}${HIDY_BOT_ID}${RESET}"
exit 1
}
install_git_if_needed() {
if ! command -v git &>/dev/null; then
echo "Git is not installed. Installing Git..."
# Install Git based on the operating system (Linux)
if [ -f /etc/os-release ]; then
source /etc/os-release
if [ "$ID" == "ubuntu" ] || [ "$ID" == "debian" ]; then
sudo apt update
sudo apt install -y git
elif [ "$ID" == "centos" ] || [ "$ID" == "rhel" ]; then
sudo yum install -y git
fi
elif [ "$(uname -s)" == "Darwin" ]; then # macOS
brew install git
else
echo "Unsupported operating system. Please install Git manually and try again."
exit 1
fi
if ! command -v git &>/dev/null; then
echo "Failed to install Git. Please install Git manually and try again."
exit 1
fi
echo "Git has been installed successfully."
fi
}
# Function to install Python 3 and pip if they are not already installed
install_python3_and_pip_if_needed() {
if ! command -v python3 &>/dev/null || ! command -v pip &>/dev/null; then
echo "Python 3 and pip are required. Installing Python 3 and pip..."
# Install Python 3 and pip based on the operating system (Linux)
if [ -f /etc/os-release ]; then
source /etc/os-release
if [ "$ID" == "ubuntu" ] || [ "$ID" == "debian" ]; then
sudo apt update
sudo apt install -y python3 python3-pip
elif [ "$ID" == "centos" ] || [ "$ID" == "rhel" ]; then
sudo yum install -y python3 python3-pip
fi
elif [ "$(uname -s)" == "Darwin" ]; then # macOS
brew install python@3
else
echo "Unsupported operating system. Please install Python 3 and pip manually and try again."
exit 1
fi
if ! command -v python3 &>/dev/null || ! command -v pip &>/dev/null; then
echo "Failed to install Python 3 and pip. Please install Python 3 and pip manually and try again."
exit 1
fi
echo "Python 3 and pip have been installed successfully."
fi
}
echo -e "${GREEN}Step 0: Checking requirements...${RESET}"
install_git_if_needed
install_python3_and_pip_if_needed
# Check if Git is installed
if ! command -v git &>/dev/null; then
display_error_and_exit "Git is not installed. Please install Git and try again."
fi
# Check if Python 3 and pip are installed
if ! command -v python3 &>/dev/null || ! command -v pip &>/dev/null; then
display_error_and_exit "Python 3 and pip are required. Please install them and try again."
fi
echo -e "${GREEN}Step 1: Cloning the repository and changing directory...${RESET}"
repository_url="https://github.com/B3H1Z/Hiddify-Telegram-Bot.git"
install_dir="/opt/Hiddify-Telegram-Bot"
branch="main"
if [ "$0" == "--pre-release" ]; then
branch="pre-release"
fi
echo "Selected branch: $branch"
if [ -d "$install_dir" ]; then
echo "Directory $install_dir exists."
else
git clone -b "$branch" "$repository_url" "$install_dir" || display_error_and_exit "Failed to clone the repository."
fi
cd "$install_dir" || display_error_and_exit "Failed to change directory."
echo -e "${GREEN}Step 2: Installing requirements...${RESET}"
pip install -r requirements.txt || display_error_and_exit "Failed to install requirements."
echo -e "${GREEN}Step 3: Preparing ...${RESET}"
logs_dir="$install_dir/Logs"
receiptions_dir="$install_dir/UserBot/Receiptions"
create_directory_if_not_exists() {
if [ ! -d "$1" ]; then
echo "Creating directory $1"
mkdir -p "$1"
fi
}
create_directory_if_not_exists "$logs_dir"
create_directory_if_not_exists "$receiptions_dir"
chmod +x "$install_dir/restart.sh"
chmod +x "$install_dir/update.sh"
echo -e "${GREEN}Step 4: Running config.py to generate config.json...${RESET}"
python3 config.py || display_error_and_exit "Failed to run config.py."
echo -e "${GREEN}Step 5: Running the bot in the background...${RESET}"
nohup python3 hiddifyTelegramBot.py >>$install_dir/bot.log 2>&1 &
echo -e "${GREEN}Step 6: Adding cron jobs...${RESET}"
add_cron_job_if_not_exists() {
local cron_job="$1"
local current_crontab
# Normalize the cron job formatting (remove extra spaces)
cron_job=$(echo "$cron_job" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')
# Check if the cron job already exists in the current user's crontab
current_crontab=$(crontab -l 2>/dev/null || true)
if [[ -z "$current_crontab" ]]; then
# No existing crontab, so add the new cron job
(echo "$cron_job") | crontab -
elif ! (echo "$current_crontab" | grep -Fq "$cron_job"); then
# Cron job doesn't exist, so append it to the crontab
(echo "$current_crontab"; echo "$cron_job") | crontab -
fi
}
# Add cron job for reboot
add_cron_job_if_not_exists "@reboot cd $install_dir && ./restart.sh"
# Add cron job to run every 6 hours
add_cron_job_if_not_exists "0 */6 * * * cd $install_dir && python3 crontab.py --backup"
# Add cron job to run at 12:00 PM daily
add_cron_job_if_not_exists "0 12 * * * cd $install_dir && python3 crontab.py --reminder"
echo -e "${GREEN}Waiting for a few seconds...${RESET}"
sleep 5
if pgrep -f "python3 hiddifyTelegramBot.py" >/dev/null; then
echo -e "${GREEN}The bot has been started successfully.${RESET}"
echo -e "${GREEN}Send [/start] in Telegram bot.${RESET}"
else
display_error_and_exit "Failed to start the bot. Please check for errors and try again."
fi