这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。

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 into which to play the text-to-speech message.
TEXT text of the message to play into the call.

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Generate your JWT

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:

Copy to Clipboard
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"}'

View full source

Run your code

Save this file to your machine and run it:

bash play-tts-into-a-call.sh

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

npm install nexmo
Initialize your dependencies

Create a file named play-tts-into-call.js and add the following code:

Copy to Clipboard
const Nexmo = require('nexmo');

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_PRIVATE_KEY
}, {debug: true});

View full source

Write the code

Add the following to play-tts-into-call.js:

Copy to Clipboard
const TEXT = 'This is some sample text to speech text. It could go on and on and never end.';

nexmo.calls.talk.start(UUID, { text: TEXT, voice_name: 'Emma', loop: 0 }, (err, res) => {
  if(err) { console.error(err); }
  else {
      console.log(res);
  }
});

View full source

Run your code

Save this file to your machine and run it:

node play-tts-into-call.js

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

Add the following to `build.gradle`:

compile 'com.nexmo:client:5.2.1'
Initialize your dependencies

Create a class named SendTalkToCall and add the following code to the main method:

Copy to Clipboard
NexmoClient client = NexmoClient.builder()
        .applicationId(NEXMO_APPLICATION_ID)
        .privateKeyPath(NEXMO_PRIVATE_KEY_PATH)
        .build();

View full source

Write the code

Add the following to the main method of the SendTalkToCall class:

Copy to Clipboard
client.getVoiceClient().startTalk(UUID, TEXT, VoiceName.KIMBERLY, 0);

View full source

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.nexmo.quickstart.voice with the package containing SendTalkToCall:

gradle run -Pmain=com.nexmo.quickstart.voice.SendTalkToCall

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

Install-Package Nexmo.Csharp.Client
Import dependencies

Create a file named PlayTextToSpeech.cs and add the following code:

Copy to Clipboard
using Nexmo.Api;
using Nexmo.Api.Request;
using Nexmo.Api.Voice;

View full source

Initialize your dependencies

Add the following to PlayTextToSpeech.cs:

Copy to Clipboard
var credentials = Credentials.FromAppIdAndPrivateKeyPath(NEXMO_APPLICATION_ID, NEXMO_PRIVATE_KEY_PATH);
var client = new NexmoClient(credentials);

View full source

Write the code

Add the following to PlayTextToSpeech.cs:

Copy to Clipboard
var command = new TalkCommand() { Text = TEXT};

var response = client.VoiceClient.StartTalk(UUID, command);

View full source

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

composer require nexmo/client
Initialize your dependencies

Create a file named index.php and add the following code:

Copy to Clipboard
$keypair = new \Nexmo\Client\Credentials\Keypair(
    file_get_contents(NEXMO_APPLICATION_PRIVATE_KEY_PATH),
    NEXMO_APPLICATION_ID
);

$client = new \Nexmo\Client($keypair);

View full source

Write the code

Add the following to index.php:

Copy to Clipboard
$talk = $client->calls[UUID]->talk();
$talk->setText(TEXT);
$talk->setVoiceName('Kimberly');
$talk->put();

View full source

Run your code

Save this file to your machine and run it:

php index.php

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

pip install nexmo
Initialize your dependencies

Create a file named play-tts-into-call.py and add the following code:

Copy to Clipboard
import nexmo


client = nexmo.Client(
    application_id=APPLICATION_ID,
    private_key=APPLICATION_PRIVATE_KEY_PATH,
)

View full source

Write the code

Add the following to play-tts-into-call.py:

Copy to Clipboard
client.send_speech(NEXMO_CALL_UUID, text='Hello from Nexmo')

View full source

Run your code

Save this file to your machine and run it:

python3 play-tts-into-call.py

Prerequisites

Use your existing application

Modifying an existing call requires that the UUID provided is a currently active call. To modify a call, you must use the same NEXMO_APPLICATION_ID and private key that were used to create the call.

Install dependencies

gem install sinatra sinatra-contrib
Initialize your dependencies

Create a file named play-text-to-speech-into-a-call.rb and add the following code:

Copy to Clipboard

client = Nexmo::Client.new(
  application_id: NEXMO_APPLICATION_ID,
  private_key: File.read(NEXMO_APPLICATION_PRIVATE_KEY_PATH)

View full source

Write the code

Add the following to play-text-to-speech-into-a-call.rb:

Copy to Clipboard

response = client.voice.talk.start(UUID, text: 'Hello from Nexmo', voice_name: "Kimberly")

View full source

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.