#!/bin/bash # LittleShop TeleBot Docker Deployment Script # Usage: ./deploy-bot.sh [OPTIONS] # # This script helps deploy TeleBot instances to local or remote Docker hosts set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Default values DOCKER_IMAGE="littleshop/telebot:latest" CONTAINER_PREFIX="littleshop-bot" API_URL="${LITTLESHOP_API_URL:-http://localhost:8080}" RESTART_POLICY="unless-stopped" DOCKER_HOST="" # Function to print colored output print_message() { local color=$1 local message=$2 echo -e "${color}${message}${NC}" } # Function to show usage show_usage() { cat << EOF Usage: $0 [OPTIONS] Deploy LittleShop TeleBot to Docker hosts OPTIONS: -n, --name NAME Bot container name (required) -t, --token TOKEN Telegram bot token (required) -k, --api-key KEY Bot API key from admin panel -a, --api-url URL LittleShop API URL (default: $API_URL) -h, --host HOST Docker host (e.g., ssh://user@host or tcp://host:2376) -c, --chat-id ID Admin chat ID for notifications -e, --encryption-key KEY Database encryption key -p, --personality NAME Bot personality name -m, --mode MODE Privacy mode (strict|moderate|relaxed) -i, --image IMAGE Docker image (default: $DOCKER_IMAGE) -r, --restart POLICY Restart policy (default: $RESTART_POLICY) -d, --detach Run in detached mode (default) -l, --logs Follow logs after deployment --pull Pull latest image before deployment --rm Remove existing container before deployment --help Show this help message EXAMPLES: # Deploy to local Docker $0 -n support-bot -t "TOKEN" -k "API_KEY" # Deploy to remote host via SSH $0 -n sales-bot -t "TOKEN" -k "API_KEY" -h ssh://user@server.com # Deploy with all options $0 -n vip-bot -t "TOKEN" -k "API_KEY" -a https://api.shop.com \\ -c "123456789" -e "32_char_encryption_key_here" \\ -p "Sarah" -m strict --pull --rm # Deploy multiple bots using environment file source .env.bot1 && $0 -n bot1 -t "\$BOT_TOKEN" -k "\$API_KEY" source .env.bot2 && $0 -n bot2 -t "\$BOT_TOKEN" -k "\$API_KEY" EOF } # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -n|--name) BOT_NAME="$2" shift 2 ;; -t|--token) BOT_TOKEN="$2" shift 2 ;; -k|--api-key) API_KEY="$2" shift 2 ;; -a|--api-url) API_URL="$2" shift 2 ;; -h|--host) DOCKER_HOST="$2" shift 2 ;; -c|--chat-id) ADMIN_CHAT_ID="$2" shift 2 ;; -e|--encryption-key) ENCRYPTION_KEY="$2" shift 2 ;; -p|--personality) PERSONALITY="$2" shift 2 ;; -m|--mode) PRIVACY_MODE="$2" shift 2 ;; -i|--image) DOCKER_IMAGE="$2" shift 2 ;; -r|--restart) RESTART_POLICY="$2" shift 2 ;; -l|--logs) FOLLOW_LOGS=true shift ;; --pull) PULL_IMAGE=true shift ;; --rm) REMOVE_EXISTING=true shift ;; --help) show_usage exit 0 ;; *) print_message "$RED" "Unknown option: $1" show_usage exit 1 ;; esac done # Validate required parameters if [ -z "$BOT_NAME" ]; then print_message "$RED" "Error: Bot name is required (-n/--name)" exit 1 fi if [ -z "$BOT_TOKEN" ]; then print_message "$RED" "Error: Bot token is required (-t/--token)" exit 1 fi # Set container name CONTAINER_NAME="${CONTAINER_PREFIX}-${BOT_NAME}" # Build Docker command DOCKER_CMD="docker" if [ -n "$DOCKER_HOST" ]; then export DOCKER_HOST="$DOCKER_HOST" print_message "$YELLOW" "Deploying to remote host: $DOCKER_HOST" fi # Pull latest image if requested if [ "$PULL_IMAGE" = true ]; then print_message "$YELLOW" "Pulling latest image: $DOCKER_IMAGE" $DOCKER_CMD pull "$DOCKER_IMAGE" fi # Remove existing container if requested if [ "$REMOVE_EXISTING" = true ]; then if $DOCKER_CMD ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then print_message "$YELLOW" "Removing existing container: $CONTAINER_NAME" $DOCKER_CMD rm -f "$CONTAINER_NAME" fi fi # Build environment variables ENV_VARS=( "-e DOTNET_ENVIRONMENT=Production" "-e TZ=UTC" "-e Telegram__BotToken=$BOT_TOKEN" "-e LittleShop__ApiUrl=$API_URL" "-e Database__ConnectionString=Filename=/app/data/telebot.db;Password=;" ) # Add optional environment variables [ -n "$API_KEY" ] && ENV_VARS+=("-e BotManager__ApiKey=$API_KEY") [ -n "$ADMIN_CHAT_ID" ] && ENV_VARS+=("-e Telegram__AdminChatId=$ADMIN_CHAT_ID") [ -n "$ENCRYPTION_KEY" ] && ENV_VARS+=("-e Database__EncryptionKey=$ENCRYPTION_KEY") [ -n "$PERSONALITY" ] && ENV_VARS+=("-e Bot__PersonalityName=$PERSONALITY") [ -n "$PRIVACY_MODE" ] && ENV_VARS+=("-e Privacy__Mode=$PRIVACY_MODE") # Default privacy settings ENV_VARS+=( "-e Privacy__DataRetentionHours=24" "-e Privacy__SessionTimeoutMinutes=30" "-e Privacy__EphemeralByDefault=true" "-e Features__EnableQRCodes=true" "-e Features__EnablePGPEncryption=true" "-e Features__EnableDisappearingMessages=true" ) # Build volumes VOLUMES=( "-v ${CONTAINER_NAME}-data:/app/data" "-v ${CONTAINER_NAME}-logs:/app/logs" ) # Deploy the container print_message "$GREEN" "Deploying bot: $CONTAINER_NAME" print_message "$YELLOW" "API URL: $API_URL" $DOCKER_CMD run -d \ --name "$CONTAINER_NAME" \ --restart "$RESTART_POLICY" \ "${ENV_VARS[@]}" \ "${VOLUMES[@]}" \ "$DOCKER_IMAGE" # Check if deployment was successful if [ $? -eq 0 ]; then print_message "$GREEN" "✅ Bot deployed successfully: $CONTAINER_NAME" # Get container info CONTAINER_ID=$($DOCKER_CMD ps -q -f name="$CONTAINER_NAME") print_message "$YELLOW" "Container ID: $CONTAINER_ID" # Show container status $DOCKER_CMD ps -f name="$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" # Follow logs if requested if [ "$FOLLOW_LOGS" = true ]; then print_message "$YELLOW" "Following logs (Ctrl+C to exit)..." $DOCKER_CMD logs -f "$CONTAINER_NAME" else print_message "$YELLOW" "View logs with: docker logs -f $CONTAINER_NAME" fi else print_message "$RED" "❌ Deployment failed" exit 1 fi # Print next steps echo "" print_message "$GREEN" "Next steps:" echo "1. Check bot status: docker ps -f name=$CONTAINER_NAME" echo "2. View logs: docker logs $CONTAINER_NAME" echo "3. Stop bot: docker stop $CONTAINER_NAME" echo "4. Remove bot: docker rm -f $CONTAINER_NAME" echo "5. Access admin panel to manage bot settings"