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

语音 API 概述

Nexmo 语音 API 是在云中构建高质量语音应用程序的最简单方法。使用语音 API,您可以:

  • 使用您已经在使用的网络技术构建可扩展的应用
  • 使用 Nexmo 呼叫控制对象 (NCCO) 控制 JSON 中的入站和出站呼叫流程
  • 录制和存储呼入或呼出电话
  • 创建电话会议
  • 以 40 种不同性别和口音的语言发送文本到语音消息

目录

在本文档中,您可以了解:

概念

  • 使用 JWT 进行身份验证 - 与语音 API 的互动使用 JWT(JSON 网络令牌)进行身份验证。Nexmo 库使用唯一的 Nexmo 语音应用程序 ID 和私钥来处理 JWT 生成。有关更多信息,请参阅对应用程序进行身份验证

  • Nexmo 语音应用程序 - Nexmo 语音应用程序代表您正在构建的应用程序的一对一映射。它们包含虚拟号码和 Webhook 回调 URL 等配置。您可以使用 Nexmo DashboardNexmo 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

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
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 make-an-outbound-call.sh:

Copy to Clipboard
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": "'$NEXMO_NUMBER'"},
      "answer_url":["https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json"]}'

View full source

Run your code

Save this file to your machine and run it:

bash make-an-outbound-call.sh

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

npm install nexmo
Initialize your dependencies

Create a file named make-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_APPLICATION_PRIVATE_KEY_PATH
})

View full source

Write the code

Add the following to make-call.js:

Copy to Clipboard
const ANSWER_URL = 'https://developer.nexmo.com/ncco/tts.json'

nexmo.calls.create({
  to: [{
    type: 'phone',
    number: TO_NUMBER
  }],
  from: {
    type: 'phone',
    number: NEXMO_NUMBER
  },
  answer_url: [ANSWER_URL]
}, (error, response) => {
  if (error) console.error(error)
  if (response) console.log(response)
})

View full source

Run your code

Save this file to your machine and run it:

node make-call.js

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

Add the following to `build.gradle`:

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

Create a class named OutboundTextToSpeech 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 OutboundTextToSpeech class:

Copy to Clipboard
client.getVoiceClient().createCall(new Call(TO_NUMBER, NEXMO_NUMBER, ANSWER_URL));

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 OutboundTextToSpeech:

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

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

Install-Package Nexmo.Csharp.Client
Import dependencies

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

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

View full source

Initialize your dependencies

Add the following to MakeOutboundCall.cs:

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

View full source

Write the code

Add the following to MakeOutboundCall.cs:

Copy to Clipboard
var command = new CallCommand() { To = new Endpoint[] { toEndpoint }, From = fromEndpoint, AnswerUrl = new[] { ANSWER_URL } };
var response = client.VoiceClient.CreateCall(command);

View full source

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
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
$call = $client->calls()->create([
    'to' => [[
        'type' => 'phone',
        'number' => TO_NUMBER
    ]],
    'from' => [
        'type' => 'phone',
        'number' => NEXMO_NUMBER
    ],
    'answer_url' => ['https://developer.nexmo.com/ncco/tts.json'],
]);

View full source

Run your code

Save this file to your machine and run it:

php -t . -S localhost:3000

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

pip install nexmo
Initialize your dependencies

Create a file named make-an-outbound-call.py and add the following code:

Copy to Clipboard
import nexmo
from pprint import pprint

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

View full source

Write the code

Add the following to make-an-outbound-call.py:

Copy to Clipboard
response = client.create_call({
  'to': [{'type': 'phone', 'number': TO_NUMBER}],
  'from': {'type': 'phone', 'number': NEXMO_NUMBER},
  'answer_url': ['https://developer.nexmo.com/ncco/tts.json']
})

pprint(response)

View full source

Run your code

Save this file to your machine and run it:

python3 make-an-outbound-call.py

Prerequisites

Create an application

A Nexmo 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 Nexmo 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 Nexmo 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

Nexmo 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://developer.nexmo.com/ncco/tts.json http://demo.ngrok.io/webhooks/events --keyfile private.key
Install dependencies

gem install nexmo
Initialize your dependencies

Create a file named outbound_tts_call.rb and add the following code:

Copy to Clipboard
client = Nexmo::Client.new(
  api_key: NEXMO_API_KEY,
  api_secret: NEXMO_API_SECRET,
  application_id: NEXMO_APPLICATION_ID,
  private_key: File.read(NEXMO_APPLICATION_PRIVATE_KEY_PATH)
)

View full source

Write the code

Add the following to outbound_tts_call.rb:

Copy to Clipboard
response = client.voice.create(
  to: [{
    type: 'phone',
    number: TO_NUMBER
  }],
  from: {
    type: 'phone',
    number: NEXMO_NUMBER
  },
  answer_url: [
    'https://raw.githubusercontent.com/nexmo-community/ncco-examples/gh-pages/text-to-speech.json'
  ]
)

puts response.inspect

View full source

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 Objects (NCCO) when a call is placed or answered. There are various actions available, such as talk, input and record.
  • Legs and Conversations: When a phone call is made or received by Nexmo 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 Nexmo 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 a websocket. 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.

代码片段

用例

参考