Phone Bot Twilio
Learn how to connect your Pipecat bot to a phone number so users can call and have voice conversations. This example shows the complete setup for telephone-based AI interactions using Twilio's telephony services. At the end, you'll be able to talk to your bot on the phone.
Prerequisites
- Python 3.10+
- ngrok (for tunneling)
- Twilio Account and phone number
- AI Service API keys for: Deepgram, OpenAI, and Cartesia
Setup
This example requires running both a server and ngrok tunnel in two separate terminal windows.
Terminal 1: Start ngrok and Configure Twilio
-
Start ngrok: In a new terminal, start ngrok to tunnel the local server:
ngrok http 7860Want a fixed ngrok URL? Use the
--subdomainflag:ngrok http --subdomain=your_ngrok_name 7860 -
Update the Twilio Webhook:
- Go to your Twilio phone number's configuration page
- Under "Voice Configuration", in the "A call comes in" section:
- Select "Webhook" from the dropdown
- Enter your ngrok URL:
https://your-ngrok-url.ngrok.io - Ensure "HTTP POST" is selected
- Click Save at the bottom of the page
-
Configure streams.xml:
- Copy the template file to create your local version:
cp templates/streams.xml.template templates/streams.xml - In
templates/streams.xml, replace<your_server_url>with your ngrok URL (withouthttps://) - The final URL should look like:
wss://abc123.ngrok.io/ws
- Copy the template file to create your local version:
Terminal 2: Server Setup
-
Set up a virtual environment
From the
examples/phone-bot-twiliodirectory, run:python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activateUsing
uv? Create your venv using:uv venv && source .venv/bin/activate. -
Install dependencies
pip install -r requirements.txtUsing
uv? Install requirements using:uv pip install -r requirements.txt. -
Configure environment variables
Create a
.envfile:cp env.example .envThen, add your API keys:
DEEPGRAM_API_KEY=your_deepgram_api_key OPENAI_API_KEY=your_openai_api_key CARTESIA_API_KEY=your_cartesia_api_keyOptional: Add your
TWILIO_ACCOUNT_SIDandTWILIO_AUTH_TOKENto enable auto-hangup. -
Run the Application
python server.py
Test Your Phone Bot
Call your Twilio phone number to start talking with your AI bot! 🚀
💡 Tip: Check your server terminal for debug logs showing Pipecat's internal workings.
Troubleshooting
- Call doesn't connect: Verify your ngrok URL is correctly set in both Twilio webhook and
streams.xml - No audio or bot doesn't respond: Check that all API keys are correctly set in your
.envfile - Webhook errors: Ensure your server is running and ngrok tunnel is active before making calls
- ngrok tunnel issues: Free ngrok URLs change each restart - remember to update both Twilio and
streams.xml
Understanding the Call Flow
- Incoming Call: User dials your Twilio number
- Webhook: Twilio sends call data to your ngrok URL
- WebSocket: Your server establishes real-time audio connection via Websocket and exchanges Media Streams with Twilio
- Processing: Audio flows through your Pipecat Pipeline
- Response: Synthesized speech streams back to caller
Next Steps
- Deploy to production: Replace ngrok with a proper server deployment
- Explore other telephony providers: Try Telnyx or Plivo examples
- Advanced telephony features: Check out pipecat-examples for call recording, transfer, and more
- Join Discord: Connect with other developers on Discord