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 Vonage's Voice APIs.
Learning objectives
In this tutorial, we will:
Create an ASP.NET Core app.
Use NancyFX with ASP.NET Core.
Create a Vonage voice application.
Create and return NCCOs.
Run and test the code using Ngrok.
Prerequisites
DT API Account
To complete this tutorial, you will need a DT API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the DT API Dashboard.
Visual Studio 2017 or higher.
A Vonage account, which you can sign up for here.
A project setup for this tutorial series, which you can find on Github.
Optional: The Vonage CLI.
Configuration
To use The Vonage Voice API, we need to create a voice application.
The configuration steps are detailed in the Vonage Voice API with ASP.NET: Before you start post
Once the configuration is done successfully, we are ready to forward a call.
Forwarding a Phone Call
When a call is received, the Vonage Voice API will make a request to your application to figure out how to respond.
For that purpose, we are going to use NancyFX alongside our ASP.NET Core project.
First of all, we need to add Nancy to our project :
PM> Install-Package Nancy
PM> Install-Package Microsoft.AspNetCore.Owin
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
.
using Microsoft.AspNetCore.Builder;
using Nancy.Owin;
namespace NexmoVoiceASPNetCoreQuickStarts
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseOwin(x => x.UseNancy());
}
}
}
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()
using Nancy;
using Vonage.Voice.Nccos;
using Vonage.Voice.Nccos.Endpoints;
namespace NexmoVoiceASPNetCoreQuickStarts
{
public class VoiceModule : NancyModule
{
public VoiceModule()
{
Get["/webhook/answer"] = x => { var response = Response.AsJson(GetConnectNCCO());
response.ContentType = "application/json";
return response;
};
}
private Ncco GetConnectNCCO()
{
var ncco = new Ncco();
ncco.Actions.Add(new ConnectAction
{
From = "VONAGE_NUMBER",
Endpoint = new[] {
new PhoneEndpoint
{
Number = "TO_NUMBER"
}
}
});
return ncco;
}
}
}
The above code will do the following:
When a call is received, Vonage will mask the original caller's number and instead use 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 Vonage account and created a voice app as shown in this post. We need to link this app to a Vonage 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:
vonage nubmers:search US
vonage numbers:buy [NUMBER] [COUNTRYCODE]
Similarly to link the number, you can use the dashboard or the CLI:
vonage apps:link --number=VONAGE_NUMBER APP_ID
We need to tell Vonage 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.
ngrok http 63286
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 VONAGE_NUMBER.
Learn More
API References and Tools
Nexmo Getting Started Guides for ASP.NET
Rabeb was a Developer Advocate at Nexmo focusing on cloud communication APIs and helping developers get the best experience possible when building their apps. Other than writing code for a living, Rabeb advocates for bringing more women and minorities into tech, thus her involvement with Women Who Code and different tech communities. She leads the Women Who Code Network in Bristol.