Files
tesla-bot/install.sh
2025-06-19 14:06:55 +00:00

167 lines
5.2 KiB
Bash
Raw Permalink 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}"
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}"
fi
echo -n "📁 Project folder '$CLONE_DIR': "
if [ -d "$CLONE_DIR" ]; then
echo -e "${GREEN}Exists${NC}"
else
echo -e "${RED}Not present${NC}"
fi
if [ "$PYTHON_OK" = false ]; then
echo -e "${YELLOW}❓ Python3 is required. Install 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. Install 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" || { echo -e "${RED}❌ git clone failed${NC}"; return; }
cd "$CLONE_DIR"
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 your Telegram chat ID...${NC}"
auto_chat_id=$(python3 - <<PYCODE
import requests, json
try:
h = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/114.0.0.0 Safari/537.36"}
r = requests.get(f"https://api.telegram.org/bot{token}/getUpdates", headers=h, timeout=30)
data = r.json().get("result",[])
if data:
print(data[-1]["message"]["chat"]["id"])
except:
pass
PYCODE
)
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 will need to enter it manually.${NC}"
fi
read -rp "🆔 TELEGRAM_CHAT_ID [default: $auto_chat_id]: " chat_id
chat_id="${chat_id:-$auto_chat_id}"
[ -z "$chat_id" ] && { echo -e "${RED}❌ No chat ID provided. Aborting.${NC}"; exit 1; }
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 installed and ready!" -d parse_mode="HTML"
echo -e "${GREEN}✅ Test message sent!${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. 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...${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
read -rp "⏱ Run interval in minutes (160): " interval
if ! [[ "$interval" =~ ^[0-9]+$ ]] || [ "$interval" -lt 1 ] || [ "$interval" -gt 60 ]; then
echo -e "${RED}❌ Invalid interval.${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 set for every $interval min.${NC}"
fi
fi
read -rp "$(echo -e "${GREEN}🔁 Press Enter to return...${NC}")"
}
function remove_cronjob() {
crontab -l | grep -v "$CRON_NAME" | crontab -
echo -e "${GREEN}✅ Cronjob removed.${NC}"
read -rp "$(echo -e "${GREEN}🔁 Press Enter to return...${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"
echo -e "4. Remove existing cronjob"
echo -e "5. Exit${NC}"
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