Skip to content

Latest commit

 

History

History
429 lines (317 loc) · 12.9 KB

File metadata and controls

429 lines (317 loc) · 12.9 KB

🤖 Enhanced Voice Bot Echo Server for Exotel

A comprehensive, intelligent WebSocket echo server with conversational AI behavior and real-time monitoring dashboard specifically designed for testing Exotel's voice streaming functionality. Features advanced audio buffering, silence detection, and interactive analytics.

Python WebSockets Flask License Status

✨ What This Does

🧠 Conversational AI Echo: Listens first, detects silence, then responds naturally - no more immediate echo interruptions

🎧 Audio Buffering: Intelligently buffers incoming audio and responds after silence detection

🛑 Smart Interruption: Handles CLEAR events to stop speaking and reset conversation state

📊 Real-time Dashboard: Live monitoring with interactive latency metrics and event visualization

🔍 Advanced Protocol Testing: Handles all Exotel WebSocket events with enhanced logging and response acknowledgments

Production Ready: Robust error handling, session management, and multiline log parsing

🚀 Easy Setup: One-command installation with automated dependency management

🆕 Enhanced Features

🎯 Conversational Echo Bot

  • Listen → Silence → Respond: Natural conversation flow instead of immediate echo
  • Audio Buffering: Collects audio chunks during listening phase
  • Silence Detection: 2-second silence threshold before responding
  • Clear Interruption: Instantly stops and resets on CLEAR events
  • Session Management: Per-call state tracking and cleanup

📊 AgentStream Dashboard (Sample Reference)

  • Real-time Event Feed: Live stream of all voice bot activities
  • Latency Analytics: Inter-event, first media, and end-to-end latency tracking
  • Interactive Tooltips: Hover explanations for all metrics
  • Call Session Tracking: Detailed per-call event analysis
  • Multiline Log Parsing: Accurate DTMF digit extraction and event correlation

Note: The dashboard is provided as a sample reference implementation. While it provides valuable insights into system behavior, data accuracy may vary depending on log timing and parsing complexity. Use it for monitoring and debugging purposes.

🏃‍♂️ Quick Start (2 Minutes)

Prerequisites

  • Python 3.8+
  • Ports 5000 (server) and 5001 (dashboard) available
  • Internet connection

Installation

# Clone the repository
git clone https://github.com/exotel/Agent-Stream-echobot.git
cd Agent-Stream-echobot

# One-command setup
chmod +x setup.sh && ./setup.sh

Start the Services

# Start both server and dashboard
./start.sh

# Or start individually:
# Enhanced Echo Server (port 5000)
source venv/bin/activate && python3 simple_server.py &

# AgentStream Dashboard (port 5001)  
source venv/bin/activate && python3 dashboard.py &

Access Points:

  • 🤖 Echo Server: ws://localhost:5000
  • 📊 Dashboard: http://localhost:5001

🌐 Public Access with ngrok

To test with Exotel, you need a public WSS URL:

# Install ngrok (if not already installed)
brew install ngrok  # macOS
# or download from https://ngrok.com/

# Configure your ngrok authtoken
ngrok config add-authtoken YOUR_NGROK_TOKEN

# Make your server public
ngrok http 5000

Use the wss:// URL from ngrok in your Exotel configuration.

🧪 Testing

Basic Connection Test

# Test local server
python3 test_connection.py

# Test public ngrok URL
python3 test_connection.py wss://your-ngrok-url.ngrok.io

Enhanced Features Test

# Test conversational behavior and all features
python3 test_enhanced_features.py

📋 Exotel Configuration

For Bidirectional Streaming (Voicebot Applet)

  1. URL: wss://your-ngrok-url.ngrok.io
  2. Custom Parameters: Optional (will be logged)
  3. Record: Enable if you want call recordings
  4. Next Applet: Configure your next flow step

