Get started with Phone System IVR using Ruby

    Overview

    Integrated Voice Response (IVR) systems are the first line in gaining customer details. Used widely in call centers, the caller must select from the options in the IVR system to reach the right team. IVR systems are popular as they can handle large call volumes and help reduce the costs associated with customer service. You can build an entire phone menu system on the Plivo platform.

    Start your Phone System IVR implementation with Plivo using PHLO or the traditional 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.

    To implement Phone System IVR 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.

    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

    Implementation

    In this section, we will guide you to create a PHLO to implement Phone System IVR 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. 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

    1. On the top 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.
    2. Click CREATE NEW PHLO to build a new PHLO.
    3. 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, Endpoint Call, and API Request. For this PHLO, we will use the Incoming Call trigger state.
    4. From the list of components, on the left hand side, drag and drop the IVR Menu component onto the canvas. This will add an IVR Menu node onto the canvas.
    5. Connect the Start node with the IVR Menu node, using the Incoming Call trigger state. Note: Make sure to configure the choices for the IVR Menu from the Configurations tab. In this example, we will select 1 and 2 as the allowed choices and enter a message to play to the user.
    6. Click Validate to add 1 and 2 trigger states to the IVR Menu.
    7. Configure No_Input_Prompt and Invalid_Input_Prompt nodes to speak a fixed message in case of a wrong input or no input.
    8. Click Validate to save the configurations for the node.
    9. Next, create two nodes for the Call Forward component and rename them as Connect_to_Support and Connect_to_Sales . Note: You can rename the nodes as per your requirements. We are using specific names in this example to help you relate to the different nodes used in this use case.
    10. Connect Connect_to_Support and Connect_to_Sales nodes to the IVR Menu using the 1 and 2 trigger states.
    11. Similarly, create two nodes for the Play Audio component.
    12. Connect Play Audio_1 and Play Audio_2 nodes to the IVR Menu using the No Input and Wrong Input trigger states.
    13. Rename Play Audio_1 to No_Input_Prompt. Note: You can rename the nodes as per your requirements. We are using specific names to help you relate to the different nodes used in this use case.
    14. Rename Play Audio_2 to Invalid_Input_Prompt.
    15. Connect the Prompt Completed trigger state of No_Input_Prompt and Invalid_Input_Prompt nodes to the IVR Menu. This will send the user back to the IVR Menu if they press an incorrect option, or if they do not press any key.
    16. Configure the Call Forward nodes to select the From number using a variable. For the To number, you can either enter a fixed number directly into the To field, or use a variable configured in the Start node Note: Enter two curly brackets to view all available variables.
    17. Rename Call Forward_1 to Connect_to_Support, and Call Forward_2 to Connect_to_Sales.
    18. Click Validate to save the configurations for the node.
    19. 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 Adding the PHLO to a Phone Number.

    Your complete PHLO will look like this:

    Phone System IVR

    Assign the PHLO to a Plivo Number for Incoming Calls

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

    To assign a PHLO to a number:

    1. Login to your Plivo Console
    2. On the Product Navigation bar, click Phone Numbers.
    3. On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
    4. In the Number Configuration window, select PHLO from the Application Type list.
    5. From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update Number.

    Assign PHLO to a Plivo Number

    If you have not purchased a phone number yet, you can buy Phone number(s) by referring to the instructions available here.

    Test and Validate

    You can now make a call to your Plivo phone number to receive an incoming call and see how the Phone System IVR implementation is working using 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.

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

    Outline

    Phone System IVR- Call Flow

    Implementation

    In this section, we will guide you in setting up an app to receive a call on your Plivo number and add the caller(s) to a IVR Phone Menu using Plivo XML. In this example, when an incoming call is received, Plivo will add the caller(s) to the IVR using the <GetInput> 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 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.13.0'
    

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

    $ bundle install
    

    Create a Rails Controller to Implement Phone System IVR

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

    $ 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
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    
    include Plivo
    include Plivo::XML
    include Plivo::Exceptions
    
    class PlivoController < ApplicationController
      # This file will be played when a caller presses 2.
      $plivo_song = "https://s3.amazonaws.com/plivocloud/music.mp3"
      # This is the message that Plivo reads when the caller dials in
      $ivr_message1 = "Welcome to the Plivo IVR Demo App. Press 1 to listen to a pre recorded text in different languages. Press 2 to listen to a song."
    
      $ivr_message2 = "Press 1 for English. Press 2 for French. Press 3 for Russian"
      # This is the message that Plivo reads when the caller does nothing at all
      $noinput_message = "Sorry, I did not catch that. Please hangup and try again later."
    
      # This is the message that Plivo reads when the caller inputs a wrong number.
      $wronginput_message = "Sorry, it's wrong input."
      def ivr
        r = Response.new()
    
        getinput_action_url = "https://example.com/ivr/firstbranch/"
        params = {
            action: getinput_action_url, 
            method: 'POST', 
            digitEndTimeout: '5',
            inputType:'dtmf',
            redirect:'true'
        }
        getinput = r.addGetInput(params)
        getinput.addSpeak($ivr_message1)
        r.addSpeak($noinput_message)
    
        xml = PlivoXML.new(r)
        render xml: xml.to_xml
      end
      def firstbranch
        digit = params[:Digits]
        r = Response.new()
        
        if (digit == "1")
            getinput_action_url = "https://example.com/ivr/secondbranch/"
            params = {
                action: getinput_action_url, 
                method: 'POST', 
                digitEndTimeout: '5',
                inputType:'dtmf',
                redirect:'true'
            }
            getinput = r.addGetInput(params)
            getinput.addSpeak($ivr_message2)
            r.addSpeak($noinput_message)
    
        elsif (digit == "2")
            r.addPlay($plivo_song)
        else
            r.addSpeak($wronginput_message)
        end
    
        xml = PlivoXML.new(r)
        render xml: xml.to_xml
      end
      def secondbranch
        digit = params[:Digits]
    
        r = Response.new()
    
        if (digit == "1")
            body = "This message is being read out in English"
            params = {
                'language'=> "en-GB"
            }
    
            r.addSpeak(body,params)
        elsif (digit == "2")
            body = "Ce message est lu en français"
            params = {
                'language'=> "fr-FR"
            }
    
            r.addSpeak(body,params)
        elsif (digit == "3")
            body = "Это сообщение было прочитано в России"
            params = {
                'language'=> "ru-RU"
            }
    
            r.addSpeak(body,params)
        else
            r.addSpeak($wronginput_message)
        end
    
        xml = PlivoXML.new(r)
        render xml: xml.to_xml
      end
    end
    

    Add a Route

    Now, you need to add a route for the inbound function in PlivoController class, open the config/routes.rb file and add the below line after the inbound route:

    get 'plivo/ivr'
    get 'plivo/firstbranch'
    get 'plivo/secondbranch'
    

    Now the plivo_controller is ready for your first inbound call, you can use the below command to handle your first inbound call using Rails and Plivo Ruby SDK.

    $ rails server
    

    Your local development server will be started and you can test the rails app for outbound calls via the URL http://127.0.0.1:3000/plivo/ivr/.

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

    Create an 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 Phone IVR. Enter your server URL (e.g., http://www.example.com/ivr) in the Answer URL field and set the method as GET. See our Application API docs to learn how to modify your application through our APIs.
    3. Click on Create to save your application.

    Plivo Create Application Phone IVR

    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 Phone IVR (name of the app) from the Plivo App dropdown list.
    3. Click on ‘Update’ to save.

    Assign Application Phone IVR

    Test and validate

    You can now make a call to your Plivo phone number to receive an incoming call and see how the Phone System IVR implementation is working using Plivo XML.

    Common Use Cases for IVR

    Interactive Voice Response(IVR) implementation is helpful in use cases such as: Auto-Attendant: You can set up an IVR as an auto-attendant and replace an actual receptionist by routing the calls to agents during business hours and by providing automated voice messages during non-business hours. Call Center Setup: You can customize the customer experience by implementing multi-level IVR in call centers that will manage incoming calls by routing the calls to the respective department based on user input. Televoting/Surveys: You can implement IVR in outbound calls to collect information from customers like satisfaction scores of a service provided. Appointment Reminders: Automated appointment reminders are a convenient way to remind customers hours or days before their scheduled appointment and can help avoid missed appointments or facilitate rescheduling as needed. Lead Assignment/Lead Routing: For inbound sales calls, you can set up IVR with a set of qualifying questions to understand customers’ interest by analyzing their response and redirect the call immediately to a representative based on the lead score.