Files
tesla-bot/install.sh
2025-06-19 13:53:38 +00:00

172 lines
5.3 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
REPO_URL="https://git.bitmaster.cc/BitMaster/tesla-bot.git"
CLONE_DIR="tesla-bot"
CRON_NAME="tesla-inventory-bot"
GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
NC="\e[0m"
PIP_OK=false
PYTHON_OK=false
function banner() {
echo -e "${GREEN}"
echo "=============================="
echo "🚗 Tesla Inventory Bot Setup (No Docker)"
echo "=============================="
echo -e "${NC}"
}
function check_requirements() {
echo -e "${YELLOW}...checking system status...${NC}"
echo -n "📦 Python3: "
if command -v python3 &>/dev/null; then
echo -e "${GREEN}Installed${NC}"
PYTHON_OK=true
else
echo -e "${RED}Missing${NC}"
PYTHON_OK=false
fi
echo -n "📦 pip3: "
if command -v pip3 &>/dev/null; then
echo -e "${GREEN}Installed${NC}"
PIP_OK=true
else
echo -e "${RED}Missing${NC}"
PIP_OK=false
fi
echo -n "📁 Project folder '$CLONE_DIR': "
if [ -d "$CLONE_DIR" ]; then
echo -e "${YELLOW}Exists${NC}"
else
echo -e "${RED}Not present${NC}"
fi
if [ "$PYTHON_OK" = false ]; then
echo -e "${YELLOW}❓ Python3 is required. Do you want to install it now? [Y/n]${NC}"
read -rp "> " confirm
confirm="${confirm,,}"
if [[ "$confirm" =~ ^(y|yes| )?$ ]]; then
sudo apt update && sudo apt install -y python3
else
echo -e "${RED}⚠️ Cannot proceed without Python3. Exiting.${NC}"; exit 1
fi
fi
if [ "$PIP_OK" = false ]; then
echo -e "${YELLOW}❓ pip3 is required. Do you want to install it now? [Y/n]${NC}"
read -rp "> " confirm
confirm="${confirm,,}"
if [[ "$confirm" =~ ^(y|yes| )?$ ]]; then
sudo apt update && sudo apt install -y python3-pip
else
echo -e "${RED}⚠️ Cannot proceed without pip3. Exiting.${NC}"; exit 1
fi
fi
}
function clone_and_setup() {
echo -e "${GREEN}📥 Cloning repo...${NC}"
rm -rf "$CLONE_DIR"
git clone "$REPO_URL" "$CLONE_DIR"
cd "$CLONE_DIR" || exit 1
echo -e "${GREEN}📦 Installing Python dependencies...${NC}"
pip3 install -r requirements.txt
echo -e "${YELLOW}📨 Enter your Telegram bot token:${NC}"
read -rp "🔑 TELEGRAM_TOKEN: " token
echo -e "${YELLOW}🤖 Auto detecting the Telegram chat-ID...${NC}"
auto_chat_id=$(curl -s "https://api.telegram.org/bot$token/getUpdates" | grep -oE '"id":[0-9-]+' | head -n1 | cut -d: -f2)
if [ -n "$auto_chat_id" ]; then
echo -e "${GREEN}✅ Auto-detected chat ID: $auto_chat_id${NC}"
else
echo -e "${RED}❌ Could not auto-detect chat ID. You may enter it manually.${NC}"
fi
read -rp "🆔 TELEGRAM_CHAT_ID [default: $auto_chat_id]: " chat_id
chat_id="${chat_id:-$auto_chat_id}"
if [ -z "$chat_id" ]; then
echo -e "${RED}❌ No chat ID provided or detected. Aborting.${NC}"
exit 1
fi
sed -i "s|^TELEGRAM_TOKEN = .*|TELEGRAM_TOKEN = \"$token\"|" bot.py
sed -i "s|^CHAT_ID = .*|CHAT_ID = \"$chat_id\"|" bot.py
echo -e "${GREEN}✅ Credentials updated in bot.py${NC}"
echo -e "${YELLOW}📤 Sending test Telegram message...${NC}"
curl -s -X POST "https://api.telegram.org/bot$token/sendMessage" \
-d chat_id="$chat_id" \
-d text="✅ Tesla bot kuruldu ve Telegram bağlantısı başarılı!" \
-d parse_mode="HTML" > /dev/null
echo -e "${GREEN}✅ Test message sent. Check your Telegram!${NC}"
cd ..
read -rp "$(echo -e "${GREEN}🔁 Press Enter to return to the main menu...${NC}")"
}
function run_bot_now() {
if [ ! -f "$CLONE_DIR/bot.py" ]; then
echo -e "${RED}❌ Bot not installed. Please run option 1 first.${NC}"
else
echo -e "${YELLOW}⚙️ Running bot.py...${NC}"
python3 "$CLONE_DIR/bot.py"
fi
read -rp "$(echo -e "${GREEN}🔁 Press Enter to return to the main menu...${NC}")"
}
function setup_cronjob() {
echo -e "${YELLOW}Setting up cronjob...${NC}"
if [ ! -d "$CLONE_DIR" ]; then
echo -e "${RED}❌ Bot not installed. Run option 1 first.${NC}"
else
echo -e "${YELLOW}⏱ How often should the bot run? (in minutes, 1-60)${NC}"
read -rp "Interval: " interval
if ! [[ "$interval" =~ ^[0-9]+$ ]] || [ "$interval" -lt 1 ] || [ "$interval" -gt 60 ]; then
echo -e "${RED}❌ Invalid input. Must be between 1 and 60.${NC}"
else
(crontab -l 2>/dev/null | grep -v "$CRON_NAME"; echo "*/$interval * * * * cd $(pwd)/$CLONE_DIR && python3 bot.py >> cron.log 2>&1") | crontab -
echo -e "${GREEN}✅ Cronjob installed to run every $interval minute(s).${NC}"
fi
fi
read -rp "$(echo -e "${GREEN}🔁 Press Enter to return to the main menu...${NC}")"
}
function remove_cronjob() {
echo -e "${YELLOW}Removing bot cronjob...${NC}"
crontab -l 2>/dev/null | grep -v "$CRON_NAME" | crontab -
echo -e "${GREEN}✅ Cronjob removed.${NC}"
read -rp "$(echo -e "${GREEN}🔁 Press Enter to return to the main menu...${NC}")"
}
while true; do
clear
banner
check_requirements
echo -e "${YELLOW}1. Install bot and configure"
echo -e "2. Run bot manually now"
echo -e "3. Setup new cronjob (every X min)"
echo -e "4. Remove existing cronjob"
echo -e "5. Exit${NC}"
echo ""
read -rp "$(echo -e "${YELLOW}🔗 Choose [1-5]: ${NC}")" choice
case $choice in
1) clone_and_setup ;;
2) run_bot_now ;;
3) setup_cronjob ;;
4) remove_cronjob ;;
5) echo -e "${RED}❌ Exiting...${NC}"; exit 0 ;;
*) echo -e "${RED}❌ Invalid option.${NC}"; sleep 1 ;;
esac
done