# cPanel Deployment Guide

## Requirements
- cPanel with **Terminal** access
- cPanel with **Node.js** app support (Setup Node.js App)
- A domain or subdomain pointing to your cPanel hosting
- SSL certificate (Let's Encrypt via cPanel)

---

## Step 1: Upload Files

Upload your bot files to cPanel via **File Manager** or **Terminal**:

```bash
# If using Terminal, go to your home directory
cd ~

# Upload files or clone from git
# Make sure these files are in your domain's folder or a subfolder
```

**Important files to upload:**
- All `.py` files (run_webhook.py, setup_webhook.py, bot.py, etc.)
- `requirements.txt`
- `.env` (create from `.env.example`)
- `templates/` folder
- `handlers/`, `keyboards/`, `middlewares/`, `utils/` folders

---

## Step 2: Install Python Dependencies

Open **Terminal** in cPanel and run:

```bash
cd ~/your_project_folder
pip install --user -r requirements.txt
```

If `pip` is not available, try:
```bash
python3 -m pip install --user -r requirements.txt
```

Or use the **Setup Node.js App** to create a Python app first (it sets up the Python environment).

---

## Step 3: Configure Environment

```bash
cd ~/your_project_folder
cp .env.example .env
nano .env
```

Fill in your settings:
```
BOT_TOKEN=your_bot_token
ADMIN_IDS=your_telegram_id
PANEL_URL=https://your-3xui-panel.com
PANEL_USER=admin
PANEL_PASS=your_password
ADMIN_WEB_USER=admin
ADMIN_WEB_PASS=your_web_password
WEBHOOK_URL=https://yourdomain.com
WEBHOOK_SECRET=some_random_string_here
WEB_PORT=5000
```

**Generate a webhook secret:**
```bash
python3 -c "import secrets; print(secrets.token_hex(32))"
```

---

## Step 4: Setup Node.js App in cPanel

1. Go to **cPanel → Software → Setup Node.js App**
2. Click **Create Application**
3. Configure:
   - **Node.js version**: Latest available
   - **Application mode**: Production
   - **Application root**: your_project_folder
   - **Application startup file**: `run_webhook.py`
4. Click **Create**
5. Click **Run NPM Install** (skip if no package.json needed)

---

## Step 5: Alternative - Use Terminal to Run

If Node.js app setup doesn't work, use Terminal:

```bash
cd ~/your_project_folder

# Set webhook first
python3 setup_webhook.py https://yourdomain.com

# Run the bot in background
nohup python3 run_webhook.py > bot.log 2>&1 &
```

To check if running:
```bash
ps aux | grep run_webhook
cat bot.log
```

To stop:
```bash
kill $(pgrep -f run_webhook)
```

---

## Step 6: Set the Webhook

```bash
cd ~/your_project_folder
python3 setup_webhook.py https://yourdomain.com
```

Expected output:
```
Setting webhook to: https://yourdomain.com/webhook
Webhook set successfully!

Webhook info:
  URL: https://yourdomain.com/webhook
  Pending updates: 0
  No errors
```

---

## Step 7: Test

1. Visit `https://yourdomain.com/health` - should return `{"status": "ok"}`
2. Open your Telegram bot and send `/start`
3. Check the web panel at `https://yourdomain.com`

---

## Troubleshooting

### Bot not responding
```bash
# Check logs
cat bot.log

# Check if process is running
ps aux | grep run_webhook

# Check webhook info
python3 -c "
import asyncio, os
from aiogram import Bot
async def check():
    bot = Bot(os.getenv('BOT_TOKEN'))
    info = await bot.get_webhook_info()
    print(f'URL: {info.url}')
    print(f'Error: {info.last_error_message}')
    await bot.session.close()
asyncio.run(check())
"
```

### "Pending updates" not decreasing
- The bot may not be receiving updates. Check:
  1. Domain SSL is working (https://)
  2. Firewall not blocking Telegram IPs
  3. `WEBHOOK_URL` matches your actual domain

### Process dies after cPanel terminal closes
cPanel terminal sessions are not persistent. Use one of:
1. **cPanel Node.js App** (recommended)
2. **Screen/Termux** (if available): `screen -S bot` then run inside
3. **Cron job** to restart (not ideal but works)

---

## Quick Commands Reference

```bash
# Start
cd ~/your_project_folder
nohup python3 run_webhook.py > bot.log 2>&1 &

# Stop
kill $(pgrep -f run_webhook)

# Restart
kill $(pgrep -f run_webhook); sleep 2; nohup python3 run_webhook.py > bot.log 2>&1 &

# View logs
tail -f bot.log

# Set webhook
python3 setup_webhook.py https://yourdomain.com

# Check webhook status
python3 setup_webhook.py
```
