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.
Configure Twilio URLs
-
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>/. -
Update streams.xml: Copy the ngrok URL and update templates/streams.xml with
wss://<ngrok_url>/ws.
Running the Application
Using Python
- Run the FastAPI application:
python server.py
Using Docker
-
Build the Docker image:
docker build -t twilio-chatbot . -
Run the Docker container:
docker run -it --rm -p 8765:8765 twilio-chatbot
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.