Dial Status Reporting using Node.js

    Overview

    Dial Status Reporting is the concept of checking the call status of an ongoing call to decide and process a call based on its status. For all the calls made using Plivo’s Make a Call API or Dial XML, Plivo sends the call status to your application server during different stages of a call. Call status will be sent as an HTTP webhook request to multiple URLs such as ring_url, answer_url, fallback_url, action_url, callback_url, and hangup_url.

    Plivo will send one of the following values under the CallStatus parameter:

    in-progress

    The call has been answered and is currently in progress. Calls under this status can be explicitly terminated using the Hangup API.

    completed

    The call has been completed. Its either terminated using the Hangup API or ended by one of the parties in the call.

    ringing

    The call is currently ringing. This status is sent to the Ring URL.

    no-answer

    The call hasn't been answered by the callee. Your application must have the logic to parse this parameter and take actions based on this.

    busy

    The callee is busy on a different call. You should retry after sometime.

    cancel

    The call has been canceled by the caller

    timeout

    There was a time out while connecting your call. This is either an issue with one of the terminating carriers or network lag in our system.

    The following parameters are sent to your app server in the HTTP webhook:

    ParameterDescription
    DialRingStatusThis indicates if the <Dial> attempt rang or not. It can be true or false.
    DialHangupCauseThe standard telephony hangup cause.
    DialStatusStatus of the dial.Can be completed, busy, failed, timeout or no-answer.
    DialALegUUIDCallUUID of the A leg.
    DialBLegUUIDCallUUID of the B leg. Empty if nobody answers.

    This guide will help you to understand the importance of dial status reporting for your use-case.

    Start your implementation with Plivo for dial status reporting using PHLO or the traditional API/XML way. PHLO allows you to create and deploy the call flows using its intuitive canvas in few clicks. Refer to the instructions from the respective tabs below to start your integration with PHLO or XML as you wish.

    For Dial Status Reporting, 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.

    Note: When you create a PHLO application to handle the incoming calls to your Plivo number(s), you don't have to write a single line of code because you will define the instructions to manage the call flow of the incoming calls on the PHLO itself.

    Outline

    Screen Incoming Calls- Call Flow

    Implementation

    In this section, we will guide you to create a PHLO for Dial Status Reporting.

    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. Buy a Plivo number: You must have a voice-enabled Plivo phone number 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. PHLO Application: When you receive a call on a Plivo voice-enabled number, you can control the call flow by associating a PHLO application to 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

    You can implement Dial Status Reporting app using PHLO by referring to the below GIF and instructions.

    Create the PHLO to Block calls from a specific number

    • On the side 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 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.
    • From the list of components, on the left hand side, drag and drop the Call Forward component onto the canvas. This will add a Call Forward node onto the canvas.
    • Connect the Start node with the Call Forward node, using the Incoming Call trigger state.
    • Configure the Call Forward node to select the From number using a variable. Enter all the numbers you wish to call in the To field, separated with commas.
    • Once you have configured a node, click Validate to save the configurations.
    • Next, from the list of components, drag and drop the HTTP Request component onto the canvas.
    • Connect the Call Forward node with the HTTP Request node, using all the Dial Status states such as Completed, No Answer, Busy/Rejected, & Failed.
    • Configure the HTTP Request node. Enter your application server URL in the box next to the HTTP Method(GET/POST) field.
    • Also, provide the key:value pair of the callback attributes such as DialRingStatus, DialHangupCause, DialStatus, DialALegUUID, & DialBLegUUID.
    • After you complete the configurations, click Save. Your PHLO is now ready. Link the PHLO to a Plivo Phone Number and test it out. For more information, see Configure the PHLO to Your Plivo Number.

    Configure the PHLO to Your Plivo Number

    Once you have created and configured your PHLO, You can configure the PHLO to one of your Plivo numbers by following the instructions below:

    Configure the PHLO to your Plivo Number

    • On the side navigation bar, click Phone Numbers. The Phone Numbers page will appear and display your existing Phone numbers(if any). If this is your first PHLO, then the PHLO page will be empty.
    • On the Phone Numbers page, you can either select one of your existing Plivo numbers and configure the PHLO or buy a new number if you do not have one already. Refer to the Buy a Phone Number Guide for more information.
    • After selecting your Plivo number, on the Your Numbers page, under the number configuration section, you can select the Application Type as PHLO and select your PHLO from the PHLO list and click on Update Number once done.

    Test and Validate

    You can now make a call to your Plivo phone number from one of the blocked phone number(s) or country and see that the calls are rejected using PHLO. If you make a call from any other number, the call will be forwarded as specified in the PHLO.

    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.

    If you still wish to implement dial status reporting in the traditional API/XML way, you can refer to the instructions in the following section to begin your implementation.

    Outline

    Outbound- Call Flow

    Implementation

    In this section, we will guide you in setting up an dial status reporting app on your Plivo number. In this example, when an incoming call is received, the call will be forwarded as the XML returned from your answer_url will be a <dial> XML. Once the destination number(s) amswers the call, the status of the call will be sent to your app server. This example uses an action URL in the Dial XML. After completion of the call, Plivo will report back the status to this URL. You can control the call flow by returning a valid Plivo XML from the action URL and setting the redirect attribute as “true”. In this example, Plivo will POST the call status to http://foo.com/dialstatus/action/ and expects a valid XML since the redirect attribute is set to “true”.

    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 Express Server for Incoming Calls & Callbacks section.

    Set Up a Express server for Incoming Calls & Callbacks

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

    Install Node.js

    Operating SystemInstructions
    OS X & LinuxTo see if you already have Node.js installed, run the command node --version in the terminal. If you don't have it installed, you can install it from here .
    WindowsTo install Node.js on Windows you can download it from here and install.

    Install Plivo Node.js Package

    • Create a project directory, run the following command:

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

      $ cd mynodeapp
      
    • Install the SDK using npm

      $ npm install plivo
      

    Create a Express Server for Dial Status Reporting

    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
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
    var plivo = require('plivo');
    var express = require('express');
    var app = express();
    
    app.set('port', (process.env.PORT || 5000));
    app.use(express.static(__dirname + '/public'));
    
    app.all('/dialstatus/', function(request, response) {
        var r = plivo.Response();
        var params = {
            'action': "https://www.foo.com/dialstatus/action/",
            'method': "POST",
            'redirect': "true"
        };
        var dial = r.addDial(params);
        var first_number = "+15671234567";
        dial.addNumber(first_number);
        console.log (r.toXML());
        response.set({
            'Content-Type': 'text/xml'
        });
        response.end(r.toXML());
    });
    
    app.all('/dialstatus/action/', function(request, response) {
        var status = request.param('Status');
        var aleg = request.param('DialALegUUID');
        var bleg = request.param('DialBLegUUID');
    
        console.log ('Status : ' + status + ' Aleg UUID  : ' + aleg + ' Bleg UUID : ' + bleg);
    });
    
    app.listen(app.get('port'), function() {
        console.log('Node app is running on port', app.get('port'));
    });
    

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

    $ node dial_status.js
    

    And you should see your basic server app in action on http://localhost:3000/dialstatus/

    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 80, run the following command:

    ./ngrok http <port_on_which_your_local_server_is_running>
    

    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.

    Sample ngrok CLI

    Create a Plivo Application

    1. Create an Application by visiting the Application Page and click on New Application. You can also use Plivo’s Application API.
    2. Give your application a name. Let’s call it Dial Status Report. Enter your server URL (e.g. http://example.com/dialstatus/) 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 to save your application.

    Create dialstatus 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 Dial Status Report (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    assign dialstatus Application

    Test and validate

    Call the Plivo phone number assigned to your app. The calls will be forwarded to the number specified in the dial XML app, also, the call details will be posted to your application server during different call statuses.