这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。
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 |
---|---|
UUID |
The UUID of the call leg |
TEXT |
The text of the message to play into the call. |
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
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/$UUID/talk\
-H "Authorization: Bearer "$JWT\
-H "Content-Type: application/json"\
-d '{"text": "Hello, hi there"}'
Run your code
Save this file to your machine and run it:
bash play-tts-into-a-call.sh
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
npm install @vonage/server-sdk
Create a file named play-tts-into-call.js
and add the following code:
const Vonage = require('@vonage/server-sdk');
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY
}, {debug: true});
Write the code
Add the following to play-tts-into-call.js
:
const TEXT = 'This is some sample text to speech text. It could go on and on and never end.';
vonage.calls.talk.start(UUID, { text: TEXT, voice_name: 'Emma', loop: 0 }, (err, res) => {
if(err) { console.error(err); }
else {
console.log(res);
}
});
Run your code
Save this file to your machine and run it:
node play-tts-into-call.js
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
Add the following to `build.gradle`:
compile 'com.vonage:client:6.2.0'
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:
final String ANSWER_URL = "https://nexmo-community.github.io/ncco-examples/silent-loop.json";
CallEvent call = client
.getVoiceClient()
.createCall(new Call(TO_NUMBER, VONAGE_NUMBER, ANSWER_URL));
Thread.sleep(5000);
final String UUID = call.getUuid();
final String TEXT = "Hello World! Would you like to know more? I bet you would.";
client.getVoiceClient().startTalk(UUID,TEXT, TextToSpeechLanguage.AMERICAN_ENGLISH);
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
:
gradle run -Pmain=com.vonage.quickstart.voice.SendTalkToCall
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
Install-Package Vonage
Create a file named PlayTextToSpeech.cs
and add the following code:
using Vonage;
using Vonage.Request;
using Vonage.Voice;
Add the following to PlayTextToSpeech.cs
:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);
Write the code
Add the following to PlayTextToSpeech.cs
:
var command = new TalkCommand() { Text = TEXT};
var response = client.VoiceClient.StartTalk(UUID, command);
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
composer require vonage/client
Create a file named index.php
and add the following code:
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);
Write the code
Add the following to index.php
:
$client->voice()->playTTS(
UUID,
new \Vonage\Voice\NCCO\Action\Talk(TEXT)
);
Run your code
Save this file to your machine and run it:
php index.php
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
pip install vonage
Create a file named play-tts-into-call.py
and add the following code:
client = vonage.Client(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)
Write the code
Add the following to play-tts-into-call.py
:
voice = vonage.Voice(client)
voice.send_speech(VONAGE_CALL_UUID, text='Hello from Vonage')
Run your code
Save this file to your machine and run it:
python3 play-tts-into-call.py
Prerequisites
Modifying an existing call requires that the UUID
provided is a currently active call. To modify a call, you must use the same VONAGE_APPLICATION_ID
and private key that were used to create the call.
gem install sinatra sinatra-contrib
Create a file named play-text-to-speech-into-a-call.rb
and add the following code:
client = Vonage::Client.new(
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
Write the code
Add the following to play-text-to-speech-into-a-call.rb
:
response = client.voice.talk.start(UUID, text: 'Hello from Vonage', language: 'en-US', style: 0)
Run your code
Save this file to your machine and run it:
ruby play-text-to-speech-into-a-call.rb
Try it out
When you run the code a text-to-speech message is played into the call identified with the specified UUID.