Play text-to-speech into a call
This code snippet plays a text-to-speech message into the specified call.
Example
Replace the following variables in the example code:
| Key | Description |
|---|---|
VOICE_CALL_ID | The UUID of the call leg. |
VOICE_TEXT | The text of the message to play into the call. |
VOICE_LANGUAGE | Language to use for the speech - see Supported Languages. |
Prerequisites
Execute the following command at your terminal prompt to create the JWT for authentication:
export JWT=$(nexmo jwt:generate $PATH_TO_PRIVATE_KEY application_id=$NEXMO_APPLICATION_ID)Write the code
Add the following to play-tts-into-a-call.sh:
curl -X PUT https://api.nexmo.com/v1/calls/$VOICE_CALL_ID/talk\
-H "Authorization: Bearer $JWT"\
-H "Content-Type: application/json"\
-d '{"text": "'"$VOICE_TEXT"'", "language": "'"$VOICE_LANGUAGE"'"}'Run your code
Save this file to your machine and run it:
Prerequisites
npm install @vonage/server-sdkCreate a file named play-tts-into-a-call.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});Write the code
Add the following to play-tts-into-a-call.js:
vonage.voice.playTTS(
VOICE_CALL_ID,
{
action: 'talk',
text: VOICE_TEXT,
language: 'en-US',
})
.then((resp) => console.log(resp))
.catch((error) => console.error(error));Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Create a class named PlayTextToSpeechIntoCall and add the following code to the main method:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}Write the code
Add the following to the main method of the PlayTextToSpeechIntoCall class:
val response = client.voice.call(VOICE_CALL_ID).startTalk(VOICE_TEXT) {
language(VOICE_LANGUAGE)
}Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.voice with the package containing PlayTextToSpeechIntoCall:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Create a class named SendTalkToCall and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the SendTalkToCall class:
var payload = TalkPayload.builder(VOICE_TEXT).language(VOICE_LANGUAGE).build();
client.getVoiceClient().startTalk(VOICE_CALL_ID, payload);Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.voice with the package containing SendTalkToCall:
Prerequisites
Install-Package VonageWrite the code
Add the following to PlayTextToSpeech.cs:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);
var command = new TalkCommand() { Text = VOICE_TEXT, Language = "en-US"};
var response = await client.VoiceClient.StartTalkAsync(VOICE_CALL_ID, command);Prerequisites
composer require vonage/clientRun your code
Save this file to your machine and run it:
Prerequisites
pip install vonage python-dotenvWrite the code
Add the following to play-tts-into-call.py:
from vonage import Auth, Vonage
from vonage_voice import CallMessage, TtsStreamOptions
client = Vonage(
Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_PRIVATE_KEY,
)
)
response: CallMessage = client.voice.play_tts_into_call(
uuid=VOICE_CALL_ID,
tts_options=TtsStreamOptions(text=VOICE_TEXT, language=VOICE_LANGUAGE),
)
pprint(response)Run your code
Save this file to your machine and run it:
Prerequisites
gem install sinatra sinatra-contribRun your code
Save this file to your machine and run it:
Try it out
When you run the code, a text-to-speech message is played into the call identified with the specified UUID.
Further Reading
- Conference Calling - This guide explains the two concepts Vonage associates with a call, a leg and a conversation.