# AI Feature Migration Guide ## For Existing 3X-UI Users This guide helps you safely upgrade your existing 3X-UI installation to include the new AI-powered Telegram bot feature. ## 📋 Pre-Migration Checklist ### 1. Check Current Version ```bash /usr/local/x-ui/x-ui -v ``` ### 2. Backup Your Database ```bash # Create backup cp /etc/x-ui/x-ui.db /etc/x-ui/x-ui.db.backup.$(date +%Y%m%d_%H%M%S) # Verify backup ls -lh /etc/x-ui/x-ui.db* ``` ### 3. Stop Telegram Bot (Critical!) ```bash # This prevents 409 bot conflicts systemctl stop x-ui ``` ## 🔄 Migration Steps ### Option 1: Automatic Update (Recommended) ```bash # 1. Stop service systemctl stop x-ui # 2. Run update script (when merged to main branch) bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/update.sh) # 3. Start service systemctl start x-ui # 4. Check logs tail -f /var/log/x-ui/3xipl.log ``` ### Option 2: Manual Update (From Source) ```bash # 1. Stop service systemctl stop x-ui # 2. Backup current installation cp -r /usr/local/x-ui /usr/local/x-ui.backup # 3. Pull latest code cd /usr/local/x-ui/source # Or your source directory git fetch origin git checkout main git pull origin main # 4. Build go mod download go build -o /usr/local/x-ui/x-ui ./main.go # 5. Start service systemctl start x-ui # 6. Verify /usr/local/x-ui/x-ui -v ``` ### Option 3: From Feature Branch (Testing) ```bash # For testing the feature before it's merged cd /usr/local/x-ui/source git fetch origin git checkout feat/ai-integration # Or the branch name git pull origin feat/ai-integration go mod download go build -o /usr/local/x-ui/x-ui ./main.go systemctl restart x-ui ``` ## ⚙️ Post-Migration Configuration ### Enable AI Feature #### Method 1: Database (Fastest) ```bash sqlite3 /etc/x-ui/x-ui.db <<'EOF' -- Enable AI INSERT OR REPLACE INTO setting (key, value) VALUES ('aiEnabled', 'true'); -- Add your API key (get from https://makersuite.google.com/app/apikey) INSERT OR REPLACE INTO setting (key, value) VALUES ('aiApiKey', 'YOUR_GEMINI_API_KEY_HERE'); -- Optional: Set max tokens (default: 1024) INSERT OR REPLACE INTO setting (key, value) VALUES ('aiMaxTokens', '1024'); -- Optional: Set temperature (default: 0.7, range: 0.0-1.0) INSERT OR REPLACE INTO setting (key, value) VALUES ('aiTemperature', '0.7'); EOF # Restart to apply systemctl restart x-ui ``` #### Method 2: Web Panel (Recommended for non-technical users) 1. Login to panel: `http://your-server:2053` 2. Navigate to: **Settings** → **Telegram Bot** 3. Scroll to: **AI Integration** section 4. Toggle: **Enable AI Features** → ON 5. Paste: Your Gemini API key 6. Click: **Save Settings** 7. Click: **Restart Panel** (or restart Telegram bot) #### Method 3: API Call ```bash # Get your session token first by logging in SESSION_TOKEN="your_session_token_here" # Update AI settings curl -X POST http://localhost:2053/panel/api/setting/ai/update \ -H "Content-Type: application/json" \ -H "Cookie: session=$SESSION_TOKEN" \ -d '{ "enabled": true, "apiKey": "YOUR_GEMINI_API_KEY", "maxTokens": 1024, "temperature": 0.7 }' ``` ## ✅ Verification ### Test AI Integration 1. **Check Logs** ```bash tail -f /var/log/x-ui/3xipl.log | grep -i "ai" # Expected output: # [INFO] Telegram Bot: AI service initialized - Enabled: true # [INFO] AI Service: Gemini client initialized successfully ``` 2. **Test in Telegram** Open your bot and send: ``` show server status ``` Expected response: Server metrics (CPU, memory, traffic, etc.) 3. **Verify Settings** ```bash sqlite3 /etc/x-ui/x-ui.db "SELECT key, value FROM setting WHERE key LIKE 'ai%';" # Expected output: # aiEnabled|true # aiApiKey|AIza... # aiMaxTokens|1024 # aiTemperature|0.7 ``` ## 🔍 Troubleshooting ### Issue: "AI service not enabled" **Diagnosis:** ```bash # Check if enabled sqlite3 /etc/x-ui/x-ui.db "SELECT value FROM setting WHERE key = 'aiEnabled';" # Check if API key exists sqlite3 /etc/x-ui/x-ui.db "SELECT length(value) FROM setting WHERE key = 'aiApiKey';" ``` **Solution:** ```bash # Enable and add API key sqlite3 /etc/x-ui/x-ui.db <