Make a Phone Call Using Python and Twilio Programmable Voice

Twilio Programmable Voice allows you to make and receive voice calls directly from your application. In this tutorial, I’ll show you how to use Programmable Voice to make a voice call in Python. 
Prerequisites

Python 3.6 or newer. If your operating sy…


This content originally appeared on Twilio Blog and was authored by Miguel Grinberg

Twilio Programmable Voice allows you to make and receive voice calls directly from your application. In this tutorial, I’ll show you how to use Programmable Voice to make a voice call in Python. 

Prerequisites

Set up your environment

In this section you are going to set up a brand new Python project. To keep things nicely organized, open a terminal or command prompt, find a suitable place and create a new directory where the project you are about to create will live:

mkdir phone-call
cd phone-call

Creating a virtual environment

Following Python best practices, you are now going to create a virtual environment, where you are going to install the Python dependencies needed for this project.

If you are using a Unix or MacOS system, open a terminal and enter the following commands to do the tasks described above:

python3 -m venv venv
source venv/bin/activate
pip install twilio

If you are following the tutorial on Windows, enter the following commands in a command prompt window:

python -m venv venv
venv\Scripts\activate
$ pip install twilio

The only Python package used by this project is the Twilio Python Helper library, which is used to work with phone calls.

Defining Twilio credentials

To be able to access the Twilio service, the Python application will need your Twilio account credentials to log in and authenticate. The most secure way to define these credentials is to add them as environment variables.

The information that you need is the “Account SID” and the “Auth Token”. You can find both on the dashboard of the Twilio Console:

Twilio account SID and auth token

In your terminal, define two environment variables called TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN and set them to your account credentials:

export TWILIO_ACCOUNT_SID=xxxxxxxxx
export TWILIO_AUTH_TOKEN=xxxxxxxxx

If you are following this tutorial on a Windows computer, use set instead of export to define your environment variables in the command prompt. If you want to learn more about environment variables, check out the How to set Environment Variables tutorial.

Buy a Twilio phone number

To be able to make a phone call you need to have a phone number associated with your Twilio account. Log in to the Twilio Console, select Phone Numbers, and then click on the red plus sign to buy a Twilio number. Note that if you are using a free account you will be using your trial credit for this purchase.

In the Buy a Number screen you can select your country and check Voice in the capabilities field. If you’d like to request a number from your region, you can enter your area code in the Number field.

Buy a phone number

Click the “Search” button to see what numbers are available, and then click “Buy” for the number that you like from the results. After you confirm your purchase, click the “Close” button.

Create a TwiML Bin

TwiML is Twilio’s markup language, an extension to XML that is used to provide instructions to Twilio on how certain events need to be handled. When a call is made from the Twilio phone number you purchased earlier, Twilio looks up the URL associated with your number and sends a request to that URL. If your URL responds with TwiML, Twilio will follow these instructions to handle the phone call.

You can host TwiML directly through Twilio, in a TwiML Bin. Navigate to the TwiML Bins section of the Console.

Click the “Create new TwiML Bin” button to create a new bin. This will direct you to a new page where you can configure your bin.

Screenshot of TwiML Bin configuration page

Give your bin any friendly name of your choosing, such as Python-Call. Then copy and paste the following TwiML into the TwiML field, replacing anything there already:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Hello, from Python!</Say>
</Response>

Scroll down and click the “Create” button. The page will refresh and then at the top of the new page, you will see the “SID” and “URL” values assigned to your new TwiML bin. Copy the URL to the clipboard.

TwiML bin properties

Making a phone call in Python

Fire up your text editor or IDE and create a new Python file in the phone-call directory you created at the start of the tutorial.

Enter the following code in this file:

from twilio.rest import Client

client = Client()
call = client.calls.create(
    from_='<your-twilio-phone-number>',
    to='<your-phone-number>',
    url='<your-twiml-bin-url>'
)

Make sure you update the following parts of the above code snippet:

  • Replace the value of the from_ argument with your Twilio phone number
  • Replace the value of the to argument with your personal phone number
  • Replace the value of the url argument with the URL you just copied from your TwiML bin

Be sure to use E.164 format for the phone numbers.

Head back to your terminal, make sure you have defined the environment variables with your Twilio credentials as indicated above, and then run the script as follows:

python call.py

Your personal phone will ring in just a few moments. When you answer the call, you’ll hear “Hello, from Python!”.

Feel free to go back to your TwiML bin in the Console and edit the message you want the caller to hear!

Conclusion

In this tutorial you’ve learned how to make a phone call from a Python application. But this is just the beginning. Be sure to check the TwiML reference to find out how you can do lots of other cool things with your phone calls!

Miguel Grinberg is a Python Developer for Technical Content at Twilio. Reach out to him at mgrinberg [at] twilio [dot] com if you have a cool Python project you’d like to share on this blog!


This content originally appeared on Twilio Blog and was authored by Miguel Grinberg


Print Share Comment Cite Upload Translate Updates
APA

Miguel Grinberg | Sciencx (2021-03-15T19:08:25+00:00) Make a Phone Call Using Python and Twilio Programmable Voice. Retrieved from https://www.scien.cx/2021/03/15/make-a-phone-call-using-python-and-twilio-programmable-voice/

MLA
" » Make a Phone Call Using Python and Twilio Programmable Voice." Miguel Grinberg | Sciencx - Monday March 15, 2021, https://www.scien.cx/2021/03/15/make-a-phone-call-using-python-and-twilio-programmable-voice/
HARVARD
Miguel Grinberg | Sciencx Monday March 15, 2021 » Make a Phone Call Using Python and Twilio Programmable Voice., viewed ,<https://www.scien.cx/2021/03/15/make-a-phone-call-using-python-and-twilio-programmable-voice/>
VANCOUVER
Miguel Grinberg | Sciencx - » Make a Phone Call Using Python and Twilio Programmable Voice. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/15/make-a-phone-call-using-python-and-twilio-programmable-voice/
CHICAGO
" » Make a Phone Call Using Python and Twilio Programmable Voice." Miguel Grinberg | Sciencx - Accessed . https://www.scien.cx/2021/03/15/make-a-phone-call-using-python-and-twilio-programmable-voice/
IEEE
" » Make a Phone Call Using Python and Twilio Programmable Voice." Miguel Grinberg | Sciencx [Online]. Available: https://www.scien.cx/2021/03/15/make-a-phone-call-using-python-and-twilio-programmable-voice/. [Accessed: ]
rf:citation
» Make a Phone Call Using Python and Twilio Programmable Voice | Miguel Grinberg | Sciencx | https://www.scien.cx/2021/03/15/make-a-phone-call-using-python-and-twilio-programmable-voice/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.