# TeleFlow CRM

Advanced Telegram CRM with AI-powered automation, multi-workspace support, and visual flow builder.

## Features

- 🚀 Multi-Telegram Account Management
- 👥 Advanced CRM with Pipelines & Tags
- 🤖 AI Assistant (OpenAI, Claude, Gemini, DeepSeek)
- 📊 Campaign Management with Analytics
- 🔄 Visual Flow Builder (Drag & Drop Automation)
- 💰 Multi-Currency Support (10+ currencies)
- 💳 Multiple Payment Methods (Stripe, Cryptomus, Manual)
- 🌍 Internationalization (English + Chinese)
- 👨‍💼 Multi-Workspace with Role-Based Access
- 📈 Subscription & Billing System

## Requirements

- PHP 8.3 or higher
- MySQL 8.0+ or MariaDB 10.3+
- Redis
- Composer
- Web server (Apache/Nginx)

### Required PHP Extensions

- PDO & PDO MySQL
- Redis
- mbstring
- JSON
- cURL
- OpenSSL
- Fileinfo
- GD
- Zip

## Installation

### 1. Clone or Download

```bash
# Download and extract to your web directory
cd /path/to/webroot
```

### 2. Install Dependencies

```bash
composer install
```

### 3. Set Permissions

```bash
# Linux/Mac
chmod -R 775 storage
chmod -R 775 public

# Windows - ensure IIS/Apache user has write access to storage/
```

### 4. Run Web Installer

Navigate to: `http://yourdomain.com/install.php`

The installer will guide you through:
- System requirements check
- Database configuration
- Application settings
- Admin account creation

### 5. Delete Installer

**IMPORTANT:** After installation completes, delete `/public/install.php` for security:

```bash
rm public/install.php
```

### 6. Setup Queue Worker

The queue worker processes background jobs (campaigns, AI responses, etc).

#### Option A: Supervisor (Recommended for VPS)

Create `/etc/supervisor/conf.d/teleflow-worker.conf`:

```ini
[program:teleflow-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/teleflow-crm/worker.php
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/path/to/teleflow-crm/storage/logs/worker.log
```

Start worker:

```bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start teleflow-worker:*
```

#### Option B: systemd

Create `/etc/systemd/system/teleflow-worker.service`:

```ini
[Unit]
Description=TeleFlow CRM Queue Worker
After=network.target

[Service]
Type=simple
User=www-data
WorkingDirectory=/path/to/teleflow-crm
ExecStart=/usr/bin/php /path/to/teleflow-crm/worker.php
Restart=always

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

Enable and start:

```bash
sudo systemctl enable teleflow-worker
sudo systemctl start teleflow-worker
```

#### Option C: Cron (Shared Hosting)

Add to crontab:

```bash
* * * * * cd /path/to/teleflow-crm && php worker.php > /dev/null 2>&1
```

### 7. Setup Cron Jobs

Add to crontab:

```bash
* * * * * php /path/to/teleflow-crm/cron.php
```

This runs scheduled tasks:
- Telegram account health checks (every 5 min)
- Process scheduled campaigns (every minute)
- Clean old sessions (daily)
- Check subscription expirations (daily)
- Generate invoices (daily)

## Configuration

All configuration is in `.env` file (created during installation).

### Key Settings

```env
APP_NAME="TeleFlow CRM"
APP_URL=https://yourdomain.com
APP_ENV=production
APP_DEBUG=false

# Payment Methods
STRIPE_KEY=pk_live_xxx
STRIPE_SECRET=sk_live_xxx
CRYPTOMUS_MERCHANT_ID=xxx
CRYPTOMUS_API_KEY=xxx

# AI Providers
OPENAI_API_KEY=sk-xxx
CLAUDE_API_KEY=sk-ant-xxx
GEMINI_API_KEY=xxx
DEEPSEEK_API_KEY=xxx
```

## Web Server Configuration

### Apache

Uses `.htaccess` file (already included in `/public`).

Ensure `mod_rewrite` is enabled:

```bash
sudo a2enmod rewrite
sudo systemctl restart apache2
```

### Nginx

Copy the example config:

```bash
cp nginx.conf.example /etc/nginx/sites-available/teleflow
ln -s /etc/nginx/sites-available/teleflow /etc/nginx/sites-enabled/
```

Edit the config:
- Replace `/path/to/teleflow-crm` with actual path
- Replace `teleflow.yourdomain.com` with your domain
- Add SSL certificates (recommended)

Reload Nginx:

```bash
sudo nginx -t
sudo systemctl reload nginx
```

## SSL Certificate (Recommended)

### Using Let's Encrypt (Free)

```bash
sudo certbot --nginx -d yourdomain.com
```

## Post-Installation

1. Login at: `http://yourdomain.com/login`
2. Use admin credentials created during installation
3. Go to Admin Panel → Settings:
   - Add payment method API keys
   - Configure email settings
   - Set default language
   - Update currency rates

## Upgrade

```bash
# Backup database and files first!
git pull origin main  # or download new version
composer install
# Run any new migrations (check release notes)
```

## Troubleshooting

### 500 Error

Check `storage/logs/` for error details.

Common fixes:
- Verify storage/ permissions are writable
- Check .env file exists and has correct database credentials
- Ensure all PHP extensions are installed

### Queue Jobs Not Processing

- Verify worker is running: `ps aux | grep worker.php`
- Check worker logs: `storage/logs/worker.log`
- Test Redis connection: `redis-cli ping`

### Telegram Account Connection Fails

- Verify MadelineProto is installed: `composer show danog/madelineproto`
- Check PHP has enough memory: increase `memory_limit` to 256M
- Verify outbound HTTPS connections are allowed

## Support

- Documentation: [docs.teleflow.app](https://docs.teleflow.app)
- Issues: [github.com/yourrepo/issues](https://github.com/yourrepo/issues)
- Email: support@teleflow.app

## License

Proprietary - All rights reserved

## Security

If you discover a security vulnerability, please email security@teleflow.app

---

Built with ❤️ for Telegram CRM automation
