Prerequisites
Before you begin, make sure you have:Python 3.12+
Required for async features and modern syntax
MongoDB
Local or cloud instance for data persistence
OpenAI API Key
With access to Realtime API capabilities
Webhook Secret
From OpenAI for webhook verification
This guide uses
uv for package management. You can also use pip or poetry if you prefer.Installation
Install Dependencies
Install the project and all required packages:This installs the dependencies from
pyproject.toml:- FastAPI - Web framework
- Pydantic - Data validation
- httpx - Async HTTP client
- pymongo - MongoDB driver
- openai - OpenAI SDK
- websockets - WebSocket client
- phonenumbers - Phone number parsing
Configuration
Configure MongoDB
Ensure MongoDB is running and accessible:The system will create the following collections automatically:
call_registries- Call metadata and transcriptstenant_configs- Per-tenant configurationsprompts- System and greeting prompts
Running the System
Sonore Phone Agent consists of two FastAPI applications that work together:Start the Call Handler
This service handles incoming webhook events and manages active call sessions:You should see:
Start the Post-Call Processor
In a new terminal, start the post-call processing service:This service handles:
- Call transcript processing
- Summary generation
- Analytics and metrics
Testing Webhook Flow
To test the complete call flow, you’ll need to expose your local server to the internet for OpenAI webhooks:Set Up Tunnel
Use ngrok or a similar tool to expose your local server:Copy the HTTPS URL (e.g.,
https://abc123.ngrok.io)Configure OpenAI Webhook
In your OpenAI dashboard, set the webhook URL to:The system will verify the webhook signature on every request:
Make a Test Call
Call the phone number associated with your tenant configuration. The system will:
- Receive webhook - OpenAI sends
realtime.call.incomingevent - Resolve tenant - Match dialed number to tenant
- Check capacity - Verify concurrent call limits
- Fetch instructions - Load prompts from MongoDB
- Accept call - Send acceptance to OpenAI
- Start session - Create WebSocket connection
- Handle conversation - Process audio and execute tools
- Post-process - Generate transcript and summary
Watch the terminal logs to see each step in real-time. The structured logging shows the complete call lifecycle.
Understanding the Call Flow
Here’s what happens during a typical call:Monitoring Active Calls
The system provides real-time visibility into call sessions:Next Steps
Architecture Deep Dive
Learn how the components work together
Custom Tools
Build custom tools for your phone agent
Prompt Engineering
Optimize your agent’s conversation flow
Production Deployment
Deploy with Docker and best practices
Troubleshooting
Webhook verification failed
Webhook verification failed
Problem:
401 Unauthorized - Invalid signatureSolution: Verify your OPENAI_WEBHOOK_SECRET in .env matches the secret in your OpenAI dashboard.MongoDB connection error
MongoDB connection error
Problem:
pymongo.errors.ServerSelectionTimeoutErrorSolution:- Check MongoDB is running:
docker psormongosh - Verify
MONGODB_URIin.env - Ensure no firewall blocking port 27017
Call rejected for capacity
Call rejected for capacity
Problem: Calls are rejected with
capacity reasonSolution: Increase limits in .env:Post-call processing fails
Post-call processing fails
Problem: Errors in post-call webhookSolution:
- Verify post-call service is running on port 8001
- Check
POST_CALL_URIin.env - Review logs in post-call terminal