Dial Status Reporting Using Ruby

    Overview

    Plivo passes the call status of an ongoing call so you can decide how to process it. For all the calls made using Plivo’s Make a Call API or Dial XML, Plivo sends the call status to the application server at different stages of a call. We send call status as an HTTP webhook request to URLs such as ring_url, answer_url, fallback_url, action_url, callback_url, and hangup_url.

    In each callback, the CallStatus parameter takes one of these values:

    in-progress

    The call was answered and is in progress. Calls with this status can be terminated using the Hangup API.

    completed

    The call was completed, terminated either by the Hangup API or by one of the parties in the call.

    ringing

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

    no-answer

    The call was not answered.

    busy

    The called line is busy.

    cancel

    The call was canceled by the caller.

    timeout

    There was a timeout while connecting your call, caused by either an issue with one of the terminating carriers or network lag in our system.

    Plivo sends these parameters to the application server in the webhook:

    ParameterDescription
    DialRingStatusIndicates whether the dial attempt rang or not. Values: true, false
    DialHangupCauseThe standard telephony hangup cause.
    DialStatusStatus of the dial. Values: completed, busy, failed, timeout, no-answer
    DialALegUUIDCallUUID of the A leg.
    DialBLegUUIDCallUUID of the B leg. Empty if nobody answers.

    You can implement dial status reporting 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 PHLO to handle dial status reporting with a few clicks on the PHLO canvas, without writing a single line of code.
    

    How it works

    Screen Incoming Calls- Call Flow

    Prerequisites

    To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. To receive incoming calls, you must have a voice-enabled Plivo phone number. You can rent numbers from the Numbers page of the Plivo console, or by using the Numbers API.

    Create the PHLO

    To create a PHLO, visit the PHLO page of the Plivo console. If this is your first PHLO, the PHLO page will be empty.

    Create the PHLO to Block calls from a specific number

    • Click Create New PHLO.
    • In the Choose your use case pop-up, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. It lets you trigger a PHLO to start upon one of three actions: incoming SMS message, incoming call, or API request.
    • From the list of components on the left side, drag and drop the Call Forward component onto the canvas. When a component is placed on the canvas it becomes a node.
    • Draw a line to connect the Start node’s Incoming Call trigger state to the Call Forward node.
    • In the configuration panel for the Call Forward node, enter the caller ID variable {{Start.call.from}} in the From field. Enter any numbers you want to call in the To field, separated by commas.
    • Once you’ve configured the node, click Validate to save the configuration.
    • Next, from the list of components, drag and drop the HTTP Request component onto the canvas.
    • Draw lines to connect all of the dial status states of the Call Forward node (Completed, No Answer, Busy/Rejected, Failed) with the HTTP Request node.
    • Configure the HTTP Request node. Enter your application server URL in the box next to the HTTP Method (GET/POST) field.
    • Provide key:value pairs of the callback attributes you’re interested in, such as DialRingStatus, DialHangupCause, DialStatus, DialALegUUID, and DialBLegUUID.
    • Give the PHLO a name by clicking in the upper left, then click Save.

    Assign the PHLO to a Plivo number

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

    Configure the PHLO to your Plivo Number

    • On the Numbers page of the console, under Your Numbers, click the phone number you want to use for the PHLO.
    • In the Number Configuration box, select PHLO from the Application Type drop-down.
    • From the PHLO Name drop-down, select the PHLO you want to use with the number, then click Update Number.

    Test

    You can now make a call to your Plivo phone number. After the call ends, Plivo reports back the status via the key:value pairs you specified to the URL you specified.

    For more information about creating a PHLO app, see the PHLO User Guide.For more information about creating a PHLO application, see the PHLO Getting Started 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 Rails Server for Incoming Calls & Callbacks section.

    Set Up a Rails Server for Incoming Calls & Callbacks

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

    Install Rails

    Use the below command to install Rails

    $ gem install rails
    

    Create a Rails Project

    As we have Rails and its dependencies installed, we can use them to create a new Rails project. As the initial step, using rails we can auto-generate code in the ruby on rails folder structure. Use the below command to start your Rails project.

    $ rails new plivotest
    

    This will create a plivotest directory with the necessary folders & files for development.

    Install Plivo

    As we have the rails application created, now, let’s add Plivo-Ruby by modifying the Gemfile. Open the Gemfile in any IDE/text-editor and add the following line:

    gem 'plivo', '~> 4.16.0'
    

    You can use the below command to install the Plivo-Ruby gem into the bundle:

    $ bundle install
    

    Create a Rails Controller for Dial Status Reporting

    Change the directory to our newly created project directory, i.e, plivotest directory and run the below command to create a rails controller for Dial Status Reporting.

    $ rails generate controller Plivo voice
    

    This will generate a controller named plivo_controller in the app/controllers/ directory and a respective view will be generated in app/views/plivo directory. We can delete the View as we will not need it.

    $ rm app/views/plivo/voice.html.erb
    

    Now, open the app/controllers/plivo_controller.rb file and add include the following code in the PlivoController class:

    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
    
    include Plivo
    include Plivo::XML
    include Plivo::Exceptions
    
    class PlivoController < ApplicationController
     def dialstatus
        r = Response.new()
        params = {
            'action' => "https://example.com/dialstatus/action/", # Redirect to this URL after leaving Dial.
            'method' => 'GET' # Submit to action URL using GET or POST.
        }
    
        r.addSpeak("Connecting your call..")
        d = r.addDial(params)
        d.addNumber("+15671234567")
        xml = Plivo::PlivoXML.new(r)
        render xml: xml.to_xml
     end
     def dialstatusaction
        status = params[:DialStatus]
        aleg = params[:DialALegUUID]
        bleg = params[:DialBLegUUID]
        puts "Status : #{status}, ALeg UUID : #{aleg}, BLeg UUID : #{bleg}"
     end
    end
    

    Add a Route

    Now, to add a route for the screen function in the PlivoController class, open the config/routes.rb file and change the line:

    get 'plivo/voice' 
    

    to

    get 'plivo/dialstatus'
    get 'plivo/dialstatusaction'
    

    Now the plivo_controller is ready for dial status reporting, you can use the below command to handle your inbound calls using Rails and Plivo Ruby SDK.

    $ rails server
    

    Your local development server will be started and you can test the rails app for inbound calls via the URL http://127.0.0.1:3000/plivo/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.

    Note: Before starting Ngrok service, you need to add ngrok in the config.hosts list in the config/environments/development.rb file and include the below line. You will start facing Blocked host error if you fail to add this.
    # Whitelist Ngrok domain
    config.hosts << /[a-z0-9]+\.ngrok\.io/
    

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

    ./ngrok http 3000
    

    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/plivo/dialstatus/.

    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/plivo/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 Application 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.