Forward a Call via Voice Proxy with ASP.NET Core

Published April 18, 2019 by Rabeb Othmani

This is the fifth tutorial on how to use Voice APIs with ASP.NET series.

In the previous tutorial, we learnt how to handle user input with ASP.NET Core.

Nowadays, we use a lot of apps and services that require us to communicate with another party, usually a stranger, via phone calls or messages. Think about food delivery or taxi booking apps.

One way to protect both parties is to mask their phone numbers by using an intermediary number. This is known as Voice Proxy.

Let’s see how we can implement this technique using Nexmo Voice APIs.

Learning objectives

In this tutorial, we will:

  • Create an ASP.NET Core app.

  • Use NancyFX with ASP.NET Core.

  • Create a Nexmo voice application.

  • Create and return NCCOs.

  • Run and test the code using Ngrok.

Prerequisites

  • Visual Studio 2017 or higher.

  • A Nexmo account, which you can sign up for here.

  • A project setup for this tutorial series, which you can find on Github.

  • Optional: The Nexmo CLI.

Configuration

To use The Nexmo Voice API, we need to create a voice application.
The configuration steps are detailed in [the “Nexmo Voice API with ASP.NET: Before you start” post (https://www.nexmo.com/blog/2017/07/28/nexmo-voice-api-asp-net-configure-dr/).

Once the configuration is done successfully, we are ready to forward a call.

Forwarding a Phone Call

When a call is received, the Nexmo Voice API will make a request to your application to figure out how to respond.

For that purpose, we are going to use NancyFX along side our ASP.NET Core project.

First of all, we need to add Nancy to our project :

To allow Nancy to handle any HTTP requests, we need to tell ASP.NET Core to use Nancy via Owin in the Configure method of Startup.cs.

The next step is to create a Nancy module in which we set up a route to /webhook/answer which will respond with the ncco returned by GetConnectNCCO()

The above code will do the following:

When a call is received, Nexmo will mask the original caller’s number and instead uses a virtual number as a facade to this phone call.

We are done! To test this sample app, some more configuration steps are required.

If you’ve been following so far, you’ve already configured your Nexmo account and created a voice app as shown in this post. We need to link this app to a the Nexmo phone number that we are going to call. If you don’t have a number, you can purchase one using the dashboard or the CLI:

Similarly to link the number, you can use the dashboard or the CLI:

We need to tell Nexmo which URL to make a request to when a call is received – this is called the answer_url. For me, this url is http://localhost:63286/webhook/answer and that’s only running locally.
To expose our webhook answer url, we will use Ngrok.

We now have a new url (http://userSubdomain.ngrok.io) that can be used as the answer_url for the voice application.
Update your application with your new answer_url. It should look like http://userSubdomain.ngrok.io/webhook/answer

Tada! Run the app and give it a go by calling the TO_NUMBER; you will notice that on the other side, you won’t see the phone number you’re using but instead your NEXMO_NUMBER.

Learn More

API References and Tools

Nexmo Getting Started Guides for ASP.NET

Leave a Reply

Your email address will not be published.