Overview
DispatchAI uses Telnyx for telephony infrastructure. The integration handles:- Incoming call webhooks - Call lifecycle events (initiated, answered, hangup)
- Audio streaming - Real-time PCM audio via WebSocket
- Call control - Answer, TTS greeting, and streaming commands
Telnyx Setup
Create a Telnyx account
Sign up at portal.telnyx.com and complete verification.
Purchase a phone number
- Navigate to Numbers → Buy Numbers
- Search for a number in your desired area code
- Select a number and complete purchase
Create a Call Control Application
- Go to Call Control → Applications
- Click Add New Application
- Configure:
- Name: DispatchAI
- Webhook URL:
https://<your-ngrok-url>/api/v1/call/incoming - Webhook HTTP Method: POST
- Send Webhook: Enable
- Save the application
In development, get your ngrok URL by running
./scripts/dev_start.sh and copying the printed URL.Assign number to application
- Go to Numbers → My Numbers
- Click your purchased number
- Under Call Settings, select your DispatchAI application
- Save changes
Get API credentials
- Navigate to API Keys under Settings
- Create a new API key with Call Control permissions
- Copy the API key to your
.env:
.env
Webhook Events
DispatchAI handles three primary webhook events:1. call.initiated
Triggered when: An incoming call is received Handler:app/main.py:363-403
Actions:
- Maps
call_control_idtocall_session_idfor tracking - Initiates call answering + audio streaming (background task)
- Seeds
LIVE_CALLSstate with initial metadata
2. call.answered
Triggered when: The call is answered (after TTS greeting) Handler:app/main.py:405-435
Actions:
- Updates call status to
ACTIVE - Seeds
LIVE_QUEUEfor dispatcher dashboard - Begins live signal tracking
3. call.hangup
Triggered when: The call ends Handler:app/main.py:438-625
Actions:
- Transcribes recorded audio via Deepgram batch API
- Analyzes emotion and distress
- Classifies service category (EMS/FIRE/POLICE/OTHER)
- Generates summary
- Computes risk score and priority ranking
- Creates
CallPacketand adds to dispatch queue - Removes from
LIVE_QUEUEandLIVE_CALLS
Call Control Flow
When a call is initiated, DispatchAI executes the following sequence:Answer and Stream
Function:_answer_and_stream() in app/main.py:175-214
Play TTS greeting
Speak greeting to caller:
overlay: false ensures the greeting completes before audio streaming starts.WebSocket Audio Handler
The/ws endpoint receives audio frames from Telnyx:
Implementation: app/api/ws/handler.py:344-549
Connection Lifecycle
Audio Processing Pipeline
- Receive - Base64-encoded audio chunks (20ms each)
- Decode - Convert µ-law to PCM16 little-endian
- Stream - Feed to Deepgram WebSocket for live transcription
- Analyze - Compute RMS for voice activity + distress proxy
- Buffer - Save to WAV file for batch transcription
- Finalize - On hangup, flush STT and save final transcript
Testing Your Integration
Update Telnyx webhook URL
In Telnyx Portal:
- Go to your Call Control Application
- Update Webhook URL to
https://abc123.ngrok.io/api/v1/call/incoming - Save
Call your Telnyx number
Use your mobile phone to call the number you configured.Expected behavior:
- Call connects
- TTS greeting plays: “This is Dispatch AI…”
- Speak your emergency scenario
- Hang up
Check the live dashboard
Open
http://localhost:8000/debug/live_calls/ in your browser.You should see:- Call status (ACTIVE during call, ENDED after)
- Live transcript updates
- Real-time distress score
- Risk level and emotion
Production Deployment
Using a Fixed Domain
Replace ngrok with a production domain:.env
Webhook Security
Validate webhook signatures in production:Troubleshooting
Webhook Not Received
Check ngrok tunnel status
Check ngrok tunnel status
Visit
http://127.0.0.1:4040 to see incoming requests.If empty, your ngrok URL may have changed - update Telnyx webhook URL.Verify Telnyx configuration
Verify Telnyx configuration
In Telnyx Portal:
- Check Webhook URL is correct
- Ensure Send Webhook is enabled
- Review Webhook Logs for delivery failures
Test webhook manually
Test webhook manually
WebSocket Connection Fails
Check WS_PUBLIC_URL
Check WS_PUBLIC_URL
Ensure it points to your WebSocket endpoint:
Verify WebSocket route
Verify WebSocket route
Test locally:
Check firewall/proxy settings
Check firewall/proxy settings
WebSocket requires:
- HTTP/1.1 protocol
Upgrade: websocketheader- Long timeout (calls can last minutes)
No Audio Received
Verify streaming_start response
Verify streaming_start response
Check server logs for:If not 200, check call_control_id validity.
Check audio format
Check audio format
Ensure Telnyx is sending correct format:
Test with recorded call
Test with recorded call
Replay a saved WAV file:
Next Steps
Agent Configuration
Customize NLP agents and classification rules
API Reference
Explore REST endpoints and WebSocket protocol