An Laravel application that combines real-time voice calls with AI-powered English tutoring. This system integrates Twilio for phone calls, OpenAI's Realtime API for conversational AI, n8n for WhatsApp messaging automation, multiagent AI systems, and WebSocket streaming for seamless audio communication.
- Real-time Voice Calls: Twilio integration for phone-based learning sessions
- AI-Powered Conversations: OpenAI Realtime API for intelligent English tutoring
- WhatsApp Integration: n8n automation for WhatsApp messaging workflows
- Multiagent AI Systems: Coordinated AI agents for enhanced learning experiences
- WebSocket Streaming: Bidirectional audio streaming between calls and AI
- Progress Tracking: Comprehensive learning journey monitoring
- Assessment System: Placement tests and lesson evaluations
- CEFR Level Management: Standard European Framework proficiency tracking
- Async WebSocket Server: Built with Amphp for high-performance concurrent connections
- Audio Processing: G.711 ΞΌ-law format support for telephony integration
- n8n Workflow Automation: Advanced message routing and WhatsApp integration
- Multiagent Architecture: Distributed AI agents for specialized learning tasks
- Real-time Transcription: Automatic speech-to-text for both user and AI responses
- Sanctum Authentication: Secure API token management
- pgsql Database: Lightweight, embedded database solution
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Phone Call β -> β Twilio Service β -> β Laravel API β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βββββββββββββββββββ ββββββββββββββββββββ β
β WhatsApp β -> β n8n Workflows β -----------β
β Messages β β Automation β
βββββββββββββββββββ ββββββββββββββββββββ
β
βββββββββββββββββββ ββββββββββββββββββββ β
β WebSocket β <- β Amphp Server β <----------β
β (Port 1337) β β (Async) β
βββββββββββββββββββ ββββββββββββββββββββ
β β
v β
βββββββββββββββββββ ββββββββββββββββββββ β
β OpenAI β <- β Realtime API β β
β Realtime API β β Service β β
βββββββββββββββββββ ββββββββββββββββββββ β
β β
v β
βββββββββββββββββββ ββββββββββββββββββββ β
β Multiagent β β AI Agent β β
β System β β Coordination β β
βββββββββββββββββββ ββββββββββββββββββββ β
β
ββββββββββββββββββββ β
β pgsql Database β <----------β
β Progress & β
β Assessment Data β
ββββββββββββββββββββ
- PHP 8.4+
- Composer
- Node.js & NPM
- pgsql (included with PHP)
- OpenAI API Key (with Realtime API access)
- Twilio Account (with phone number)
- n8n Instance (for WhatsApp workflow automation)
- WhatsApp Business API (optional, for enhanced messaging)
git clone <repository-url>
cd laravel-lab# PHP dependencies
composer install
# Node.js dependencies (for Vite)
npm install# Install Composer dependencies via Docker (for fresh clone without Composer locally)
docker run --rm \
-u "$(id -u):$(id -g)" \
-v "$(pwd):/var/www/html" \
-w /var/www/html \
laravelsail/php84-composer:latest \
composer install --ignore-platform-reqs
# Start Sail services
./vendor/bin/sail up -d
# Install Node.js dependencies within Sail
./vendor/bin/sail npm install# Copy environment file
cp .env.example .env
# Generate application key
php artisan key:generate
# Create pgsql database
touch database/database.pgsql# Copy environment file
cp .env.example .env
# Generate application key using Sail
./vendor/bin/sail artisan key:generate
# Create pgsql database (Sail will handle permissions)
./vendor/bin/sail exec laravel.test touch database/database.pgsqlEdit .env file with your API credentials:
# Application
APP_NAME="Laravel English Lab"
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
# Database
DB_CONNECTION=pgsql
# Twilio Configuration
TWILIO_ACCOUNT_SID=your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_PHONE_NUMBER=your_twilio_number
TWILIO_WS_URL=ws://your-server:1337/call/
TWILIO_WHATSAPP_ENDPOINT=whatsapp_webhook_url
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key
OPENAI_ORGANIZATION=your_org_id
OPENAI_PROJECT=your_project_id
OPEN_AI_REALTIME_URL=wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01
# n8n Configuration
N8N_HOST=your_n8n_instance_url
N8N_API_KEY=your_n8n_api_key
N8N_WEBHOOK_URL=your_n8n_webhook_url
# Multiagent System
MULTIAGENT_ENABLED=true
MULTIAGENT_COORDINATOR_URL=your_coordinator_endpointphp artisan migrate --seedThis creates the database schema and seeds initial data:
- Roles: admin, student
- Levels: A1, A2, B1, B2, C1, C2 (CEFR standards)
- Status: in_progress, completed, expired
# Start all services concurrently
composer run devThis starts:
- Laravel server (
php artisan serve) - Queue worker (
php artisan queue:listen) - Log viewer (
php artisan pail) - Vite dev server (
npm run dev)
# Laravel API server
php artisan serve
# WebSocket server for call handling
php artisan app:call-web-socket
# Queue worker for background jobs
php artisan queue:work
# Frontend assets
npm run devnpm run build- Incoming Call: Twilio receives call and hits
/api/incoming-call - TwiML Response: Returns XML connecting call to WebSocket
- WebSocket Connection: Amphp server accepts connection on port 1337
- AI Integration: OpenAI Realtime API processes conversation
- Audio Streaming: Bidirectional audio between phone β AI
- Progress Tracking: Conversations and assessments saved to database
The application uses 8 core tables:
users: Student profiles with learning preferencesroles: Access levels (admin/student)levels: CEFR proficiency levels (A1-C2)english_journey_logs: Progress tracking with AI summariesmessages: Conversation history (text/audio)tests: Placement tests and lesson evaluationsquestions: Multi-type questions (listening/writing/MCQ/vocab)status: Progress states (in_progress/completed/expired)
See db-architecture.md for detailed schema documentation.
POST /api/incoming-message- Twilio message webhookPOST /api/incoming-call- Twilio call webhook
GET /api/user- Get authenticated userPUT /api/onboarding/user/{user}- Update user profileGET /api/onboarding/user/{user}- Get onboarding statusPUT /api/onboarding/english-journey-log/{user}- Update learning log
GET /api/messages/{user}- Message historyGET /api/tests/user/{user}- Test statusPOST /api/tests/upload-file/question/{question}- File uploadPOST /api/tests/upload-file-from-twilio/question/{question}- Twilio file upload
# Run PHPUnit tests
composer run test
# Or directly
php artisan testOpenAiRealTimeService: Manages WebSocket connection to OpenAITwilioCallHandlerService: Handles incoming call WebSocket connectionsAuthService: User authentication and managementMessageService: Conversation history managementTestService: Assessment and evaluation logicOnboardingService: User journey management
CallWebSocket: Starts Amphp WebSocket server on port 1337
User: Core user entity with learning progressMessage: Conversation messages with transcriptionsTest&Question: Assessment systemEnglishJourneyLog: Learning milestone tracking
- Twilio Request Validation: Middleware validates webhook signatures
- Sanctum Authentication: Token-based API security
- Input Validation: Form requests for data validation
- CORS Configuration: Proper cross-origin resource sharing
- Environment Variables: Sensitive data in
.env
- Laravel Pail: Real-time log viewing
- Queue Jobs: Background task processing
- WebSocket Logging: Connection and message tracking
- AI Conversation Logs: User interaction monitoring
WebSocket Connection Failed
# Check if port 1337 is available
netstat -tulpn | grep :1337
# Restart WebSocket server
php artisan app:call-web-socketOpenAI API Errors
- Verify API key has Realtime API access
- Check rate limits and billing
- Review OpenAI status page
Twilio Webhook Issues
- Ensure webhook URLs are publicly accessible
- Verify request validation middleware
- Check Twilio webhook logs in console
Database Issues
# Reset database
php artisan migrate:fresh --seed
# Check pgsql file permissions
ls -la database/database.pgsql- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
This project is open-sourced software licensed under the MIT license.
Note: This is the official Laravel Lab application for AI-powered English learning. For production deployment, ensure proper security hardening, scalability considerations, and comprehensive monitoring are implemented.