Set up Your Dotnet Dev Environment for Voice Calls

    In this guide, you will learn how to set up a development environment in under 5 minutes to trigger API requests and to start serving valid Plivo XML to control your call flow. Also, Using a tunnelling software like ngrok, we will see how you can expose your local dev server with a public address to test out your integration with Plivo.

    Prerequisites

    Install .NET Framework and the Plivo Nuget package

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

    Install Dotnet Framework

    Operating SystemInstructions
    OS X & 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 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 an API Request

    To trigger any Plivo API, you can create a file in the project directory and execute the code. Please check below example to make an outbound call:

    • 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
    
    using System;
    using System.Collections.Generic;
    using Plivo;
    
    namespace testplivo
    {
        class Program
        {
            static void Main(string[] args)
            {
                var api = new PlivoApi("auth_id", "auth_token");
                var response = api.Call.Create(
                    to: new List<String> { "the_destination_number" },
                    from: "the_callerID",
                    answerMethod: "GET",
                    answerUrl: "http://s3.amazonaws.com/static.plivo.com/answer.xml"
                );
                Console.WriteLine(response);
            }
        }
    }
    
    • Replace the placeholders of mandatory parameters to make outbound call using Plivo API.
    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
    • You can use Environment.SetEnvironmentVariable Method to store environment variables and fetch them using Environment.GetEnvironmentVariable Method while initializing the client.
    • Replace the placeholder the_callerID with the Phone number which you have purchased and the_destination_number with the phone number you will be making calls to.
    • Both the_callerID and the_destination_number should be in E.164 format, for example +15671234567.
    • Save the file and run it.

    Make outbound call

    Note: You can follow the same approach to trigger any other API requests. Please refer to the detailed API reference to check all the API requests available on the Voice API platform.

    Serve an XML Document and Manage Callbacks

    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. We will see how to create and set up a new Plivo Application through the Plivo console in the following section.

    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 above, 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.

    Set up a .NET Framework app to Serve XML & Manage Callbacks

    In this section, we’ll walk you through how to set up a .NET Framework app in under five minutes to serve XML documents & manage callbacks.

    • 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 Controller to Receive 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/

    Ngrok Setup

    To sever XML documents 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.

    Use Ngrok to Expose Your Local Server to the Internet

    Run ngrok on the port which currently hosts your application. For example, if your port number is 5000, run the following command:

    ./ngrok http 5000
    

    This will start the ngrok server on your local server. Refer to the below screenshot.

    ngrok

    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. And you should see your basic server app in action on http://<nrgok_URL>/receive_call/

    ngrok

    Note: You can follow the same approach to serve any other XML documents to manage various call flows. Please refer to the detailed XML reference to check all the XML elements available on the Voice API platform.