For Unidirectional Streaming (Stream Applet)

  1. Action: Start
  2. URL: wss://your-ngrok-url.ngrok.io
  3. Next Applet: Configure your next flow step

🎯 Enhanced Call Flow

Traditional Echo Flow

User speaks → Immediate echo → Interruption → Poor UX

Enhanced Conversational Flow

1. 🎧 LISTENING: User speaks → Audio buffering
2. 🤔 SILENCE: 2s silence detected → Prepare response  
3. 🗣️ SPEAKING: Send buffered audio naturally
4. 🛑 CLEAR: Handle interruptions gracefully
5. 👂 RESET: Ready for next turn

📊 Monitoring & Analytics

Real-time Dashboard Features

  • 📈 Live Metrics: Calls, media packets, events with tooltips
  • ⏱️ Latency Tracking:
    • Avg Latency: Time between consecutive events
    • First Media: Connection to first audio packet
    • End-to-End: Complete call duration
  • 🎯 Event Feed: Real-time activity stream with filtering
  • 📱 Call Sessions: Interactive call selection and analysis
  • 🧹 Log Management: Clear logs and export functionality

Enhanced Logging

The server creates comprehensive logs in the logs/ directory:

  • voice_bot_echo.log: Enhanced server activity with conversation flow
  • calls.log: Individual call details in JSON format

Log Monitoring Commands

# Watch enhanced server logs
tail -f logs/voice_bot_echo.log

# Monitor conversational behavior
grep -E "(LISTENING|BUFFERING|SILENCE|SPEAKING|CLEAR)" logs/voice_bot_echo.log

# Watch specific events
grep "DTMF EVENT" logs/voice_bot_echo.log

📁 Enhanced File Structure

Agent-Stream-echobot/
├── simple_server.py          # Enhanced conversational echo server
├── dashboard.py               # Real-time monitoring dashboard
├── dashboard_fixed.py         # Dashboard optimizations
├── enhanced_parser.py         # Advanced log parsing utilities
├── test_connection.py         # Basic connection testing
├── test_enhanced_features.py  # Comprehensive feature testing
├── requirements.txt           # Enhanced dependencies (Flask, SocketIO)
├── setup.sh                   # Automated setup script
├── start.sh                   # Multi-service startup script
├── templates/
│   └── dashboard.html         # Interactive dashboard UI
├── logs/                      # Log files (created at runtime)
│   ├── voice_bot_echo.log     # Enhanced server logs
│   └── calls.log              # Call session data
└── venv/                      # Virtual environment

🔧 Advanced Configuration

Echo Mode Configuration

Choose between immediate echo (traditional) or conversational AI mode:

# In simple_server.py - Line ~153
IMMEDIATE_ECHO_MODE = True   # Traditional immediate echo for testing
IMMEDIATE_ECHO_MODE = False  # Conversational AI with silence detection

Conversation Parameters (when IMMEDIATE_ECHO_MODE = False)

class VoiceSession:
    def __init__(self, connection_id, websocket):
        # Customize these parameters
        self.silence_threshold = 2.0    # Seconds before responding
        self.response_delay = 0.1       # Delay between audio chunks

Dashboard Customization

Edit dashboard.py for custom analytics:

# Modify latency calculations
live_stats = {
    'custom_metric': your_calculation,
    'threshold_alerts': custom_thresholds
}

Custom Port Configuration

# Set custom ports via environment variables
export ECHO_PORT=8080
export DASHBOARD_PORT=8081

# Or edit the files directly

🛠️ Troubleshooting

Enhanced Server Issues

# Check server status
curl -f http://localhost:5000 || echo "Server not responding"

# Monitor conversation flow
grep -E "(LISTENING|SPEAKING)" logs/voice_bot_echo.log | tail -10

# Check session cleanup
grep "Connection ended" logs/voice_bot_echo.log | tail -5

Dashboard Issues

# Verify dashboard
curl -f http://localhost:5001 || echo "Dashboard not accessible"

