This project is a Pipecat-based chatbot that integrates with Twilio to handle inbound phone calls via WebSocket connections and provide real-time voice conversations.
When someone calls your Twilio number:
- Twilio sends WebSocket messages: Twilio processes the associated TwiML Bin and starts a WebSocket stream to your bot (local or Pipecat Cloud)
- Parse the WebSocket messages: Your bot parses the WebSocket connection messages to set up the corresponding Pipecat transport
- (Optional) Look up the caller: Optionally, look up the caller using Twilio's REST API to retrieve custom information about the call and personalize your bot's behavior
- Bot starts responding: Once the pipeline is started, your bot will initiate the conversation
- A Twilio account with:
- Account SID and Auth Token
- A purchased phone number that supports voice calls
- OPENAI API key for the LLM inference
- Deepgram API key for speech-to-text and text-to-speech is required
- Python 3.10+
uvpackage manager- ngrok (for local development)
-
Set up a virtual environment and install dependencies:
cd vitlycare-inbound uv sync -
Create an .env file and add API keys:
cp env.example .env
-
Start ngrok: In a new terminal, start ngrok to tunnel the local server:
ngrok http 7860
Tip: Use the
--subdomainflag for a reusable ngrok URL. -
Create a TwiML Bin:
-
Go to your Twilio Console: https://console.twilio.com/
-
Navigate to TwiML Bins > My TwiML Bins
-
Click the
+to create a new TwiML Bin -
Name your bin and add the TwiML containing your ngrok URL:
<?xml version="1.0" encoding="UTF-8"?> <Response> <Connect> <Stream url="wss://your-url.ngrok.io/ws" /> </Connect> </Response>
-
Click "Save"
-
-
Assign the TwiML Bin to your number:
- Navigate to Phone Numbers > Manage > Active numbers
- Click on your Twilio phone number
- In the "Voice Configuration" section:
- Set "A call comes in" to "TwiML Bin"
- Select the name of your TwiML Bin from step 2
- Click "Save configuration"
Run your bot by passing in the twilio command line arg
uv run bot.py --transport twilioNote: This bot uses Pipecat's development runner, which runs a FastAPI server that handles and routes incoming WebSocket messages to your bot.
Place a call to the number associated with your bot. The bot will answer and start the conversation.
The bot.py example file is configured to look up the caller's phone number by calling Twilio's REST API using the Call SID. With this information, you can:
- Perform a lookup in your own database to retrieve customer information
- Personalize the bot's greeting and behavior based on the caller