How To Build a Voicemail with ASP.NET Core and NancyFX

Published July 08, 2019 by Rabeb Othmani

Have you ever wondered if Meg Ryan would rather listen to Tom Hanks voice instead of simply reading messages?

In my opinion, “Call me Maybe” sounds more exciting than “You’ve got mail”.

So if like me, you’d like to provide someone – your customers perhaps – with a phone number where they can leave you a message, you create your own voicemail powered by 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

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 create a voicemail.

Creating a Voicemail

Similar to the previous blog posts in this series, 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 GetVoiceMailNCCO(). For more details on exactly how to write an ncco check out the NCCO reference in our documentation.

The above code will greet the caller and instruct them when to begin their message.

Once the message is recorded, Nexmo will make a request to the URL in the ‘record’ action which is ‘https://example.com/recording’ currently.

Let us fix that by sending the information about the recording to another route /webhook/voicemail

Some more configuration steps are still required to be able to test the code.

If you’ve been following along 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://.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/answer
Now, you can run the app.

Learn More

API References and Tools

Nexmo Getting Started Guides for ASP.NET

Leave a Reply

Your email address will not be published.