How It Works
When someone calls your Twilio number:- Twilio receives the call and requests TwiML instructions from your webhook
- Server detects incoming call and creates a session automatically
- AI answers immediately with a default or custom greeting
- Natural conversation begins powered by OpenAI Realtime API
- Telegram notifications (optional) send live transcripts
src/agenticai/server/app.py:86-159 (incoming call detection)
Prerequisites
Purchase a phone number from Twilio Console:
Configure Twilio Webhook
Point your Twilio number to the Agentic AI webhook:Navigate to Phone Numbers → Manage → Active Numbers
Make sure to include
/twilio/voice at the end of your webhook URL.Test Incoming Calls
Now test by calling your Twilio number:- The AI will greet you with the default prompt
- It will listen and respond naturally
- Transcripts appear in real-time (if Telegram is configured)
Customize the Greeting
The default greeting comes fromconfig.yaml:
config.yaml
Custom Greeting Example
config.yaml
Advanced Configuration
Programmatic Greeting Override
You can detect incoming calls and customize behavior in the webhook handler. Reference implementation insrc/agenticai/server/app.py:130-145:
Caller-Specific Greetings
You can customize greetings based on the caller’s number:Time-Based Greetings
Incoming Call Flow
Here’s what happens when a call arrives:POST https://your-url.ngrok.io/twilio/voice
Form Data:
CallSid: CA1234567890abcdef
From: +15551234567
To: +15559876543
Direction: inbound
call_info = call_manager.get_pending_call_info(call_sid)
if not call_info:
# This is an incoming call!
call_id = await call_manager.register_incoming_call(...)
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Connect>
<Stream url="wss://your-url.ngrok.io/twilio/media-stream">
<Parameter name="prompt" value="..." />
<Parameter name="call_sid" value="CA123..." />
</Stream>
</Connect>
</Response>
Telegram Integration
Get notified of incoming calls and see live transcripts:Enable Telegram Notifications
config.yaml
What You’ll Receive
When an incoming call arrives:src/agenticai/telegram/direct_client.py
Status Callbacks
Track incoming call status changes:Configure Status Callback
In Twilio Console under your number’s Voice settings:- Status Callback URL:
- Status Events: Select all that apply:
initiatedringingansweredcompleted
Handle Status Updates
The server receives status updates throughout the call:src/agenticai/server/app.py:162-174
Troubleshooting
Incoming calls not connecting
Incoming calls not connecting
Symptoms: Caller hears ringing but no answerSolutions:
-
Verify webhook URL is correct:
-
Check server is running:
-
Test webhook URL accessibility:
-
Verify Twilio webhook configuration:
- Go to Twilio Console → Phone Numbers
- Check Voice & Fax settings
- Ensure URL ends with
/twilio/voice - Confirm HTTP method is POST
AI not speaking to caller
AI not speaking to caller
Symptoms: Call connects but AI is silentSolutions:
-
Check server logs for WebSocket errors:
-
Verify OpenAI API key is valid:
-
Confirm OpenAI Realtime API access:
- Realtime API may require waitlist approval
- Check your OpenAI account dashboard
-
Test with different voice:
Call drops immediately
Call drops immediately
Symptoms: Call connects then drops after 1-2 secondsSolutions:
-
WebSocket URL might be incorrect:
- Server logs should show “WebSocket connection accepted”
- Verify WSS (secure WebSocket) is used with HTTPS/ngrok
-
Check firewall/security groups:
- Port 8080 must be accessible
- WebSocket upgrades must be allowed
-
Increase timeout in uvicorn config:
No Telegram notifications
No Telegram notifications
Symptoms: Calls work but no Telegram messagesSolutions:
-
Verify Telegram is enabled:
-
Check bot token and chat ID:
-
Ensure you’ve messaged the bot:
- Open Telegram and search for your bot
- Send any message (e.g., “hi”)
- Then get your chat ID
-
Get your chat ID:
src/agenticai/telegram/direct_client.py:159-173API Endpoints
List Active Calls
See all incoming and outbound calls:End an Active Call
Programmatically end an incoming call:src/agenticai/server/app.py:236-264
Next Steps
Making Calls
Initiate outbound calls
Telegram Integration
Set up live transcripts
Service Management
Run as a daemon
Scheduling
Automate recurring calls