How to Receive a Phone Call with Nexmo Voice API, ASP.NET Core and NancyFX

Published November 21, 2018 by Rabeb Othmani

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

In previous tutorials, we learned how to make a Text-to-Speech phone call with ASP.NET and how to Play Audio to a Caller in ASP.NET Core.
But how about receiving calls? The good news is the Nexmo Voice API handles inbound calls as well.

Inbound calls are calls made to a Nexmo number from another regular phone anywhere in the world. Both inbound and outbound calls follow the same call flow once answered. This call flow is controlled by an NCCO.

In this tutorial, we will create an ASP.NET app that handles inbound voice calls and returns a dynamic response.

Learning objectives

In this tutorial, we will:

  • Create an ASP.NET Core app.
  • Use NancyFX with ASP.NET Core.
  • Create a Nexmo voice application.
  • Receive inbound calls within the app.
  • Create and return an NCCO.
  • Run and test the code using Ngrok.

Prerequisite

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.

Once the application is configured successfully, we are ready to receive an inbound call with The Nexmo Voice API!

Receiving a phone call with ASP.NET

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

To achieve this, we are going to use NancyFX along side our ASP.NET Core project.

Nancy is a lightweight open source framework that promotes the “super-duper-happy-path”. This means that it has sensible defaults and conventions and tries to stay our of our way as much as possible.

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.

We are all good to go! The next step is to create a Nancy module to handle any requests to /webhook/answer.

I’m using Postman to test, and as you can see our /webhook/answer route is returning exactly what’s expected.


This is a great start, but Nexmo doesn’t know what to do with that string. To properly respond to the call, we need to return an NCCO.

GetInboundNCCO() will create an NCCO object that will use Text-To-Speech to read the caller’s phone number back to them using the talk action within the NCCO.

We are accessing the phone number via the from param in the request.


That’s all the code we need. To test this properly, some more configuration steps are required.

If you’ve been following up 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 Nexmo phone number, the number we will be calling. 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 (answer_url). For me, this url is http://localhost:63286/webhook/answer and that’s only running locally.

To expose our answer_url, we will use our good friend Ngrok.

This will return a new URL (mine is http://5e18af56.ngrok.io) that can be used as the answer_url for the voice application. Update your answer_url to http://[id].ngrok.io/webhook/answer

Tada! Run the app and give it a go by calling the Nexmo number you purchased. It should thank you for calling, then read out your phone number.

Learn more

API References and Tools

Nexmo Getting Started Guides for ASP.NET

Leave a Reply

Your email address will not be published.