Want to programmatically use the Twilio voicemail feature? While I was building a burner phone with Twilio voicemail, I had to look up so many tutorials to find out how to use Twilio’s API programmatically, so here’s a comprehensive guide on how to use Python and Twilio to set up voicemail. We’ll go over how to:
- Request a phone number from Twilio
- Build a custom webhook with Python Flask, ngrok, and Twilio to set up voicemail
- Change a phone number’s webhook to your custom one
- Download your Twilio voicemail recordings
- Delete your Twilio phone number
Okay before we do any of this, you will need a Twilio account to follow along. You can find your account sid and auth token where I’ve indicated in the picture.
Copy your credentials and either save them in your environment variables or save them in a configure.py file in the folder you’re working in.
Request a phone number from Twilio
Now that we have a Twilio account, the first thing we’ll want to do to set up our voicemail is get a phone number. We’ll create a Python script that will request 20 local numbers with whatever area code you’d like, and allow us to pick one of them.
When we’re done, our terminal output should look like:
Build a custom webhook with Python Flask, ngrok, and Twilio to set up voicemail
This is the part where we use Twilio to set up voicemail. We’ll build a Flask application that will use Twilio to go straight to voicemail and record phone calls. We’ll create a custom Twilio voicemail endpoint that will tell the caller to record their call and then record the call with a 10 second time out. We’ll download and run ngrok on our port to expose it to the internet. Note that we need two open terminals to do this.
Run this code in the first terminal
We should see an output like this:
After our flask app is up and running in our terminal, we also need to download and run ngrok. You can download ngrok here. After downloading ngrok and copying it into our working folder, we’ll open a second terminal and run
Note that the “5000” can be replaced with whatever port you’re running your Python Flask application on. As you can see above, ours is running on port 5000. Ngrok should expose and return an endpoint we can hit. We’ll need to keep track of the https forwarding link, that is going to be our updated webhook URL for Twilio to hit when we call.
Change a phone number’s webhook to your custom one
Alright, now that we have an app that will record phone calls, let’s update our webhook on Twilio via their Python REST API. This will make it so that when we call the number, Twilio will send us straight to voicemail.
When we run this, we should get this output in the terminal. Notice that I added a “/voice” to the end of the URL provided by ngrok, that is because I defined “/voice” as the endpoint in our Flask application above.
Download your Twilio voicemail recordings
To test out our voicemail, make some calls. If everything is set up correctly, you should hear a female voice say “Please leave a message”. For the purpose of this tutorial, I made 3 phone calls and left 3 messages.
The message I left are:
“This is a third and final recording that I’m going to use for testing transcription services. So yeah, I should have been a cowboy”
“This is a test recording for transcriptions. Sally sells seashells down by the sea shore”
“A B C D E F G This is a test call for recording transcriptions with Twilio and AssemblyAI”
Now let’s check out our recordings on Twilio and download them.
When we’re done, the output in the terminal should look like:
I ran this three times to pull down all three recordings. You can alternatively execute the code below to download all of them at once.
Delete your Twilio phone number
One last thing that I wanted to do after setting up my burner voicemail with Twilio was to be able to delete my number programmatically.
It should run like this:
In this tutorial we covered how to use Twilio’s API to get and download voicemails. Here is a full tutorial on how to build a burner phone to get voicemail transcripts. For more information about AssemblyAI, check out our blog for tutorials and updates. Follow us on Twitter @assemblyai or the author, @yujian_tang, to stay updated.