Get Started with Connect Call to Second Person Using .NET

    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 Dotnet Dev Environment

    You must set up and install Dotnet Framework(.NET Framework 4.6 or higher) and Plivo’s Dotnet SDK. Here’s how.

    Install Dotnet Framework

    Operating SystemInstructions
    macOS & LinuxYou would already have Dotnet Framework installed, you can check this by running the command dotnet --version in the terminal. If you do not have it installed, you can install it from here.
    WindowsTo install Dotnet Framework on Windows you can follow the instructions from here.

    Install Plivo .NET Nuget Package using Visual Studio

    • Create a new project in Visual Studio

      Create New Project

    • Choose a Template for the new project

      Choose Template

    • Install the Plivo Nuget package

      Manage Nuget packages Select Plivo Nuget package

    Trigger the PHLO

    Once you have created and setup your Dotnet 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, open the file in the CS project called Program.cs 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
    
    using System;
    using System.Collections.Generic;
    using Plivo;
    
    namespace test_PHLO
    {
        class Program
        {
            public static void Main(string[] args)
            {
                var phloClient = new PhloApi("<auth_id>", "<auth_token>");
                var phloID = "phlo_id"; // replace the placeholder phlo_id with your PHLO_ID from Plivo Console
                var phlo = phloClient.Phlo.Get(phloID); 
                var data = new Dictionary<string, object>
                {
                    { "from", "+14157778888" },
                    { "to", "+14157778889" }
                    { "second_user", "+14157778887" }
    
                };  
                Console.WriteLine(phlo.Run(data));
            }
        }
    }
    

    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 run it.

    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 Your 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 .Net Framework App section.

    Set Up a .NET Framework App

    In this section, we’ll walk you through how to set up a .NET Framework app in under five minutes and start making calls.

    Install .NET Framework

    You must set up and install Dotnet Framework(.NET Framework 4.6 or higher) and Plivo’s Dotnet SDK to make calls. Here’s how.

    Operating SystemInstructions
    macOS & LinuxTo see if you already have Dotnet Framework installed, run the command dotnet --version in the terminal. If you do not have it installed, you can install it from here.
    WindowsTo install Dotnet Framework on Windows follow the instructions listed here.

    Install Plivo .NET Package using Visual Studio

    • Create a MVC web app:

    Create a MVC app

    • Configure the MVC app and provide a project name:

    Configure the MVC app

    • Install the Plivo Nuget package

    Install Plivo Nuget Package

    Create a MVC Controller to Connect Calls to second person

    Create a Controller called Connect.cs in “Controllers Directory” and paste the following 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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    using System;
    using Plivo;
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    
    namespace VoiceApp.Controllers
    {
        public class Connect : Controller
        {
            public IActionResult Index()
            {
                var hostName = Request.HttpContext.Request.Host.Value;
                Console.WriteLine(hostName);
                var api = new PlivoApi("<auth_id>", "<auth_token>");
                    var response = api.Call.Create(
                        to: new List<String> { "+14157654321" },
                        from: "+14151234567",
                        answerUrl: "https://" + hostName + "/Connect/Dial/"
                    );
    
                    return this.Content(response.ToString());
            }
    
            public IActionResult Dial()
            {
                Plivo.XML.Response resp = new Plivo.XML.Response();
                resp.AddSpeak("Please wait while we connect your call to the second number",
                new Dictionary<string, string>() { });
                Plivo.XML.Dial dial = new Plivo.XML.Dial(new Dictionary<string, string>(){});
                dial.AddNumber("+14151234567",
                    new Dictionary<string, string>() { }); // Dial to second number
                resp.Add(dial);
    
                var output = resp.ToString();
                Console.WriteLine(output);
    
                return this.Content(output, "text/xml");
            }
        }
    }
    

    Before starting the app, you have to update Properties/launchSettings.json by setting the applicationUrl as

        "applicationUrl": "http://localhost:5000/"
    

    Run the project and you should see your basic server app in action on http://localhost:5000/Connect/

    Exposing your local server to the internet

    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

    In this case, the ngrok url will be something like https://37347786acf6.ngrok.io/Connect and you can check the XML document using any browser.

    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.