Incoming Call Webhook
Handles incoming call events from Telnyx telephony service
Endpoint
Description
This webhook endpoint receives call lifecycle events from Telnyx including call initiation, answering, and hangup. The system processes these events to manage call state, trigger audio streaming, perform real-time analysis, and create dispatch queue items.Request
Headers
Must be
application/jsonRequest Body
Telnyx sends events with the following structure:Container for event data
Type of call event. Values:
call.initiated, call.answered, call.hangupEvent-specific payload with call details
Unique identifier for the call
Control ID used for call actions (answer, stream, hangup)
Caller’s phone number
Destination phone number
ISO 8601 timestamp when call started
ISO 8601 timestamp when call ended (only in
call.hangup)Example: Call Initiated
Example: Call Hangup
Response
Always returns
"ok" for successful webhook processingSuccess Response (200 OK)
Event Processing
call.initiated
When a call is initiated:- Maps
call_control_idtocall_session_idfor WebSocket routing - Creates entry in
LIVE_CALLSdictionary with initial state - Triggers background task to answer call and start audio streaming
- Sends TTS greeting: “This is Dispatch AI. I’m listening. Please describe your emergency.”
call.answered
When a call is answered:- Updates call status to
ACTIVEinLIVE_CALLS - Creates entry in
LIVE_QUEUEfor real-time monitoring - Initializes risk assessment fields
call.hangup
When a call ends, the system performs comprehensive analysis:- Transcribes full audio using Deepgram batch API
- Analyzes emotional state using LLM
- Classifies service type (EMS/FIRE/POLICE/OTHER) and extracts tags
- Computes risk level based on:
- Audio distress score
- Emotional analysis
- Semantic tags (life-threatening conditions)
- Generates call summary
- Creates queue item with status
OPEN - Saves complete call record
- Removes from
LIVE_QUEUE(streaming ended)
Call Record Structure
When a call ends, the system creates a comprehensive record:Error Responses
HTTP status code
Error message
Error Response (400 Bad Request)
call_control_id or call_session_id is missing from a call.initiated event.
Implementation Notes
- Location:
app/main.py:353-626 - Uses background tasks for async operations (answering calls, starting streams)
- Masks phone numbers (shows only last 4 digits)
- Stores WAV files in
data/calls/directory - Uses in-memory storage (dev mode) or database (prod mode)
Risk Level Computation
The system computes risk levels using:- Audio Analysis: RMS-based distress scoring from voice characteristics
- Emotional State: LLM-based emotion classification
- Semantic Tags: Critical conditions override risk score
ACTIVE_SHOOTER,GUNSHOT,STABBINGNOT_BREATHING,CARDIAC_ARREST,UNCONSCIOUSSEVERE_BLEEDING,OVERDOSE,SUICIDE_ATTEMPT,EXPLOSION
TRAUMA,VIOLENCE,ASSAULT,DOMESTIC_VIOLENCEFIRE,SMOKE,GAS_LEAKCARDIAC_EVENT,STROKE,SEIZURE,BREATHING_DIFFICULTY,CHEST_PAINVEHICLE_ACCIDENT