Files
gotelegram_pro/install.sh

125 lines
4.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Установка GoTelegram MTProxy Bot.
# Формат как kaskad: curl -sL -H "Authorization: token TOKEN" https://raw.githubusercontent.com/anten-ka/gotelegram_pro/main/install.sh -o /usr/local/bin/gotelegram && chmod +x /usr/local/bin/gotelegram && systemctl restart gotelegram-bot 2>/dev/null; GITHUB_TOKEN=TOKEN gotelegram
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Запустите с sudo.${NC}"
exit 1
fi
# Токен можно передать аргументом: gotelegram ВАШ_ТОКЕН (для приватного репо)
[ -n "$1" ] && export GITHUB_TOKEN="$1"
BOT_DIR="/opt/gotelegram-bot"
SERVICE_NAME="gotelegram-bot"
REPO_RAW="${GOTETELEGRAM_REPO_RAW:-https://raw.githubusercontent.com/anten-ka/gotelegram_pro/main}"
echo -e "${GREEN}[*] GoTelegram Bot — установка...${NC}"
# Зависимости (python3, curl, git для приватного репо)
if ! command -v python3 &>/dev/null; then
if command -v apt-get &>/dev/null; then
apt-get update && apt-get install -y python3 python3-pip python3-venv curl git
elif command -v dnf &>/dev/null; then
dnf install -y python3 python3-pip python3-virtualenv curl git 2>/dev/null || dnf install -y python3 python3-pip curl git
elif command -v yum &>/dev/null; then
yum install -y python3 python3-pip curl git
else
echo -e "${RED}Установите python3 и curl.${NC}"
exit 1
fi
fi
if [ -n "$GITHUB_TOKEN" ] && ! command -v git &>/dev/null; then
echo -e "${GREEN}[*] Установка git для клонирования приватного репо...${NC}"
if command -v apt-get &>/dev/null; then apt-get update && apt-get install -y git; fi
if command -v dnf &>/dev/null; then dnf install -y git; fi
if command -v yum &>/dev/null; then yum install -y git; fi
fi
if ! command -v docker &>/dev/null; then
echo -e "${YELLOW}[!] Docker не найден. Нужен для /install в боте.${NC}"
fi
mkdir -p "$BOT_DIR"
cd "$BOT_DIR"
# Получение файлов бота: при токене — только git clone (raw для приватного не работает)
if [ ! -f "$BOT_DIR/bot.py" ]; then
echo -e "${GREEN}[*] Загрузка файлов из репозитория...${NC}"
if [ -n "$GITHUB_TOKEN" ] && command -v git &>/dev/null; then
TMP="/tmp/gotelegram_pro_$$"
if git clone --depth 1 --branch main "https://${GITHUB_TOKEN}@github.com/anten-ka/gotelegram_pro.git" "$TMP" 2>/tmp/gotelegram_clone_err_$$; then
cp -r "$TMP/gotelegram-bot"/* "$BOT_DIR/"
rm -rf "$TMP"
else
echo -e "${RED}Ошибка клонирования:${NC}"
cat /tmp/gotelegram_clone_err_$$ 2>/dev/null
rm -rf "$TMP" /tmp/gotelegram_clone_err_$$ 2>/dev/null
exit 1
fi
rm -f /tmp/gotelegram_clone_err_$$
else
# Публичный репо — пробуем curl
for f in bot.py requirements.txt config.example.env; do
curl -sL -f "$REPO_RAW/gotelegram-bot/$f" -o "$BOT_DIR/$f" 2>/dev/null || true
done
fi
if [ ! -f "$BOT_DIR/bot.py" ]; then
echo -e "${RED}Не удалось загрузить файлы. Для приватного репо запустите с токеном:${NC}"
echo -e " ${YELLOW}gotelegram ВАШ_GITHUB_ТОКЕН${NC}"
exit 1
fi
fi
# venv
if [ ! -d "$BOT_DIR/venv" ]; then
python3 -m venv "$BOT_DIR/venv"
fi
"$BOT_DIR/venv/bin/pip" install -r "$BOT_DIR/requirements.txt" -q
# Конфиг
if [ ! -f "$BOT_DIR/.env" ]; then
echo -e "${YELLOW}Введите BOT_TOKEN от @BotFather:${NC}"
TOKEN=""
while [ -z "$TOKEN" ]; do
read -r TOKEN
TOKEN=$(echo "$TOKEN" | tr -d '[:space:]')
[ -z "$TOKEN" ] && echo -e "${RED}Токен не может быть пустым.${NC}"
done
echo "BOT_TOKEN=$TOKEN" > "$BOT_DIR/.env"
[ -f "$BOT_DIR/config.example.env" ] && grep -v "^BOT_TOKEN=" "$BOT_DIR/config.example.env" | grep -v "^#" >> "$BOT_DIR/.env"
chmod 600 "$BOT_DIR/.env"
echo -e "${GREEN}[*] .env создан.${NC}"
fi
# systemd
cat > "/etc/systemd/system/${SERVICE_NAME}.service" << EOF
[Unit]
Description=GoTelegram MTProxy Bot
After=network.target docker.service
[Service]
Type=simple
WorkingDirectory=$BOT_DIR
ExecStart=$BOT_DIR/venv/bin/python $BOT_DIR/bot.py
Restart=always
RestartSec=5
Environment=PATH=$BOT_DIR/venv/bin:/usr/bin
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME" 2>/dev/null || systemctl start "$SERVICE_NAME"
echo -e "${GREEN}[*] Сервис $SERVICE_NAME запущен.${NC}"
echo -e "Проверка: systemctl status $SERVICE_NAME"
echo -e "Логи: journalctl -u $SERVICE_NAME -f"
exit 0