Rabeb Othmani

Vonage Team Member

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

[Read more]
< Tutorial />

How to Make a Text-to-Speech Phone Call in ASP.NET

Last updated on Dec 09, 2021

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

Building a high-quality voice application is now made easy with The Vonage 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.

View on GitHub

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

Flow of an outbound call
Flow of an outbound call

Prerequisites

  • Visual Studio 2017
  • A project set up for this tutorial series which you can find on Github
  • Optional: Vonage CLI

Vonage API Account

To complete this tutorial, you will need a Vonage 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 Vonage API Dashboard.

This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs.

Screenshot of new Meetings API session in progress
Start developing in minutes with free credits on us. No credit card required!

Configuration

In order to be able to use The Vonage 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 “Vonage 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 Vonage Voice API!

Making a Text-to-Speech call with The Vonage 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 Vonage with your API credentials as well as the app ID and private key you just created.

{
 "appSettings": 
 {
  "Vonage.UserAgent": "NEXMOQUICKSTART/1.0",
  "Vonage_key": "NEXMO-API-KEY",
  "Vonage_secret": "NEXMO-API-SECRET",
  "VONAGEFROMNUMBER": "NEXMO-VIRTUAL-NUMBER",
  "Vonage.Application.Id": "NEXMO-APP-ID",
  "Vonage.Application.Key": "PATH:\TO\private.key"
 }
}

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.

[HttpGet]
public ActionResult MakeCall()
{
   Return 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.

using Vonage;
using Vonage.Request;

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 Vonage’s community NCCO examples or create your own and host it somewhere reachable by The Vonage 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.

[
  {
    "action": "talk",
    "voiceName": "Amy",
    "text": "Hi, this is Amy. You are listening to a Call made with the Vonage Voice API"
  }
]

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

[HttpPost]
public ActionResult MakeCall(string to)
{
    var VONAGE_FROM_NUMBER = Configuration.Instance.Settings["appsettings:VONAGE_FROM_NUMBER"];
    var VONAGE_TO_NUMBER = to;
    var VONAGE_CALL_ANSWER_URL = "https://nexmo-community.github.io/ncco-examples/first_call_talk.json";

    var VoiceClient = new VonageClient(new Credentials()).VoiceClient;

    var callCommand = new CallCommand
    {
        To = new[]
        {
            new PhoneEndpoint {
                Number = VONAGE_TO_NUMBER
            }
        },
        From = new PhoneEndpoint
        {
            Number = VONAGE_FROM_NUMBER
        },
        AnswerUrl = new[] { VONAGE_CALL_ANSWER_URL }
    };

    var callResponse = await VoiceClient.CreateCallAsync(callCommand);

    var result = new HttpStatusCodeResult(200);

    return RedirectToAction("Index", "Voice");
}

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

Vonage Getting Started Guide for ASP.NET