How to Handle User Input With ASP.NET Core

Published January 10, 2019 by Rabeb Othmani

This is the fourth tutorial on how to use Voice APIs with ASP.NET series.
In the previous tutorial, we learnt how to Receive a Phone Call with Nexmo Voice API, ASP.NET Core and NancyFX. This is a great start, but in a real life scenario we expect some sort of interaction with the caller. Maybe they will be prompted to pick an option or enter a PIN. We need a way to handle user input.

That’s exactly the aim of this tutorial; we will create an ASP.NET app that handles inbound voice calls and respond to user input using the Nexmo Voice API.

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 NCCOs.

  • Handle user input.

  • Run and test the code using Ngrok.

Prerequisites

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 configuration is done successfully, we are ready to receive an inbound call and handle user input with The Nexmo Voice API!

Receiving a phone call and handling input with ASP.NET

When a call is received, the Nexmo Voice API will make a request to your application to figure out how to respond.
In this post we will be using Text-To-Speech to greet the caller, and the input action to collect DTMF input from the caller.

DTMF (Dual Tone Multi Frequency) is a form of signalling used by phone systems to transmit the digits 0-9 and the * and # characters. Typically a caller presses these buttons on their telephone keypad and the phone then generates a tone made up of two frequencies played simultaneously (hence Dual Tone).

To capture DTMF in our applicatoin, 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 setup a route to /webhook/answer which will respond with the ncco returned by GetDTMFNCCO()

The above code will do the following:

When a call is received, Nexmo will prompt the user to press any key by reading out the text “Hello. Please press any key to continue.”. When the user presses a key, we pass that input to webhook/dtmf where it will be handled properly.
To handle the DTMF, we need to tell the Nancy module how to respond once the request is received. In this case, we will simply read back to the caller which key they pressed.

We are done! To test this sample app, 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 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 (mine is http://5e18af56.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://[id].ngrok.io/webhook/answerTada! Run the app and give it a go by calling the Nexmo number you purchased.

Learn more

API References and Tools

Nexmo Getting Started Guides for ASP.NET

Leave a Reply

Your email address will not be published.