# Publish to Telegram/X - Setup Guide

## Quick Start

### Step 1: Migrate Database

```bash
cd /home/rasamemek/public_html/player2027

# Run migration to create publish_logs table
php artisan migrate
```

### Step 2: Configure Environment Variables

Add to `.env` file:

```env
# Telegram Bot
TELEGRAM_BOT_TOKEN=your_bot_token_here

# X (Twitter) - API v2
X_BEARER_TOKEN=your_bearer_token_here

# Optional: X API v1.1 credentials
X_API_KEY=optional
X_API_SECRET=optional
X_ACCESS_TOKEN=optional
X_ACCESS_TOKEN_SECRET=optional
```

### Step 3: Get Credentials

#### For Telegram:
1. Talk to [@BotFather](https://t.me/BotFather)
2. Send `/newbot` and follow instructions
3. Copy the token to `TELEGRAM_BOT_TOKEN`
4. To find chat ID:
   - Personal: Talk to [@userinfobot](https://t.me/userinfobot)
   - Group: Add bot to group and check message

#### For X (Twitter):
1. Go to [developer.twitter.com](https://developer.twitter.com)
2. Create/Access your app
3. Go to "Keys and tokens"
4. Copy Bearer Token to `X_BEARER_TOKEN`
5. Make sure app has write permissions

### Step 4: Clear Config Cache

```bash
php artisan config:clear
```

### Step 5: Start Queue Worker

```bash
# For development
php artisan queue:work

# For production, use supervisor
```

### Step 6: Test

1. Go to Admin → Videos
2. Click "📢 Publish" on any ready video
3. Fill in the form and submit
4. Check publish history to see status

## File Changes Summary

### Created Files:

1. **Database**:
   - `database/migrations/2026_06_10_create_publish_logs_table.php`

2. **Models**:
   - `app/Models/PublishLog.php`

3. **Services**:
   - `app/Services/PublishService.php`
   - `app/Services/TelegramBotService.php`
   - `app/Services/XPublishService.php`

4. **Jobs**:
   - `app/Jobs/TelegramPublishJob.php`
   - `app/Jobs/XPublishJob.php`

5. **Controllers**:
   - `app/Http/Controllers/Admin/PublishController.php`

6. **Views**:
   - `resources/views/admin/videos/publish-form.blade.php`
   - `resources/views/admin/videos/publish-history.blade.php`

### Modified Files:

1. **Models**:
   - `app/Models/Video.php` - Added `publishLogs()` relationship

2. **Configuration**:
   - `config/services.php` - Added Telegram and X config

3. **Routes**:
   - `routes/web.php` - Added publish routes

4. **Views**:
   - `resources/views/admin/videos/index.blade.php` - Added publish button

## Testing the Feature

### Test Telegram Publish:

```bash
# SSH into server
# Go to project directory
cd /home/rasamemek/public_html/player2027

# Open tinker
php artisan tinker

# Create test publish
$video = App\Models\Video::find(1);
$service = app(App\Services\PublishService::class);
$data = $service->preparePublishData($video, [
    'publish_title' => 'Test Title',
    'platform' => 'telegram',
]);
$log = $service->publishToTelegram($video, $data, YOUR_CHAT_ID);
```

### Check Logs:

```bash
tail -f storage/logs/laravel.log
```

## Troubleshooting

### Queue Jobs Not Processing?

Check if queue worker is running:
```bash
ps aux | grep queue:work
```

If not, start it:
```bash
php artisan queue:work
```

### Telegram Not Working?

1. Verify token:
```bash
curl -X GET https://api.telegram.org/botTOKEN/getMe
```

2. Test send message:
```bash
curl -X POST https://api.telegram.org/botTOKEN/sendMessage \
  -d "chat_id=CHAT_ID&text=test"
```

### X Not Working?

1. Verify bearer token in config:
```bash
php artisan config:get services.x.bearer_token
```

2. Test API call:
```bash
curl -H "Authorization: Bearer TOKEN" \
  https://api.twitter.com/2/users/me
```

## Features Overview

✅ **Custom Title** - Change title for each publish
✅ **Custom Thumbnail** - Upload new, paste URL, or use original
✅ **Real-time Preview** - See thumbnail before publishing
✅ **Two Platforms** - Telegram and X (Twitter) support
✅ **Publish History** - Track all publish attempts
✅ **Retry Failed** - Retry failed publishes
✅ **Original Data Safe** - Never modifies original video data
✅ **Async Processing** - Publish via queue jobs

## Important Notes

⚠️ **Original Video Data is NEVER Modified**
- Only creates record in `publish_logs` table
- Original title, thumbnail, URL remain unchanged

⚠️ **Queue Processing**
- All publishes are async (queued)
- Need `php artisan queue:work` running
- Use supervisor in production

⚠️ **API Credentials**
- Keep `.env` file secure
- Never commit `.env` to git
- Rotate tokens periodically

## Support

For issues or questions, check:
- `PUBLISH_FEATURE_DOCUMENTATION.md` - Detailed documentation
- `storage/logs/laravel.log` - Error logs
- Database table `publish_logs` - Publish history

## Next Steps

1. ✅ Run migration
2. ✅ Add environment variables
3. ✅ Clear config cache
4. ✅ Start queue worker
5. ✅ Test with first video
6. ✅ Monitor publish history

Happy publishing! 🚀
