How to Build a Call Forwarding App in .NET with ASP.NET MVC and Plivo

Businesses use call forwarding all the time to route incoming calls to available agents, extensions, or departments that cater to the caller’s needs. Creating a call forwarding app is simple when you use Plivo’s Dotnet SDK. This guide shows you how to receive incoming calls on Plivo numbers and manage the call flow once a call reaches the Plivo voice platform. To see how to do this, we’ll build a .NET MVC application to forward the call to a mobile number using the Dial XML element.

Prerequisites

Before you get started, you’ll need:

  • A Plivo account — sign up for one for free if you don’t have one already.
  • A voice-enabled Plivo phone number if you want to receive incoming calls. To search for and buy a number, go to Phone Numbers > Buy Numbers on the Plivo console.
  • ASP.NET MVC application and Plivo NuGet package.
  • ngrok — a utility that exposes your local development server to the internet over secure tunnels.

How it works

Call Forward

Create an ASP.NET MVC application to forward incoming calls

Once you’ve created the ASP.NET MVC application using this tutorial, you can add the Plivo .NET SDK using the NuGet package manager. Create a Controller, name it ForwardCalls.cs to handle incoming calls on a Plivo number. To handle an incoming call, you need to return an XML document from the URL configured as the Answer URL in the application assigned to the Plivo number. The .NET SDK can manage the XML document generation, and you can use the Dial XML Element to forward the call to a mobile number. Use this code:

using System;
using System.Collections.Generic;
using Plivo.XML;
using Microsoft.AspNetCore.Mvc;

namespace VoiceApp.Controllers {
  public class ForwardCalls: Controller {
    public IActionResult Index() {
      Plivo.XML.Response resp = new Plivo.XML.Response();
      Plivo.XML.Dial dial = new Plivo.XML.Dial(new Dictionary < string, string > () {});

      // call will be forwarded to the below number
      dial.AddNumber("+14156667777",
        new Dictionary < string, string > () {});
      resp.Add(dial);

      var output = resp.ToString();
      Console.WriteLine(output);

      return this.Content(output, "text/xml");
    }
  }
}

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

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

Test the code locally

Save the file and run the application. You should see your basic server app in action on http://localhost:5000/ForwardCalls/

Expose the local server to the internet using ngrok

Once you see the application working locally, the next step is to connect the application to the internet to return the XML document to process the incoming call. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels.

Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive calls (5000 in this case, as our local ASP.NET MVC application is running there):

$ ./ngrok http 5000

Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.

Ngrok CLI

Test the link by opening the ngrok URL(https://42f68190ab0d.ngrok.io/ForwardCalls/) in a browser or HTTPie to check the XML response from the ngrok URL.

XML document with GetDigits XML element

Connect the ASP.NET MVC application to a Plivo number

The final step is to configure the application as a Plivo voice application and assign it to a Plivo number on which you want to receive incoming calls.

Go to the Plivo console and navigate to Voice > Applications > XML, then click on the Add New Application button in the upper right.

Provide a friendly name for the application — we used “App-call-forward” — and configure the ngrok URL https://42f68190ab0d.ngrok.io/ForwardCalls/ as the Answer URL. Select the HTTP verb as POST, then click Create Application.

Create Plivo App to Forward Incoming calls

Now go to Phone Numbers > Your Numbers and click on the number to which you want to assign the application. From the Plivo Application drop-down, choose the voice application you just created. Finally, click Update Number.

Assign the call forward Plivo App to a Plivo Number

Test the application

Make a phone call to the Plivo number you selected. You should see that the ASP.NET MVC application automatically forwards the call to the phone number configured in the call forwarding app.

And that’s how simple it is to receive an incoming call on a Plivo number and forward it using XML documents using Plivo’s Dotnet SDK and ASP.NET MVC application. You can implement other use cases on the Plivo Voice platform, such as phone system IVR, receive DTMF/Speech inputs, and number masking, as your business requires.

Haven’t tried Plivo yet? Getting started is easy and only takes five minutes. Sign up today.

comments powered by Disqus

By submitting this form, you agree we may contact you in the manner described in our Privacy Policy