created twilio_websocket_service.py, TwilioFrameSerializer.py moved pcm_16000_to_ulaw_8000 and ulaw_8000_to_pcm_16000 to src/pipecat/utils/audio.py fixed callback on disconnect
Twilio Chatbot
This project is a FastAPI-based chatbot that integrates with Twilio to handle WebSocket connections and provide real-time communication. The project includes endpoints for starting a call and handling WebSocket connections.
Table of Contents
Features
- FastAPI: A modern, fast (high-performance), web framework for building APIs with Python 3.6+.
- WebSocket Support: Real-time communication using WebSockets.
- CORS Middleware: Allowing cross-origin requests for testing.
- Dockerized: Easily deployable using Docker.
Requirements
- Python 3.10
- Docker (for containerized deployment)
- ngrok (for tunneling)
- Twilio Account
Installation
-
Set up a virtual environment (optional but recommended):
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate` -
Install dependencies:
pip install -r requirements.txt -
Create .env: create .env based on .env.example
-
Install ngrok: Follow the instructions on the ngrok website to download and install ngrok.
Running the Application
Using Python
-
Run the FastAPI application:
python server.py -
Start ngrok: In a new terminal, start ngrok to tunnel the local server:
ngrok http 8765 -
Update the Twilio Webhook: Copy the ngrok URL and update your Twilio phone number webhook URL to
http://<ngrok_url>/start_call. -
Update the streams.xml: Copy the ngrok URL and update your .xml URL to
wss://<ngrok_url>/ws.
Using Docker
-
Build the Docker image:
docker build -t twilio-chatbot . -
Run the Docker container:
docker build -t twilio-chatbot . docker run -it --rm -p 8765:8765 twilio-chatbot -
Start ngrok: In a new terminal, start ngrok to tunnel the local server:
ngrok http 8765 -
Update the Twilio Webhook: Copy the ngrok URL and update your Twilio phone number webhook URL to
http://<ngrok_url>/start_call. -
Update the streams.xml: Copy the ngrok URL and update your .xml URL to
wss://<ngrok_url>/ws.
Usage
To start a call, simply make a call to your Twilio phone number. The webhook URL will direct the call to your FastAPI application, which will handle it accordingly.