Receive Incoming Calls Using .NET

    Overview

    This guide shows how to receiving incoming calls on a Plivo number and greet callers with a text-to-speech message. Managing incoming calls is a key part of the call flow in many common use cases, such as interactive voice response (IVR), call forwarding, and conference calling.

    You can handle incoming calls 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 receive an inbound call with a few clicks on the PHLO canvas, without writing a single line of code.

    How it works

    When you receive a call on a voice-enabled Plivo number, you can control the call flow by associating a PHLO application to that Plivo number. Plivo will fetch the PHLO associated with the number and expect valid instructions via PHLO to handle the call.

    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 a PHLO to receive incoming call

    • 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 Play Audio 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 Play Audio node.
    • In the Configuration pane at the right of the canvas, configure the Play Audio node to play a message to the caller.
    • Once you’ve configured the node, click Validate to save the configuration.
    • 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.

    • 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.

    Assign PHLO to a Plivo number

    Test

    You can now make a call to your Plivo phone number and see how the inbound call is handled.

    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.

    To receive your first inbound call in the traditional 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 app to receive a call on your Plivo number and answer it using Plivo XML. In this example, when an incoming call is received, Plivo’s text-to-speech engine will play a message using the <Speak> 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 .Net Framework App for Incoming Calls & Callbacks section.

    Set Up a .NET Framework App for Incoming Calls & Callbacks

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

    Install .NET Framework

    You must set up and install Dotnet Framework(.NET Framework 4.6 or higher) and Plivo’s Dotnet SDK to receive incoming 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 for Incoming Calls

    Navigate to Controllers directory in “Receivecall” app and create a Controller named ReceivecallController.cs paste the following code.

    Install Plivo Nuget Package

    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
    
    using System;
    using Plivo.XML;
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    
    namespace Receivecall
    {
        public class ReceivecallController : Controller
        {
            public IActionResult Index()
            {
                Plivo.XML.Response resp = new Plivo.XML.Response();
                resp.AddSpeak("Hello, you just received your first call",
                    new Dictionary<string, string>() {
            {
            "loop",
            "3"
            }
                    });
                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/receivecall/

    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.

    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/receivecall and you can check the XML document using any browser.

    XML Document

    Create an 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 Receive_call. Enter your server URL (e.g. http://example.com/receivecall/) 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 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 Receive Call (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Phone Number to Receive Call App

    Test and validate

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