This is a PUBLIC repository. Never include PII (personally identifiable information) in any files:
- No real IP addresses, phone numbers, or domain names in examples
- No API keys, tokens, or credentials
- Use example/placeholder values only (e.g.,
+15551234567,your-domain.com, → reference to command output)
This is a personal voice assistant that bridges Twilio Voice and OpenAI's Realtime API for phone-based AI conversations. The architecture uses a FastAPI server as a WebSocket relay between Twilio's Media Streams and OpenAI's Realtime API.
Run the application:
make dev
# or directly: uv run python main.pyStart cloudflared tunnel:
make tunnel-quick # Quick temporary tunnel with random URL
make tunnel # Named tunnel with stable domain (requires setup)Docker:
docker compose up # Run without tunnel
docker compose --profile dev up # Run with cloudflared tunnelFly.io deployment:
fly deploy # Deploy to Fly.io
fly logs # View logs
fly status # Get app URL and status-
Layer 1: Twilio Function Allowlist (
twilio/allowlist-function.js)- Runs on Twilio's serverless infrastructure
- Filters calls by phone number before they reach your server
- Configured via Twilio Console with
ALLOWED_NUMBERSandWEBHOOK_URLenvironment variables - Only allowed numbers are redirected to the FastAPI server
-
Layer 2: Twilio Signature Validation (
main.py:67-79)- Validates webhook requests using HMAC-SHA1 with
TWILIO_AUTH_TOKEN - Ensures requests are authentic and untampered
- Validates webhook requests using HMAC-SHA1 with
-
Layer 3: WebSocket Token Authentication (
main.py:50-157)- Single-use tokens with 60-second expiration
- Generated in
/incoming-callendpoint, validated in/media-streamWebSocket - Prevents unauthorized WebSocket connections
- Caller dials Twilio number
- Twilio sends webhook to Twilio Function (Layer 1)
- Function checks allowlist, redirects to FastAPI
/incoming-callif approved - FastAPI validates signature (Layer 2), generates WebSocket token
- Returns TwiML with WebSocket URL + token
- Twilio connects to
/media-streamWebSocket with token - FastAPI validates token (Layer 3), removes it (single-use)
- Establishes OpenAI WebSocket connection
- Bidirectional audio streaming begins
Interruption Handling (main.py:239-268):
- Listens for
input_audio_buffer.speech_startedevents - Calculates elapsed audio time using Twilio's media timestamps
- Sends
conversation.item.truncateto OpenAI to cut off AI mid-sentence - Sends
clearevent to Twilio to stop playback - Enables natural conversation interruptions
Audio Relay (main.py:174-238):
receive_from_twilio(): Forwards Twilio audio chunks to OpenAI asinput_audio_buffer.appendsend_to_twilio(): Forwards OpenAIresponse.output_audio.deltaevents to Twilio- Both run concurrently using
asyncio.gather()
Mark Queue (main.py:171, 269-277):
- Tracks audio chunks sent to Twilio for synchronization
- Used to calculate precise truncation points during interruptions
Environment variables are loaded from .env (not committed):
OPENAI_API_KEY- Required for Realtime API accessTWILIO_AUTH_TOKEN- Required for signature validationASSISTANT_INSTRUCTIONS- Assistant personality, behavior, and tool usage instructionsZAPIER_MCP_URL- URL of the Zapier MCP server (e.g.,https://mcp.zapier.com/api/mcp/mcp)ZAPIER_MCP_PASSWORD- API key for Zapier MCP authentication (base64 encoded)VOICE- OpenAI voice (alloy, shimmer, nova, etc.)PORT- Server port (default: 5050)TEMPERATURE- AI temperature (default: 0.8)
Note: WEBHOOK_URL and ALLOWED_NUMBERS are only used in the Twilio Function, not in the FastAPI application.
To enable AI speaking first when a call connects, uncomment line 325 in main.py:
await send_initial_conversation_item(openai_ws)This sends an initial user message prompting the AI to greet the caller.
Both Twilio and OpenAI use μ-law (PCMU) encoding at 8kHz for compatibility without transcoding.
The main FastAPI application is deployed to Fly.io with a blue-green deployment strategy for zero-downtime updates:
make deploy # Deploys both MCP and main app
fly logs # View application logs
fly status # Check deployment statusImportant Fly.io Configuration:
- Uses
blue-greendeployment strategy (fly.toml:12) - Health checks ensure new machines are ready before traffic switches (
fly.toml:22-27) - Secrets are managed via
fly secrets set(never committed to git)
The assistant integrates with Zapier via an MCP (Model Context Protocol) server. Zapier connects to multiple services including:
- Todoist: Task management and reminders
- Gmail: Email search and management
Zapier MCP Setup:
- Get your API key from: https://zapier.com/app/developer/mcp
- Set
ZAPIER_MCP_URL=https://mcp.zapier.com/api/mcp/mcpin.env - Set
ZAPIER_MCP_PASSWORD=your_zapier_api_key_base64in.env
Connection Flow:
- OpenAI Realtime API session initializes with MCP tools configured (
main.py:342-351) - Main app connects to Zapier MCP server via HTTPS
- API key authentication is handled by MCP protocol
- Zapier routes tool calls to configured integrations (Todoist, Gmail, etc.)
Common Issues:
- 401 Unauthorized: Missing or invalid API key in
ZAPIER_MCP_PASSWORD - Tools not being called: Check logs for
allowed_toolsfield in session.updated event. IfNone, the MCP server may not be exposing tools properly to the Realtime API.
The application includes enhanced logging to debug MCP tool discovery:
-
Check session configuration: After deployment, check logs for:
Registered tools count: X Tool: mcp - zapier - allowed_tools: [...] -
Look for tool call events: The logs will show if OpenAI is attempting to call tools:
response.function_call_arguments.delta- Function call in progressresponse.function_call_arguments.done- Function call completed- If you see
output_textwith JSON instead, the AI is simulating tool calls rather than making real ones
Known Limitation: OpenAI's Realtime API MCP support may be experimental. If tools aren't being called properly, consider using explicit function definitions instead of MCP (see Alternative Approaches below).
make dev # Run locally
make deploy # Deploy main app to Fly.io
make tunnel-quick # Start development tunnelIf MCP integration isn't working with the Realtime API, you can define functions explicitly and implement handlers to forward calls to Zapier or directly to service APIs. This gives you full control over the integration and ensures the AI can actually invoke the tools.