In Context

The next-generation communications blog from Nexmo

< Back

How to make a Text-to-Speech phone call in ASP.NET

July 28, 2017 Published by

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

Building high quality voice applications is now made easy with The Nexmo Voice API. It allows you to make and receive phone calls with your ASP.NET applications among other things.
In this tutorial, we will see how to make a Text-to-Speech phone call with ASP.NET.

The Nexmo Voice API makes it so easy to make a call. You send an authenticated call request to the API. Once it’s accepted, Nexmo makes a GET request to retrieve an NCCO (Nexmo Call Control Objects)and executes the actions within that NCCO.

Prerequisites

  • Visual Studio 2017
  • Nexmo Account. You can sign up here.
  • A project set up for this tutorial series which you can find on Github.
  • Optional: Nexmo CLI

Configuration

In order to be able to use The Nexmo Voice API , you’ll have to create a voice application.

Before diving into the code, you need to follow the configuration steps details in the “Nexmo Voice API with ASP.NET: Before you start” post.
Once the configuration is done successfully, you are ready to make a Text-to-Speech phone call with The Nexmo Voice API!

Making a Text-to-Speech call with The Nexmo voice API

Congratulations! You made it so far which means you managed to configure your voice application and you created an ASP.NET project.

In appsettings.json file, make sure to initialize Nexmo with your API credentials as well as the app ID and private key you just created.

Then create a new controller called VoiceController.cs in which create an action method called MakeCall.

Above the method, add a HttpGetAttribute to allow the user to navigate to the corresponding view.

Afterwards, click on the Views folder and add a new folder called Voice. Within this folder, create a new view (`MakeCall.cshtml’). Then, add a form to the view with an input tag (type = “text”) for the destination number. Lastly, add an input tag (type = “submit”) to submit the form.
Back to the VoiceController, add the following using statement to the top of the file.

Add another action method named MakeCall with a string parameter: to.

Within this method, you will make a call using the parameter as the to. The from number is your Nexmo virtual number (retrieved from the appsettings.json), the answer_url is the NCCO ( Nexmo Call Control Object) which is a JSON array used to control the flow of your call.

You can use one of the Nexmo’s community NCCO examples or create your own and host it somewhere reachable by The Nexmo Voice API. An easy hosting solution is GitHub Gist.

Since we are trying to make a Text-to-Speech call in this demo, the action required for the NCCO is Talk , you also need to provide the text to be synthesised into speech in the call and the last thing to provide is a voice name. Nexmo provides a list a voiceNames in different languages and accents for both genders.

In my NCCO below, I chose the voiceName to be “Amy” which is a female British-english voice.

Now back to MakeCall method, make sure NEXMO_CALL_ANSWER_URL points to your NCCO json file.

Now, let’s run the app and make a Text-to-Speech phone call.

When it is successful, it retrieves the NCCO from your webhook, executes the actions, and terminates the call.

You may observe some delay before your phone rings depending on your phone carrier.

Learn more

API References and Tools

Nexmo Getting Started Guide for ASP.NET

Tags: , , , , , ,

Categorised in: , , ,

This post was written by