Introduction
In a previous tutorial, we showed you how to Receive a Phone Call with Java and respond using Text-to-Speech. In addition to receiving a phone call, you can also make outgoing phone calls.
In this tutorial, you will create an application that can make outgoing text-to-speech phone calls utilizing Java and the Nexmo Voice AP一世.
Prerequisites
To work through this tutorial, you will need a Nexmo account. Sign up now if you don't already have an account.
You will be using Gradle to manage your dependencies and run your application. Additionally, you'll need to make sure you have a copy of the JDK installed. I will be using JDK 11, which is the current LTS, in this tutorial.
Finally, you'll need the Nexmo CLI installed. You'll use it to purchase a phone number and configure your Nexmo account to point at your new application.
Make Text-to-Speech Phone Call with Java
本教程将指导您完成以下步骤:
- 使用Nexmo CLI购买电话号码并创建应用程序。使用Gradle初始化新的Java应用程序。使用Nexmo Java客户端库发起电话并执行文本到语音转换。
Purchasing a Number
您将需要一个Nexmo号码来发送电话。 如果您没有电话号码,可以使用Nexmo CLI购买:
nexmo number:buy --country_code US
记下您刚购买的号码。 您将需要此号码来链接您的应用程序,稍后在编写代码以创建电话时将使用它。
Configure Your Nexmo Account
如果没有应用程序,则可以使用Nexmo CLI创建一个应用程序。 您将需要定义应用程序的名称,以及默认情况下Voice API将使用的答案和事件URL:
- 当链接到您应用程序的电话号码接到电话时,Nexmo Voice API会向您的答案URL发出请求。发生各种状态变化时,Nexmo语音API会向您的事件URL发出请求。
To learn more about applications see our Nexmo Concepts Guide.
使用以下命令通过Nexmo CLI创建应用程序:
nexmo app:create "Send TTS Call" http://example.com/webhooks/answer http://example.com/webhooks/events --keyfile private.key
此命令还将创建一个名为私钥您需要使用Nexmo Voice API进行身份验证才能进行呼叫。 该文件将保存到您在其中运行命令的目录中。 还要记下返回的应用程序ID,因为以后将需要它。
Using Gradle to Set Up a New Java Project
您将使用Gradle来管理依赖关系以及创建和运行Java应用程序。
的gradle init --type = java应用程序命令将创建您需要的所有文件夹以及用于编写代码的示例类。
在命令行中,使用以下命令创建一个新的Java项目,并在交互式提示符下接受默认值:
mkdir make-tts-call
cd make-tts-call
gradle init --type java-application
Gradle将创建应用程式中的课程src / 主要 / java / make / tts / call夹。 在这堂课里面是getGreeting和主要方法。 您将不需要getGreeting方法,因此随时将其删除。
你的应用程式类应如下所示:
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package make.tts.call;
public class App {
public static void main(String[] args) {
// Future Code Goes Here
}
}
Add the Dependencies
您将使用Nexmo Java库与Nexmo语音API通信。 将以下内容添加到依存关系封锁你的build.gradle文件:
// Nexmo Java Client
implementation 'com.nexmo:client:4.2.0'
Initialize the Nexmo Client
Nexmo Java库包含一个NexmoClient类,使您可以访问各种Nexmo API。 您将使用您的应用程序ID和您的路径私钥上一步中的文件。 的NexmoClient将使用此信息对Nexmo语音API进行身份验证。
将以下内容添加到主要的方法应用程式类,解决所有导入问题:
NexmoClient nexmoClient = NexmoClient.builder()
.applicationId(APPLICATION_ID)
.privateKeyPath(PRIVATE_KEY_PATH)
.build();
VoiceClient voiceClient = nexmoClient.getVoiceClient();
的NexmoClient抛出例外如果无法加载私钥文件。 为了方便起见,请修改您的主要方法签名引发任何异常。 你的主要方法现在应如下所示:
public static void main(String[] args) throws Exception {
NexmoClient nexmoClient = NexmoClient.builder()`
.applicationId(APPLICATION_ID)
.privateKeyPath(PRIVATE_KEY_PATH)
.build();
VoiceClient voiceClient = nexmoClient.getVoiceClient();
}
Build the Nexmo Call Control Object
The Nexmo Voice API is controlled using Nexmo Call Control Object (NCCO). An NCCO is a JSON array which contains a set of actions that the Voice API will execute.
以下NCCO将指示Nexmo语音API在接听电话时与收件人通话:
[
{
"action": "talk",
"text": "This is a text-to-speech call from Nexmo"
}
]
Nexmo Java客户端库提供了允许您构造NCCO的类。 您将使用尼科和对话动作类来构建NCCO。
将以下内容添加到主要方法,解决所有导入问题:
TalkAction intro = TalkAction.builder("This is a text-to-speech call from Nexmo").build();
Ncco ncco = new Ncco(intro);
Make the Phone Call
的语音客户端包含一个称为create呼叫期望com.nexmo.client.voice.呼叫. 的呼叫对象用于定义您从中拨打的电话号码,要呼叫的接收者以及由NCCO控制呼叫的方式。
创建一个新的呼叫中的对象主要方法并调用create呼叫创建对象的方法,解决所有导入问题:
Call call = new Call(TO_NUMBER, NEXMO_NUMBER, ncco);
voiceClient.createCall(call);
Test Your Application
使用以下命令启动您的应用程序Gradle运行您内部的命令拨打电话目录。 您应该从您的Nexmo号码收到一个电话。
接听此电话后,Nexmo语音API会说出“这是Nexmo的语音转语音”消息。
Conclusion
在几行代码中,您创建了一个可以呼叫收件人并说出一条消息的应用程序。
Check out our documentation on Nexmo Developer where you can learn more about call flow or Nexmo Call Control Objects. See our Nexmo Code Snippets for Java for full code examples on this tutorial and more.
The post Make Ťext-to-Speech Phone Call with Java appeared first on Nexmo.