How do i make a free call in python?

    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 system does not provide a Python interpreter, you can go to python.org to download an installer.
    • A Twilio account. If you are new to Twilio click here to create a free account now and receive $10 credit when you upgrade to a paid account. You can review the features and limitations of a free Twilio account.
    • A phone that can make phone calls, to test the project.

    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:

    How do i make a free call in python?

    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.

    How do i make a free call in python?

    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.

    How do i make a free call in python?

    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.

    How do i make a free call in python?

    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:

    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!


    Related Posts

    How do i make a free call in python?

    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 do i make a free call in python?

    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 do i make a free call in python?

    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

    How do i make a free call in python?

    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.

    How do i make a free call in python?

    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.

    How do i make a free call in python?

    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..