# Webhook Connection Timeout - Troubleshooting Guide

## Issue
```
Last Error: Connection timed out
Webhook URL: https://telegrambots.iwe3.cloud/earnify//bot/webhook.php
```

## Fixes Applied

### 1. Fixed Double Slash in URL ✅
**Problem:** BASE_URL had trailing slash causing `earnify//bot/webhook.php`  
**Solution:** Removed trailing slash from BASE_URL in `config.php`  
**New URL:** `https://telegrambots.iwe3.cloud/earnify/bot/webhook.php`

### 2. Created Logs Directory ✅
**Problem:** Logs directory missing  
**Solution:** Run `admin/create_logs.php` to create directory

## Steps to Fix Webhook

### Step 1: Update Webhook URL
Visit this URL to set correct webhook:
```
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook?url=https://telegrambots.iwe3.cloud/earnify/bot/webhook.php
```

Or use the "Set Webhook" button in diagnostics page.

### Step 2: Verify Webhook File
Check that `webhook.php` is accessible:
```
https://telegrambots.iwe3.cloud/earnify/bot/webhook.php
```

Should return blank page (not 404 or 500 error).

### Step 3: Check Server Configuration

#### PHP Version
Ensure PHP 8.0+ is installed:
```bash
php -v
```

#### File Permissions
```bash
chmod 644 earnify/bot/webhook.php
chmod 755 earnify/bot
chmod 755 earnify/logs
```

#### .htaccess (if needed)
Create `earnify/.htaccess`:
```apache
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /earnify/
</IfModule>

# Allow webhook access
<Files "webhook.php">
    Order allow,deny
    Allow from all
</Files>
```

### Step 4: Test Webhook Manually

Send test update to webhook:
```bash
curl -X POST https://telegrambots.iwe3.cloud/earnify/bot/webhook.php \
  -H "Content-Type: application/json" \
  -d '{"update_id":1,"message":{"message_id":1,"from":{"id":123,"first_name":"Test"},"chat":{"id":123},"text":"/start"}}'
```

Should return HTTP 200 (even if blank response).

### Step 5: Check Logs

After webhook is set, check logs:
```
earnify/logs/webhook_YYYY-MM-DD.log
earnify/logs/error_YYYY-MM-DD.log
```

## Common Issues

### Issue: 404 Not Found
**Cause:** File path incorrect or mod_rewrite issue  
**Fix:** Verify file exists, check .htaccess

### Issue: 500 Internal Server Error
**Cause:** PHP syntax error or missing dependencies  
**Fix:** Check PHP error logs, verify all files uploaded

### Issue: Connection Timeout
**Cause:** Server firewall, slow response, or infinite loop  
**Fix:** 
- Check server firewall allows Telegram IPs
- Optimize webhook.php (already done)
- Check for infinite loops

### Issue: SSL Certificate Error
**Cause:** Invalid or self-signed SSL  
**Fix:** Use valid SSL certificate (Let's Encrypt recommended)

## Telegram IP Ranges (for Firewall)

Allow these IP ranges:
```
149.154.160.0/20
91.108.4.0/22
```

## Verification Checklist

- [ ] BASE_URL has no trailing slash
- [ ] Webhook URL is correct (single slash)
- [ ] webhook.php file exists and is readable
- [ ] Logs directory exists and is writable
- [ ] PHP version is 8.0+
- [ ] Database connection works
- [ ] SSL certificate is valid
- [ ] Server allows Telegram IPs
- [ ] No PHP errors in error_log

## Testing

1. **Set webhook** via diagnostics page
2. **Send /start** to bot on Telegram
3. **Check logs** for webhook activity
4. **Verify** bot responds

## Support

If issues persist:
1. Check `logs/error_*.log` for PHP errors
2. Check `logs/webhook_*.log` for incoming requests
3. Check server error logs (usually in cPanel)
4. Verify all configuration in diagnostics page

---

**Status:** Fixes applied, ready for webhook reset
