Skip to main content

Overview

You may want to have an application dial out for someone, so that it calls them on their phone, then connects them to the number they want. This involves three tasks:
  1. Make an outbound call to a caller.
  2. When the call recipient answers the phone, place a new call to a different number (second user).
  3. Bridge the calls (first and second user) after the second user answers.
Common use cases for this practice include click to call, where a server application directs a call to a person who clicks on a web link, then connects them with a company representative. This guide shows how to code connecting 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.
Here‘s how to connect a call to a second person using XML.

Prerequisites

To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. You must have a voice-enabled Plivo phone number to receive incoming calls; you can rent numbers from the Numbers page of the Plivo console, or by using the Numbers API. If this is your first time using Plivo APIs, follow our instructions to set up a .NET development environment and a web server and safely expose that server to the internet.

Create an MVC controller to connect calls to a second person

In Visual Studio, create a controller called Connect.cs and paste into it this code.
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> { "<destination_number>" },
                    from: "<caller_id>",
                    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("<second_number>",
                new Dictionary<string, string>() { }); // Dial to second number
            resp.Add(dial);
    
            var output = resp.ToString();
            Console.WriteLine(output);
    
            return this.Content(output, "text/xml");
        }
    }
}
Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers (for example, 12025551234).Before starting the application, edit Properties/launchSettings.json and set the applicationUrl as
"applicationUrl": "http://localhost:5000/"
Run the project and you should see your basic server application in action at http://localhost:5000/Connect/.Set up ngrok to expose your local server to the internet.
Note: We recommend that you store your credentials in the auth_id and auth_token environment variables, to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch them from the environment variables. You can use the Environment.SetEnvironmentVariable method to store environment variables and Environment.GetEnvironmentVariable to fetch them when when initializing the client.

Test

Have your application make a call to a regular mobile phone. Plivo will send a request to your answer URL requesting a valid XML response and connect the call to a second user.