# Check log parsing
grep "Error parsing" logs/* 

# Monitor WebSocket connections
grep "connected\|disconnected" logs/voice_bot_echo.log

Latency Issues

  1. High Inter-event Latency: Check network connection and server load
  2. Poor First Media: Verify Exotel connection establishment
  3. Long End-to-End: Review call flow and timeout configurations

🎨 Customization Examples

Add Custom Audio Processing

async def start_response(self):
    """Enhanced response with custom processing"""
    for i, media_data in enumerate(self.audio_buffer):
        # Your custom audio processing
        processed_audio = your_audio_processor(media_data)
        
        # Send enhanced response
        echo_response = {
            'event': 'media',
            'stream_sid': self.stream_sid,
            'media': processed_audio
        }
        await self.websocket.send(json.dumps(echo_response))

Custom Dashboard Metrics

# Add custom analytics
def calculate_custom_metrics(events):
    return {
        'speech_to_silence_ratio': calculate_ratio(events),
        'interruption_frequency': count_clears(events),
        'conversation_turns': count_turns(events)
    }

🚀 Production Deployment

Docker Deployment

FROM python:3.9-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
EXPOSE 5000 5001

# Start both services
CMD ["bash", "-c", "python3 simple_server.py & python3 dashboard.py & wait"]

Cloud Deployment

# On your cloud server
git clone https://github.com/exotel/Agent-Stream-echobot.git
cd Agent-Stream-echobot
./setup.sh

# Use reverse proxy for HTTPS
nginx -t && systemctl reload nginx

Environment Variables

# Production configuration
export LOG_LEVEL=INFO
export ECHO_PORT=5000
export DASHBOARD_PORT=5001
export SILENCE_THRESHOLD=1.5
export ENABLE_DASHBOARD=true

📈 Performance Metrics

Latency Benchmarks

  • Inter-event Latency: < 50ms (excellent), < 100ms (good)
  • First Media Latency: < 200ms (excellent), < 500ms (good)
  • End-to-End Latency: Depends on call duration
  • Silence Detection: 2s threshold (configurable)

Scalability

  • Concurrent Calls: 100+ (single instance)
  • Memory Usage: ~50MB base + ~1MB per active call
  • CPU Usage: < 5% (idle), < 20% (active calls)

🧪 Testing Scenarios

Conversation Flow Testing

  1. Normal Flow: Speak → Wait → Hear response
  2. Interruption: Speak → Send CLEAR → Verify stop
  3. Multiple Turns: Alternate speaking/listening
  4. DTMF Integration: Test keypress during conversation

Load Testing

# Simulate multiple concurrent calls
for i in {1..10}; do
    python3 test_enhanced_features.py &
done

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

Development Setup

# Development mode with hot reload
export FLASK_ENV=development
python3 dashboard.py

# Test with verbose logging
export LOG_LEVEL=DEBUG
python3 simple_server.py

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🆘 Support & Documentation

🎯 Use Cases

  • 🧪 Testing: Validate Exotel voice streaming with realistic conversation flow
  • 🔍 Debugging: Analyze audio latency and protocol behavior
  • 🎓 Learning: Study conversational AI and WebSocket telephony
  • 🚀 Foundation: Starting point for building production voice bots
  • 📊 Monitoring: Real-time analytics for voice streaming performance
  • 🤖 AI Development: Test natural conversation patterns and interruption handling

🔮 Future Enhancements

  • 🧠 AI Integration: Real conversational AI responses
  • 📊 Advanced Analytics: Call quality metrics and insights
  • 🌍 Multi-language: Support for different audio formats
  • ☁️ Cloud Integration: Direct cloud deployment templates
  • 📱 Mobile Dashboard: Responsive mobile monitoring interface

🚀 Ready to experience natural voice conversations with Exotel? This enhanced echo server brings AI-like behavior to voice testing!

Made with ❤️ for the Exotel developer community | Enhanced with Conversational AI & Real-time Analytics