Get Started with Call Forwarding Using Python

    Overview

    You can use call forwarding to dynamically route incoming calls to available agents, extensions, or departments that cater to callers’ needs.

    Start by signing up for a free trial account that you can use to experiment with and learn about our services. The free trial account comes with free credits, and you can add more as you go along. You can also add a phone number to your account to start testing the full range of our voice and SMS features. A page in our support portal walks you through the signup process.

    You can start making and receiving calls either by using our PHLO visual workflow builder or our APIs and XML documents. Follow the instructions in one of the tabs below.

    You can create and deploy a workflow to implement a call forwarding use case with a few clicks on the PHLO canvas.

    Prerequisites

    1. Create a Plivo account if you don’t have one already. Sign up with your work email address.
    2. Buy a Plivo number: You must have a voice-enabled Plivo phone number to receive incoming calls. You can purchase numbers from the Numbers page of the Plivo console, or by using the Numbers API.
    3. PHLO application: When you receive a call on a Plivo voice-enabled number, you can control the call flow by associating a PHLO application with that Plivo phone number. Plivo will fetch the PHLO associated with the number and expect valid instructions via PHLO to handle the call.

    Create the PHLO

    • On the left navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any. 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, displaying the Start node, which is the starting point of any PHLO. You can choose between three available trigger states to start the PHLO: Incoming Message, Incoming Call, and API Request. For this PHLO, choose Incoming Call.
    • From the list of components on the left side, drag and drop the Call Forward component onto the canvas to add a Call Forward node to the canvas.
    • Connect the Start node with the Call Forward node by dragging down from the Incoming Call trigger state.

    • Configure the Call Forward node to select the From number using a variable. Enter all the numbers you want to call in the To field, separated with commas.
    • Once you’ve configured a node, click Validate to save the configuration.
    • If this PHLO had more nodes you could configure them now. After you complete all the configurations, click Save.

    Your complete PHLO will look like this:

    Call Forwarding

    Assign the PHLO to a Plivo number for incoming calls

    Once you’ve created and configured your PHLO, assign your PHLO to a Plivo number.

    1. Go to the Numbers page of the Plivo console.
    2. Under Your Numbers, click the phone number you want to use for the PHLO.
    3. In the Number Configuration section, under Application Type, select PHLO from the drop-down list.
    4. From the PHLO Name drop-down, select the PHLO you want to use with the phone number, then click Update Number.

    Assign PHLO to a Plivo Number

    Test and validate

    To test that the PHLO is working, call your Plivo phone number to see how the inbound call is forwarded.

    For more information about creating a PHLO app, see the PHLO User Guide. For information on components and their variables, see the PHLO Components Library.

    To implement Call Forwarding use-case in the traditional XML way, you can refer to the instructions in the below section to begin your implementation.

    Outline

    Call Forward- Call Flow

    Implementation

    In this section, we will guide you in setting up an app to receive a call on your Plivo number and forward it using Plivo XML. In this example, when an incoming call is received, Plivo will forward the call using the <Dial> XML.

    Prequisites

    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. Buy a Plivo Number: 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.
    3. Answer URL & Callback URLs: When a call is received on a Plivo voice-enabled number, you can control the call flow by declaring an answer URL for your Plivo application associated with that Plivo phone number. Plivo will invoke the answer URL specified and expect a valid XML response to handle the call.

      Notice how the concept of Answer URLs applies to both outbound API calls as well as incoming calls to your Plivo numbers. In the outbound API call example in the Make-Outbound-Calls use-case guide, we specified the answer URL along with the make call API request, whereas in the case of incoming calls to Plivo numbers, the answer URL is specified in the Plivo application associated with the phone number.

      In addition to requests to the answer URL, Plivo initiates HTTP requests to your application server through the course of a call based on the specific XML elements in your answer XML. Such requests can be broadly classified into two categories:

      Action URL requests: XML instructions to carry forward the call are expected in response to these requests. These requests are typically invoked at the end of an XML element’s execution. For example: when an IVR input is received from the caller during a GetInput XML execution.

      Callback URL requests: No XML instructions are expected in response to these requests. Such requests serve as webhooks to notify your application server of important events through the course of an XML element’s execution. For example: when a conference participant is muted or unmuted.

    4. Set Up Your Web Server: To be able to host Answer and Callback URLs and to be able to provide valid XMLs and accept notifications on these URLs respectively, you need to host a webserver at your end. To set up your Web Server in your preferred programming language, please refer to the instructions available in the Set Up a Flask Server for Incoming Calls & Callbacks section.

    Set Up a Flask server for Incoming Calls & Callbacks

    In this section, we’ll walk you through how to set up a Flask server in under five minutes and start handling incoming calls & callbacks.

    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
      
    • Alternatively, you can download the source code from this repo(beta branch) 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.

    Create a Flask App to Forward Incoming Calls

    Use the following code snippet to start a local server.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    from flask import Flask, request, make_response, Response
    from plivo import plivoxml
    
    app = Flask(__name__)
    
    @app.route('/forward_call/', methods=['GET', 'POST'])
    def forwardcall():
    
        response = plivoxml.ResponseElement()
        response.add(
            plivoxml.DialElement().add(
                plivoxml.NumberElement('15671234567'))) // call wll be forwarded to this number
        return(response.to_string())
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', debug=True)
    

    Save this code in any file (name the file something like forward_call.py). To run this file on the server, go to the folder where this file resides and use the following command:

    $ python forward_call.py
    

    And you should see your basic server app in action on http://localhost:5000/forward_call/

    Exposing your local server to the internet

    To receive Incoming Calls and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.

    ngrok block diagram

    You can download and install ngrok from here. Follow the detailed configuration instructions to get started.

    Run ngrok on the port which currently hosts your application. For example, if your port number is 8000, run the following command:

    ./ngrok http 8000
    

    This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.

    Ngrok CLI

    You can check the app in action on https://6ea358b0f703.ngrok.io/forward_call/.

    Create a Plivo Application

    1. Create an Application by visiting the Application Page and click on New Application or by using Plivo’s Application API.
    2. Give your application a name. Let’s call it Forward Call. Enter your server URL (e.g., http://myvoiceapp.com/forward_call) in the Answer URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
    3. Click on Create Application to save your application.

    Plivo Create Application

    Assign a Plivo number to your app

    1. Navigate to the Numbers page and select the phone number you want to use for this app.
    2. Select Forward Call (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Call Forward Application

    Test and validate

    Make a call to your Plivo number using a regular mobile phone. Plivo will send a request to your Answer URL requesting for a valid XML response and forward the call. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.

    Common use cases for call forwarding

    Call forwarding plays a pivotal role in routing customers’ calls based on any of several possible factors.

    • Agent availability: You can place calls in a holding queue and route them to an available agent as soon as one is available.
    • Business hours-based forward: You can route calls to an office number during business hours and to a mobile phone or voicemail during non-business hours.
    • Time zone-based forward: You can forward calls to agents from different time zones to ensure round-the-clock availability.