#!/bin/bash # Telegram Bot Service Control Script for ROA2WEB Unified App # Manages the Telegram Bot on port 8002 # Script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$SCRIPT_DIR" # Source helper functions source "$SCRIPT_DIR/scripts/service-helpers.sh" # Service configuration SERVICE_NAME="Telegram Bot" PORT=8002 LOG_FILE="/tmp/telegram-bot.log" BOT_DIR="$ROOT_DIR/reports-app/telegram-bot" VENV_DIR="$BOT_DIR/venv" ENV_FILE="$BOT_DIR/.env" # Function to start bot start_bot() { print_header "Starting $SERVICE_NAME" # Check if port is already in use if ! check_port_available $PORT "$SERVICE_NAME"; then print_warning "$SERVICE_NAME may already be running" print_info "Use './bot.sh status' to check or './bot.sh stop' to stop it" return 1 fi # Check bot directory if [ ! -d "$BOT_DIR" ]; then print_error "Bot directory not found: $BOT_DIR" return 1 fi # Check virtual environment if [ ! -d "$VENV_DIR" ]; then print_error "Virtual environment not found: $VENV_DIR" print_info "Please run setup first: cd reports-app/telegram-bot && python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt" return 1 fi # Check .env file if [ ! -f "$ENV_FILE" ]; then print_warning ".env file not found: $ENV_FILE" print_info "Bot may not start without TELEGRAM_BOT_TOKEN" fi # Start bot print_info "Starting Telegram bot..." cd "$BOT_DIR" # Activate venv and start bot in background ( source venv/bin/activate nohup python -m app.main > "$LOG_FILE" 2>&1 & echo $! > /tmp/telegram-bot.pid ) local pid=$(cat /tmp/telegram-bot.pid 2>/dev/null) print_info "Started with PID: $pid" print_info "Log file: $LOG_FILE" # Wait for port to be ready if wait_for_port $PORT "$SERVICE_NAME" 5; then print_success "$SERVICE_NAME started successfully!" echo "" print_info "🤖 Bot is running and listening for commands" print_info "📄 View logs: tail -f $LOG_FILE" return 0 else print_error "Failed to start $SERVICE_NAME (timeout waiting for port $PORT)" print_info "Check logs: tail -20 $LOG_FILE" rm -f /tmp/telegram-bot.pid return 1 fi } # Function to stop bot stop_bot() { print_header "Stopping $SERVICE_NAME" kill_port $PORT "$SERVICE_NAME" # Clean up PID file if [ -f "/tmp/telegram-bot.pid" ]; then rm /tmp/telegram-bot.pid fi return 0 } # Function to show bot status status_bot() { print_header "$SERVICE_NAME Status" if check_service_status $PORT "$SERVICE_NAME"; then echo "" print_info "Service is running" # Show recent logs if [ -f "$LOG_FILE" ]; then echo "" tail_logs "$LOG_FILE" 10 fi return 0 else echo "" print_warning "Service is not running" # Show last logs if available if [ -f "$LOG_FILE" ]; then echo "" print_info "Last logs before shutdown:" tail_logs "$LOG_FILE" 10 fi return 1 fi } # Main script logic case "${1:-}" in start) start_bot ;; stop) stop_bot ;; status) status_bot ;; *) echo "Usage: $0 {start|stop|status}" echo "" echo "Commands:" echo " start - Start the Telegram bot" echo " stop - Stop the Telegram bot" echo " status - Show bot status" echo "" echo "Examples:" echo " ./bot.sh start # Start bot" echo " ./bot.sh status # Check if running" echo " ./bot.sh stop # Stop bot" exit 1 ;; esac