Skip to content

exotel/Agent-Stream-echobot

Repository files navigation

๐Ÿค– 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

About

Simple WebSocket echo server for testing Exotel voice streaming functionality

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published