A voice-enabled AI assistant that integrates Twilio's ConversationRelay with Meta's Llama chat completions API. This system handles incoming phone calls through Twilio, processes voice input in real-time, generates AI responses using Llama models, and streams natural-sounding speech back to the caller.
- ποΈ Real-time Voice Processing: Handles voice input through Twilio ConversationRelay
- π€ AI-Powered Responses: Integrates with Meta Llama API for intelligent conversation
- π WebSocket Communication: Bidirectional real-time communication with Twilio
- π± DTMF Support: Handles touch-tone input for menu navigation
- π― Interruption Handling: Gracefully manages user interruptions during AI responses
- π¨ Enhanced Logging: Beautiful colored logs with icons and smart formatting
- π Simplified Architecture: Clean, minimal code without complex state management (60% less code!)
- Node.js v18+ and npm
- Twilio account with phone number
- Meta Llama API access key
- ngrok or public URL for webhooks (development)
- Clone the repository and install dependencies:
npm install- Copy the environment template and configure:
cp .env.example .env- Edit
.envwith your credentials (see Configuration section)
# Twilio Configuration
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # Your Twilio Account SID
TWILIO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # Your Twilio Auth Token
TWILIO_WELCOME_GREETING="Hello! How can I assist you today?" # Initial greeting
# Meta Llama API
LLAMA_API_KEY=your_api_key_here # Your Llama API key
LLAMA_API_URL=https://api.llama.com/v1/chat/completions # API endpoint
LLAMA_MODEL=Llama-4-Maverick-17B-128E-Instruct-FP8 # Model to use
# External URL (for webhooks)
PUBLIC_URL=https://your-domain.ngrok.io # Your public URL
# Logging (enhanced with colors and icons)
LOG_LEVEL=info # debug, info, warn, error
LOG_FORMAT=simple # 'simple' for pretty colors, 'json' for productionSee .env.example for the complete list of configuration options including:
- Server settings (port, environment)
- Llama model parameters (temperature, max tokens, system prompt)
- WebSocket configuration (path, heartbeat, max payload)
Start the server with hot reload and pretty logging:
npm run devYou'll see a beautiful startup banner:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β π€ CR-META-LLAMA v1.0.0 β
β Twilio ConversationRelay + Meta Llama Integration β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Build and start:
npm run build
npm startRun tests:
npm testngrok http 3000In your Twilio Console:
- Go to Phone Numbers > Manage > Active Numbers
- Select your phone number
- Set the Voice webhook to:
https://your-ngrok-url.ngrok.io/api/voice/incoming - Method: POST
- Set the Status Callback URL to:
https://your-ngrok-url.ngrok.io/api/voice/status
Set PUBLIC_URL in your .env file to your ngrok URL.
POST /api/voice/incoming- Handles incoming voice calls, returns TwiMLPOST /api/voice/action- Handles Connect action callbacksPOST /api/voice/status- Receives call status updatesGET /health- Health check endpoint
wss://[PUBLIC_URL]/ws- ConversationRelay WebSocket connection
-
HTTP Server (Express)
- Handles Twilio webhooks
- Generates TwiML responses with ConversationRelay
- Simple routing and health checks
-
WebSocket Server
- Real-time bidirectional communication
- Implements Twilio ConversationRelay protocol
- Basic connection handling
-
Llama Service
- Meta Llama API integration
- Response generation
- Conversation context from session
-
Session Manager
- Simple call tracking
- Conversation history (last 20 messages)
- Automatic memory management
- Twilio sends call to
/api/voice/incoming - Server returns TwiML with ConversationRelay element
- Twilio establishes WebSocket connection
- Setup message initializes session (welcome greeting via TwiML)
- Voice prompts are transcribed and sent as WebSocket messages
- Prompts are processed through Llama API
- Responses are sent back as text tokens for TTS
- Session ends on hangup or timeout
The system implements the Twilio ConversationRelay WebSocket protocol:
Messages FROM Twilio:
setup- Connection initialization with session detailsprompt- Transcribed voice input from callerdtmf- Touch-tone digit inputinterrupt- Caller interrupted TTS playbackerror- Error notifications
Messages TO Twilio:
text- Text to be converted to speechend- End the session- (Additional message types supported but not yet implemented)
The system features beautiful, color-coded logging with icons:
- π¬ INFO (Cyan) - Normal operations
β οΈ WARN (Yellow) - Warnings- β ERROR (Red) - Errors
- π DEBUG (Gray) - Debug information
Service-specific icons:
- π Twilio events
- π€ Llama API calls
- π WebSocket connections
- π Session management
- β‘ Performance metrics
LOG_LEVEL=info # Verbosity: debug, info, warn, error
LOG_FORMAT=simple # 'simple' for colors, 'json' for structuredLogs are written to files in production:
logs/error.log- Error-level logs onlylogs/combined.log- All logs (JSON format)- Automatic rotation at 5MB, keeps last 5 files
The session manager tracks:
- Total/active sessions
- Average session duration
- Message counts
- API call counts
Access metrics via:
const metrics = sessionManager.getMetrics();-
WebSocket Connection Fails
- Verify PUBLIC_URL is accessible
- Check firewall/security group settings
- Ensure WebSocket path matches configuration (
/ws)
-
Llama API Errors
- Verify API key is valid
- Check API endpoint URL
- Monitor rate limits in logs