Get Started with Voice Notifications Using Python

    Overview

    This guide shows how to send audio notifications using voice calls. You can play recorded audio when the call recipient answers or use text-to-speech, as we show here, combining static text with dynamic information that Plivo gets from a variable.

    You can use voice notification for use cases such as:

    • Order notification
    • Booking status
    • Delivery status
    • Flight cancellation/rescheduling
    • Two-factor authentication/one-time password
    • New offer notification
    • Account balance notification

    Implement voice notification either by using our PHLO visual workflow builder or our APIs and XML documents. Follow the instructions in one of the tabs below.

    To implement voice notifications use-case, you can create and deploy a PHLO with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, you only pay for calls you make/receive, and building with PHLO is free.

    Outline

    Implementation

    In this section, we will guide you to create a PHLO to implement voice notifications use-case.

    Prerequisites

    1. Create a Plivo Account(if you don’t have one already): You can sign up with your work email address and complete the phone verification step using your mobile number.
    2. Set up your Dotnet dev environment: To set up your dev environment in your .NET(C#) programming language, please refer to the instructions available in the Set up Your Dotnet Dev Environment section.
    3. Buy a Plivo number(optional): You must have a voice-enabled Plivo phone number if you are willing to receive incoming calls. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Create the PHLO

    • On the top navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.

    • Click CREATE NEW PHLO to build a new PHLO.

    • On the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node. Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the Start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the API Request trigger state.
    • Click the Start node to open the Configurations tab.
    • Then, enter the information to retrieve from the HTTP Request payload. For example, this could be a variable for the list of numbers to call.

    • From the list of components on the left-hand side, drag and drop the Initiate Call component onto the canvas. This will add a Initiate call node onto the canvas.

    • Connect the Start node with the Initiate Call node, using the API Request trigger state.

    • The values for the From and To numbers are picked from the HTTP Request Payload. Make sure to clearly define the variables in the Start node.

    • Next, to configure the From and To numbers in the Initiate Call node, use the variables set in the Start node. Note:

      • Enter two curly brackets to view all available variables.
      • The values for the From and To numbers are picked from the HTTP Request Payload. Make sure to clearly define the variables in the Start node.

    • Similarly, create a node for the Play Audio component and connect it to the Initiate Call node using the Answered trigger state.

    • Next, configure the Play Audio node to play a specific message to the user.

      Note:

      • The audio playback can either be static or dynamic. In this example, we will use a combination of a static message along with some dynamic message.
      • On the Play Audio Configurations tab, enter a static message (For example, Your Flight status is:) in the Speak Text field, followed by a variable to include the dynamic text.
    Note:
    • You can choose a variable by entering two curly brackets in the Speak Text field.
    • Make sure you validate the configuration for each node.
    • After you complete the configurations, click Save. You can trigger your PHLO using the PHLO URL to test it out. Your complete PHLO will look like this:

      Voice Notification

    Set up Your Python Dev Environment

    You must set up and install Python and Plivo’s Python SDK to implement voice notifications use-case. Here’s how.

    Install Python

    Operating SystemInstructions
    macOS & LinuxYou would already have Python installed, you can check this by running the command python --version in the terminal
    WindowsTo install Python on Windows you can follow the instructions from here.

    Install Plivo Python Package

    • Create a project directory, run the following command:

      $ mkdir mypythonapp
      
    • Change the directory to our project directory in the command line:

      $ cd mypythonapp
      
    • Install the SDK using pip

      pip install plivo
      
    • Alternatively, you can download the source code from this repo and run

      python setup.py install
      

    We recommend that you use virtualenv to manage and segregate your Python environments, instead of using sudo with your commands and overwriting dependencies.

    Trigger the PHLO

    Once you have created and setup your Python dev envrironment, you can go to your Plivo Consolse and copy the PHLO_ID. You can integrate a PHLO into your application workflow by making an API request to trigger the PHLO with the required payload.

    With Static Payload

    You can choose to either configure the mandatory params required for a PHLO while creating the PHLO itself or, you can pass the params as payload while triggering the PHLO from your app.

    Note: When you configure the params while creating the PHLO, the values are hardcoded, and they act as static payload.

    With Static Payload

    Code

    Now, create a file called trigger_phlo.py and paste the below code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    import plivo
    
    auth_id = 'Your AUTH ID'
    auth_token = 'Your AUTH Token'
    phlo_id = 'Your PHLO ID' # https://console.plivo.com/phlo/list/
    phlo_client = plivo.phlo.RestClient(auth_id=auth_id, auth_token=auth_token)
    phlo = phlo_client.phlo.get(phlo_id)
    response = phlo.run()
    print str(response)
    

    With Dynamic Payload

    To use dynamic values for the parameters, you can use the liquid templating params while creating the PHLO and pass the values while triggering the PHLO.

    With Dynamic Payload

    Code

    Now, create a file called trigger_phlo.py and paste the below code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    import plivo
    
    auth_id = 'Your AUTH ID'
    auth_token = 'Your AUTH Token'
    phlo_id = 'Your PHLO ID' # https://console.plivo.com/phlo/list/
    payload = {"from" : "+14157778888","to" : "+14157778889"}
    phlo_client = plivo.phlo.RestClient(auth_id=auth_id, auth_token=auth_token)
    phlo = phlo_client.phlo.get(phlo_id)
    response = phlo.run(**payload)
    print str(response)
    

    You can get your Auth_ID and Auth_token from your dashboard AUTHID

    You can find the PHLO_ID on the PHLO Listing page. PHLO Listing

    Test and Validate

    You can save the file and use the below command to run your code.

    $ python trigger_phlo.py
    
    Note: If you are using a Plivo Trial account for this example, you can only make calls to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Here’s how to use Plivo APIs and XML to implement voice notifications.

    How it works

    Outbound Call Flow

    Plivo requests an answer URL when the call is answered (step 4) and expects the file at that address to hold a valid XML response from the application with instructions on how to handle the call. To see how this works, you can use https://s3.amazonaws.com/static.plivo.com/notification.xml as an answer URL to test your first outgoing call. The file contains this XML code:

    <Response> 
    <Speak>Congratulations, your order was successfully placed</Speak> 
    </Response>
    

    This code instructs Plivo to say, “Congratulations, your order was successfully placed” to the call recipient. You can find the entire list of valid Plivo XML verbs in our XML Reference documentation.

    Prerequisites

    To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a Python development environment.

    Create a voice notification application in Python

    Create a file called make_call.py and paste into it this code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    import plivo
    
    client = plivo.RestClient('<auth_id>','<auth_token>')
    response = client.calls.create(
        from='<caller_id>',
        to='<destination_number>',
        answer_url='https://s3.amazonaws.com/static.plivo.com/notification.xml',
        answer_method='GET', )
    print(response)
    

    Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).

    Note: We recommend that you store your credentials in the auth_id and auth_token environment variables, to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use the os module (os.environ) to store environment variables and fetch them when initializing the client.

    Test

    Save the file and run it.

    $ python make_call.py
    
    Note: If you’re using a Plivo Trial account, you can make calls only to phone numbers that have been verified with Plivo. You can verify (sandbox) a number by going to the console’s Phone Numbers > Sandbox Numbers page.