Get Started with Connect Call to Second Person Using Ruby

    Overview

    You may want to request your application to dial out for you, such that you’ll receive a call on your phone, which will then connect you to the user you want. With the help of this tutorial named connect a call to the second person, you may accomplish this. Step-by-step instructions are as follows:

    1. Make an outbound call to the caller.
    2. When the first user answers the phone, a new call is placed to a different user (second user).
    3. Both calls (first and second user) are bridged once the second user answers the call.

    Common Connect call to second use cases include:

    • Call Forward: You can forward calls to another user whom you wish for.
    • Click to Call: You can direct calls to the right representative as well as the person who clicked on the link.
    • Lead assignment and lead routing: For inbound sales calls, you can set up an IVR menu with a set of qualifying questions to discover a customer’s interests, then redirect their call to a representative based on their responses.

    This guide shows how to build Connect a user to second person on the Plivo platform, 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 connect a call to second user 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.

    Implementation

    In this section, we will guide you to create a PHLO to implement connect a call to second use-case.

    Prerequisites

    1. Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already! Find Your Auth Credentials on Plivo Console
    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. Buy a New Plivo Number

    Create the PHLO

    Create a PHLO for Connect a Call to Second User

    1. 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.
    2. Click CREATE NEW PHLO to build a new PHLO.
    3. In 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.
    4. Click the Start node to open the Configurations tab, and then enter the information to retrieve from the HTTP Request payload. Only the keys from, to, and second_user will be filled in here.
    5. Click Validate to save the configurations for the node.
    6. 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.
    7. Connect the Start node with the Initiate Call node, using the API Request trigger state.
    8. Configure the Initiate Call node to select the From and To numbers from 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.
    9. Click Validate to save the configurations for the node.
    10. From the list of components on the left-hand side, drag and drop the Play Audio component onto the canvas. This will add a Play Audio node onto the canvas.
    11. Connect the Initiate Call node with the Play Audio node, using the Answered trigger state.
    12. Configure the Play Audio node with the text of your choice, For example: Please wait while we connect your call to the second number.
    13. Click Validate to to save the configurations of the node.
    14. 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.
    15. Connect the Call Forward node with the Play Audio node, using the Prompt Completed trigger state.
    16. Configure the Call Forward node to select the From and To numbers from 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
    17. Click Validate to to save the configurations of the node.
    18. 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:

    Connect Second User

    • After you complete the configurations, provide a friendly name for your PHLO and click Save. Your PHLO is now ready. You can trigger the PHLO and test it out. For more information, refer to the below section.

    Set up Your Ruby Dev Environment

    You must set up and install Ruby and Plivo’s Ruby SDK. Here’s how.

    Install Ruby

    Operating SystemInstructions
    macOS & LinuxYou would already have Ruby installed, you can check this by running the command ruby --version in the terminal. If you don't have it installed, you can install it using homebrew.
    WindowsTo install Ruby on Windows you can download it from here and install.

    Install Plivo Ruby Package

    • Create a project directory, run the following command:

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

      $ cd myrubyapp
      
    • Add this line to your application’s Gemfile:

      gem 'plivo', '>= 4.3.0'
      
    • And then execute:

      $ bundle
      
    • Or install it yourself as:

      $ gem install plivo
      

    Trigger the PHLO

    Once you have created and setup your Ruby 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 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, follow the below steps.

    • Select the Start node - under API request, fill the Payload key as from, to & second_user and keep the values empty, then click validate & save.

    With Dynamic Payload

    Code

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

    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
    
    require 'rubygems'
    require 'plivo'
    
    include Plivo
    
    AUTH_ID = '<auth_id>'
    AUTH_TOKEN = '<auth_token>'
    
    client = Phlo.new(AUTH_ID, AUTH_TOKEN)
    
    # if credentials are stored in the PLIVO_AUTH_ID and the PLIVO_AUTH_TOKEN environment variables
    # then initialize client as:
    # client = Phlo.new
    
    begin
      phlo = client.phlo.get('phlo_id')
      #parameters set in PHLO - params
      params = {
         from: '+14157778888',
         to: '+14157778889',
         second_user: '+14157778887'
      }
      response = phlo.run(params)
      puts response
    rescue PlivoRESTError => e
      puts 'Exception: ' + e.message
    end
    

    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.

    $ ruby trigger_phlo.rb
    
    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.

    To implement connect a call to second person use-case in the traditional XML way, you can refer to the instructions in the below section to begin your implementation.

    Implementation

    In this section, we will guide you in setting up an app to make a call to a number and when answered, connect the call to second person it using Plivo XML. In this example, when an outgoing call is answered, the call will be made to the second person using the <Dial> XML.

    Prequisites

    1. Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

      Find Your Auth Credentials on Plivo Console

    2. Plivo Phone Number(Optional): Purchase numbers from the Numbers section of your Plivo Console and use the same as the caller ID for the outbound call. This number will also help you receive incoming calls as you must have a voice-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

      Buy a New Plivo Number

    3. Answer URL: When an outgoing call is made number, you can control the call flow by declaring an answer URL. Plivo will invoke the answer URL specified and expect a valid XML response to handle the call. 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.

    4. Set up a web server: To be able to host Answer URL 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 section.

    Set up a Rails server

    You can set up a Rails server in less than five minutes to start handling calls.

    Install Rails

    Use this command to install Rails.

    $ gem install rails
    

    Create a Rails project

    Use this command to start your Rails project. It creates a plivotest directory with the necessary folders and files for development.

    $ rails new plivotest
    

    Install Plivo

    Now edit the Gemfile and add the Plivo Ruby gem by adding the line:

    gem 'plivo', '~> 4.20.0'
    

    Then use this command to install the Plivo Ruby gem into the bundle:

    $ bundle install
    

    Create a Rails controller to Connect Calls to second person

    Change to the newly created plivotest project directory and run this command to create a Rails controller for inbound calls.

    $ rails generate controller Plivo voice
    

    This command generates a controller named plivo_controller in the app/controllers/ directory and a respective view in app/views/plivo directory. You can delete the view, as we don’t need it.

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

    Open the app/controllers/plivo_controller.rb file and add this 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 outbound_call
    		api = RestClient.new('<auth_id>','<auth_token>')
    		response = api.calls.create(
    			'+14151234567',
    			['+14157654321'],
    			'https://'+request.host+'/plivo/connect',
    			{answer_method:'GET'}
    		)
    		render json: response.to_s
    	end
    
    	def connect
    		response = Response.new
    		response.addSpeak('Please wait while we connect your call')
    		dial = response.addDial()
    		dial.addNumber('+14151234567') # Dial to second number
    		xml = PlivoXML.new(response)
    		render xml: xml.to_xml
    	end
    end
    
    Note:
    • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console.
    • We recommend that you store your credentials in the auth_id & auth_token environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables
    • Replace the placeholder +14151234567 with the Phone number which you have purchased and +14157654321 with the phone number you will be making calls to.
    • Both +14151234567 and +14157654321 should be in E.164 format
    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.

    Add a route

    Add a route for the inbound function in the PlivoController class. Open the config/routes.rb file and add below lines:

      get 'plivo/outbound_call'
      get 'plivo/connect'
    

    Now plivo_controller is ready for making calls. Use this command to start the server.

    $ rails server
    

    You can test the Rails app to make calls via the URL http://127.0.0.1:3000/plivo/outbound_call/.

    Expose your local server to the internet

    Your local server must be able to connect with Plivo. To make your server available, we recommend using ngrok, which exposes local servers behind NAT and firewalls to the public internet over secure tunnels. With ngrok you can set webhooks that can talk to a Plivo server.

    ngrok block diagram

    Note: Before you start the ngrok service, you need to add ngrok in the config.hosts list in the config/environments/development.rb file and include the line below. If you forget to add it, you'll see Blocked host errors.
    # whitelist ngrok domain
    config.hosts << /[a-z0-9]+\.ngrok\.io/
    

    Run ngrok on the command line, specifying the port that hosts the application from which you want to make calls. For example, if your port number is 3000, run the command

    ./ngrok http 3000
    

    That command displays output 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 application in action on https://021bf9953e9f.ngrok.io/plivo/outbound_call/.

    Test and validate

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