这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。
语音 API 概述
Nexmo 语音 API 是在云中构建高质量语音应用程序的最简单方法。使用语音 API,您可以:
- 使用您已经在使用的网络技术构建可扩展的应用
- 使用 Nexmo 呼叫控制对象 (NCCO) 控制 JSON 中的入站和出站呼叫流程
- 录制和存储呼入或呼出电话
- 创建电话会议
- 以 40 种不同性别和口音的语言发送文本到语音消息
目录
在本文档中,您可以了解:
- Nexmo 语音 API 概念介绍术语
- 如何开始使用语音 API,包括您所用语言的示例
- 指南 了解有关使用语音 API 的信息
- 代码片段 帮助完成特定任务的代码片段
- 用例 带有代码示例的详细用例
- 参考 API 文档和其他支持内容
概念
使用 JWT 进行身份验证 - 与语音 API 的互动使用 JWT(JSON 网络令牌)进行身份验证。Nexmo 库使用唯一的 Nexmo 语音应用程序 ID 和私钥来处理 JWT 生成。有关更多信息,请参阅对应用程序进行身份验证。
Nexmo 语音应用程序 - Nexmo 语音应用程序代表您正在构建的应用程序的一对一映射。它们包含虚拟号码和 Webhook 回调 URL 等配置。您可以使用 Nexmo Dashboard、Nexmo CLI 或通过应用程序 API 创建 Nexmo 语音应用程序。
NCCO - Nexmo 呼叫控制对象是一组指示 Nexmo 如何控制对 Nexmo 应用程序的呼叫的操作。例如,您可以
connect
呼叫,使用talk
发送合成语音,stream
音频,或record
呼叫。它们以 JSON 形式表示为对象数组。有关更多信息,请参阅 NCCO 参考。号码 - Nexmo 语音 API 中使用电话号码的关键概念。
Webhook - 向您的应用程序 Web 服务器发出 HTTP 请求,以便您可以对它们进行操作。例如,来电将发送 Webhook。
入门
语音 Playground
在 Nexmo Dashboard 中,您可以在语音 Playground 中交互式地试用语音 API。注册 Nexmo 帐户后,您可以转到 Dashboard 中的语音 Playground(语音 ‣ 语音 Playground)。
更多详细信息,请参阅此博客文章:了解语音 Playground,您的 Nexmo 语音应用测试沙箱
使用 API
与 Nexmo 语音平台进行交互的主要方式是通过公共 API 。要发出呼出电话,您需要向 https://api.nexmo.com/v1/calls
发出 POST
请求。
为了简化这一过程,Nexmo 提供了各种语言的服务器 SDK,负责身份验证并为您创建正确的请求主体。
首先,请在下面选择您的语言,然后在示例代码中替换以下变量:
密钥 | 说明 |
---|---|
NEXMO_NUMBER |
您拨打电话的 Nexmo 号码。示例:447700900000 。 |
TO_NUMBER |
您要以 E.164 格式拨打的号码。示例:447700900001 。 |
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
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 make-an-outbound-call.sh
:
curl -X POST https://api.nexmo.com/v1/calls\
-H "Authorization: Bearer "$JWT\
-H "Content-Type: application/json"\
-d '{"to":[{"type": "phone","number": "$TO_NUMBER"}],
"from": {"type": "phone","number": "$VONAGE_NUMBER"},
"answer_url":["https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json"]}'
Run your code
Save this file to your machine and run it:
bash make-an-outbound-call.sh
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
npm install @vonage/server-sdk
Create a file named make-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_APPLICATION_PRIVATE_KEY_PATH
})
Write the code
Add the following to make-call.js
:
const ANSWER_URL = 'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'
vonage.calls.create({
to: [{
type: 'phone',
number: TO_NUMBER
}],
from: {
type: 'phone',
number: VONAGE_NUMBER
},
answer_url: [ANSWER_URL]
}, (error, response) => {
if (error) console.error(error)
if (response) console.log(response)
})
Run your code
Save this file to your machine and run it:
node make-call.js
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Add the following to `build.gradle`:
compile 'com.vonage:client:5.5.0'
Create a class named OutboundTextToSpeech
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 OutboundTextToSpeech
class:
final String ANSWER_URL = "https://nexmo-community.github.io/ncco-examples/talk.json";
client.getVoiceClient().createCall(new Call(TO_NUMBER, VONAGE_NUMBER, ANSWER_URL));
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 OutboundTextToSpeech
:
gradle run -Pmain=com.vonage.quickstart.voice.OutboundTextToSpeech
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install-Package Vonage
Create a file named MakeOutboundCall.cs
and add the following code:
using Vonage;
using Vonage.Request;
using Vonage.Voice;
using Vonage.Voice.Nccos.Endpoints;
Add the following to MakeOutboundCall.cs
:
var creds = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(creds);
Write the code
Add the following to MakeOutboundCall.cs
:
var command = new CallCommand() { To = new Endpoint[] { toEndpoint }, From = fromEndpoint, AnswerUrl = new[] { ANSWER_URL } };
var response = client.VoiceClient.CreateCall(command);
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
composer require vonage/client
Create a file named text-to-speech-outbound.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 text-to-speech-outbound.php
:
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone(TO_NUMBER),
new \Vonage\Voice\Endpoint\Phone(VONAGE_NUMBER)
);
$outboundCall->setAnswerWebhook(
new \Vonage\Voice\Webhook(
'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json',
\Vonage\Voice\Webhook::METHOD_GET
)
);
$response = $client->voice()->createOutboundCall($outboundCall);
var_dump($response);
Run your code
Save this file to your machine and run it:
php text-to-speech-outbound.php
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
pip install vonage
Create a file named make-an-outbound-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 make-an-outbound-call.py
:
voice = vonage.Voice(client)
response = voice.create_call({
'to': [{'type': 'phone', 'number': TO_NUMBER}],
'from': {'type': 'phone', 'number': VONAGE_NUMBER},
'answer_url': ['https://developer.nexmo.com/ncco/tts.json']
})
pprint(response)
Run your code
Save this file to your machine and run it:
python3 make-an-outbound-call.py
Prerequisites
A Vonage application contains the required configuration for your project. You can create an application using the Nexmo CLI (see below) or via the dashboard. To learn more about applications see our Vonage concepts guide.
Install the CLI
npm install -g nexmo-cli
Create an application
Once you have the CLI installed you can use it to create a Vonage application. Run the following command and make a note of the application ID that it returns. This is the value to use in NEXMO_APPLICATION_ID
in the example below. It will also create private.key
in the current directory which you will need in the Initialize your dependencies step
Vonage needs to connect to your local machine to access your answer_url
. We recommend using ngrok to do this. Make sure to change demo.ngrok.io
in the examples below to your own ngrok URL.
nexmo app:create "Outbound Call code snippet" https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json http://demo.ngrok.io/webhooks/events --keyfile private.key
gem install vonage
Create a file named outbound_tts_call.rb
and add the following code:
client = Vonage::Client.new(
api_key: VONAGE_API_KEY,
api_secret: VONAGE_API_SECRET,
application_id: VONAGE_APPLICATION_ID,
private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
)
Write the code
Add the following to outbound_tts_call.rb
:
response = client.voice.create(
to: [{
type: 'phone',
number: TO_NUMBER
}],
from: {
type: 'phone',
number: VONAGE_NUMBER
},
answer_url: [
'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'
]
)
puts response.inspect
Run your code
Save this file to your machine and run it:
ruby outbound_tts_call.rb
指南
- Call Flow: The various stages of a call and how they interact.
-
Nexmo Call Control Objects: To tell Vonage how to handle a phone call, you must provide a Nexmo Call Control Object (NCCO) when a call is placed or answered. There are various actions available, such as
talk
,input
andrecord
. - Legs and Conversations: When a phone call is made or received by Vonage it is added to a conversation. A single conversation contains one or more phone calls (sometimes referred to as legs).
- Numbers: Numbers are a key part of using the Vonage Voice API. This guide covers number formatting, outgoing caller IDs and incoming call numbers.
- Text to Speech: Using our Text-To-Speech engine, you can play machine-generated speech to your callers
- Customizing Spoken Text: Use Speech Synthesis Markup Language (SSML) to control how text-to-speech is read out
- Recording: Recording audio input from a caller or recording the conversation between two callers.
-
Endpoints: When connecting a call, you can connect to another phone number, a
sip
endpoint or awebsocket
. These are known as endpoints. - DTMF: Capture user input by detecting DTMF tones (button presses) during a call.
- Speech Recognition: Capture user input by converting user speech to text form during a call.
- WebSockets: You can connect the audio of a call to a websocket to work with it in real time.
- Signed Webhooks: A method for your application to verify a request is coming from Vonage.
- Contact Center Intelligence: Learn how to enhance your contact center solution with Voice API.
代码片段
- Before you begin
- Make an outbound call with an NCCO
- Connect an inbound call
- Download a recording
- Earmuff a call
- Handle user input with DTMF
- Handle user input with ASR
- Connect callers into a conference
- Make an outbound call
- Mute a call
- Play an audio stream into a call
- Play DTMF into a call
- Play text-to-speech into a call
- Receive an inbound call
- Record a call with split audio
- Record a call
- Record a conversation
- Record a message
- Retrieve information for a call
- Retrieve information for all calls
- Transfer a call
- Transfer a call with inline NCCO
- Track NCCO progress
用例
- Interactive Voice Response (IVR)
- Local Numbers
- Call Recording Transcription
- Private Voice Communication
- Call Whisper
- Voice-based Critical Alerts
- Voice Bot
- Call Tracking