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. Show PrerequisitesSet up your environmentIn 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:
Creating a virtual environmentFollowing 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:
If you are following the tutorial on Windows, enter the following commands in a command prompt window:
The only Python package used by this project is the Twilio Python Helper library, which is used to work with phone calls. Defining Twilio credentialsTo 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: In your terminal, define two environment variables called
If you are following this tutorial on a Windows computer, use Buy a Twilio phone numberTo 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. 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 BinTwiML 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. 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:
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. Making a phone call in PythonFire 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:
Make sure you update the following parts of the above code snippet: 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: 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! ConclusionIn 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! Related Posts
Record Page Visits to your Flask Website with Twilio Segment Sep 28, 2022 Learn how to create a server-side web analytics solution for Flask using Twilio Segment. How to Build a Motion Detection System Using Raspberry Pi and Twilio WhatsApp API Sep 13, 2022 Learn how to use a Raspberry Pi, an IR motion sensor, and WhatsApp to build a system that notifies you when movement is detected in your room or house. How to Automate WhatsApp Messages using Bottle and Twilio Sep 06, 2022 How to Automate WhatsApp Messages using the Python Bottle Framework and Twilio Sandbox Schedule a NASA Astronomy Picture of the Day SMS with Python, Django, and Twilio Sep 06, 2022 Build a Django application that sends out a daily SMS containing the NASA APOD Photo of the Day. Build a Digital Sticky Notes App with Flask and Svelte Sep 02, 2022 Learn how to build a digital sticky notes app with Python and JavaScript. Keep track of your to-dos, study notes, and more. Summarize Text from Images Using AI, Python, and Twilio Sep 01, 2022 Build an app in Python that performs text recognition on photos, summarizes that text, and then sends you a summary via SMS How do you make free calls with Python?Making a phone call in Python
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.
Can you call with Python?With just a few lines of Python code plus a web application programming interface we can make and receive phone calls from any application. Our example calls will say a snippet of text and put all incoming callers into a recorded conference call.
How do you receive phone calls in Python?How to Receive a Phone Call in Python with Flask and Plivo. Prerequisites. ... . Create a Flask application to receive incoming calls and play a TTS message. ... . Test the code locally. ... . Expose the local server to the internet using ngrok. ... . Connect the Flask application to a Plivo number. ... . Test the application.. |