This Python Flask app is based on the code from Twilio's media-streams repo, specifically the Python basic example.
There are 2 main differences with this code compared to Twilio's:
- Twilio's example is uni-directional vs. this example is bi-directional
- This example is based on the more modern flask-sock library in comparison to flask-sockets which is deprecated.
- Create a venv by running
python3 -m venv venv && source venv/bin/activate
- Run
pip install -r requirements.txt
- Run the app by running
python app.py
- By default, the app will be bound to localhost on port 5000
- To expose the app to the internet, you can use ngrok by running the command
ngrok http 5000
- In the Twilio Console, use the ngrok URL as your Voice URL, appending
/twiml
for the phone number you would like to use. For example - https://123456789.ngrok.io/twiml - You will also need to replace the WSS URL in the streams.xml file, for example - wss://123456789.ngrok.io/
Included with the app (app.py) is a mulaw encoded raw (headerless) audio file that has been encoded using base64. When the call first connects, the app will recieve 100 chunks of audio from the caller, once it reached 100 chunks, the app will send audio back to Twilio. You will hear the full audio - "When you use Twilio and media stream you can achieve great things"
app_clear.py extend the above app by exploring how to sending clear message to interrupt the audio that has been sent. You will hear partial audio - "When you use Twilio and media stream". Run the app by running python app_clear.py