# AI News Digest Bot - Deployment Guide

## Overview

This guide covers deploying the AI News Digest Bot for production use with Discord and Telegram delivery.

## Prerequisites

### Required Dependencies

```bash
# Python packages
pip install feedparser requests python-dateutil

# Or install from requirements.txt if available
pip install -r requirements.txt
```

### Required API Keys

1. **OpenAI API Key** (for AI filtering)
   - Sign up at https://platform.openai.com/
   - Create API key in dashboard
   - Set as `OPENAI_API_KEY` environment variable

2. **Discord Webhook** (for Discord delivery)
   - Go to Discord server settings → Integrations → Webhooks
   - Create new webhook for your target channel
   - Copy webhook URL
   - Set as `DISCORD_WEBHOOK` environment variable

3. **Telegram Bot** (for Telegram delivery)
   - Message @BotFather on Telegram
   - Create new bot with `/newbot`
   - Get bot token and set as `TELEGRAM_BOT_TOKEN`
   - Get chat ID for target channel/group and set as `TELEGRAM_CHAT_ID`

## Environment Variables

Create a `.env` file or set these environment variables:

```bash
# Required for AI filtering
OPENAI_API_KEY=sk-your-openai-key-here
OPENAI_MODEL=gpt-3.5-turbo

# Optional: Digest configuration
DIGEST_MAX_AGE_HOURS=24
DIGEST_MIN_SCORE=6.0
DIGEST_MAX_ARTICLES=50
DIGEST_TOP_STORIES=5
DIGEST_FORMATS=discord,telegram

# Discord delivery (optional)
DISCORD_WEBHOOK=https://discord.com/api/webhooks/your-webhook-url

# Telegram delivery (optional)
TELEGRAM_BOT_TOKEN=your-bot-token
TELEGRAM_CHAT_ID=your-chat-id
```

## Manual Deployment

### 1. Basic Setup

```bash
# Clone or copy the skill to your server
cd /path/to/ai-news-digest

# Make scripts executable
chmod +x scripts/*.py

# Test the setup
python3 scripts/run_digest.py status
```

### 2. Test Individual Components

```bash
# Test RSS aggregation
python3 scripts/run_digest.py test rss

# Test AI filtering (requires OpenAI key)
python3 scripts/rss_aggregator.py
python3 scripts/ai_filter.py cache/articles_*.json --top-stories 5

# Test digest compilation
python3 scripts/run_digest.py test compile
```

### 3. Run Full Pipeline

```bash
# Generate digest for Discord and Telegram
python3 scripts/run_digest.py run --formats discord telegram

# Generate without automatic delivery
python3 scripts/run_digest.py run --no-delivery
```

## Automated Deployment (Cron)

### Daily Digest Cron Job

Add to your crontab (`crontab -e`):

```bash
# Run daily AI news digest at 9 AM UTC
0 9 * * * cd /path/to/ai-news-digest && python3 scripts/run_digest.py run --formats discord telegram >> logs/digest.log 2>&1

# Alternative: Run every 6 hours
0 */6 * * * cd /path/to/ai-news-digest && python3 scripts/run_digest.py run >> logs/digest.log 2>&1
```

### Systemd Service (Linux)

Create `/etc/systemd/system/ai-news-digest.service`:

```ini
[Unit]
Description=AI News Digest Bot
After=network.target

[Service]
Type=oneshot
User=your-username
WorkingDirectory=/path/to/ai-news-digest
Environment=PATH=/usr/bin:/usr/local/bin
EnvironmentFile=/path/to/ai-news-digest/.env
ExecStart=/usr/bin/python3 scripts/run_digest.py run
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
```

Create timer `/etc/systemd/system/ai-news-digest.timer`:

```ini
[Unit]
Description=Run AI News Digest daily
Requires=ai-news-digest.service

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target
```

Enable and start:

```bash
sudo systemctl enable ai-news-digest.timer
sudo systemctl start ai-news-digest.timer
sudo systemctl status ai-news-digest.timer
```

## Docker Deployment

### Dockerfile

```dockerfile
FROM python:3.11-slim

WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy skill files
COPY . .

# Make scripts executable
RUN chmod +x scripts/*.py

# Create necessary directories
RUN mkdir -p cache output logs

# Run the digest
CMD ["python3", "scripts/run_digest.py", "run"]
```

### docker-compose.yml

```yaml
version: '3.8'

services:
  ai-news-digest:
    build: .
    environment:
      - OPENAI_API_KEY=${OPENAI_API_KEY}
      - DISCORD_WEBHOOK=${DISCORD_WEBHOOK}
      - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN}
      - TELEGRAM_CHAT_ID=${TELEGRAM_CHAT_ID}
    volumes:
      - ./cache:/app/cache
      - ./output:/app/output
      - ./logs:/app/logs
    restart: "no"
```

### Docker Cron Setup

Use a cron container or host cron to run:

```bash
# Daily at 9 AM UTC
0 9 * * * docker-compose -f /path/to/docker-compose.yml up --build
```

## Monitoring and Maintenance

### Log Monitoring

Check logs for errors:

```bash
# View recent logs
tail -f logs/digest.log

# Check for errors
grep ERROR logs/digest.log

# Monitor with systemd journal
sudo journalctl -u ai-news-digest.service -f
```

### Health Checks

```bash
# Check system status
python3 scripts/run_digest.py status

# Test connectivity to sources
python3 scripts/run_digest.py test rss --limit 1

# Validate output files
ls -la output/digest_*
```

### Backup and Cleanup

```bash
# Backup important data
tar -czf backup_$(date +%Y%m%d).tar.gz output/ cache/

# Clean old cache files (older than 7 days)
find cache/ -name "*.json" -mtime +7 -delete

# Clean old output files (older than 30 days)
find output/ -name "digest_*" -mtime +30 -delete
```

## Troubleshooting

### Common Issues

1. **No articles found**
   - Check RSS sources in `references/sources.json`
   - Verify network connectivity
   - Check source websites for changes

2. **AI filtering fails**
   - Verify OpenAI API key is valid and has credits
   - Check API rate limits
   - Review error logs

3. **Delivery failures**
   - Verify webhook URLs and bot tokens
   - Check message size limits
   - Test with manual delivery command

4. **RSS parsing errors**
   - Some feeds may be malformed or require user agents
   - Update `rss_aggregator.py` if needed
   - Add fallback sources

### Performance Optimization

1. **Reduce API costs**
   - Use cheaper models (gpt-3.5-turbo vs gpt-4)
   - Implement caching for similar articles
   - Pre-filter articles by keywords

2. **Speed up RSS aggregation**
   - Parallel processing of feeds
   - HTTP connection pooling
   - Intelligent caching

3. **Improve reliability**
   - Add retry logic for failed requests
   - Implement circuit breaker for problematic sources
   - Graceful degradation when AI is unavailable

## Scaling

### Multiple Instances

For high-frequency updates or multiple channels:

```bash
# Different configurations for different audiences
python3 scripts/run_digest.py run --config configs/technical.json
python3 scripts/run_digest.py run --config configs/business.json
python3 scripts/run_digest.py run --config configs/general.json
```

### Custom Sources

Add your own RSS sources to `references/sources.json`:

```json
{
  "name": "Your Custom Source",
  "url": "https://example.com",
  "rss": "https://example.com/feed.xml",
  "category": "custom",
  "weight": 0.8,
  "description": "Description of the source"
}
```

### Integration with Other Services

- **Slack**: Modify delivery code for Slack webhooks
- **Email**: Use SMTP for email delivery
- **RSS**: Generate your own RSS feed from digests
- **API**: Create REST API endpoints for digest data