使用Java记录电话消息

Introduction

In a previous tutorial, we showed you how to Receive a Phone Call with Java and respond using text to speech. We can also allow users to leave a message and then retrieve that recorded message.

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 8 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.

Record a Phone Message with Java

本教程将指导您完成以下步骤:

  1. Using Gradle to setup a new Java project.
  2. Using the Spark framework for controlling the call.
  3. Purchasing a number and configuring your Nexmo account to use that number with your application.

Using Gradle to Setup a New Java Project

您将使用Gradle来管理依赖关系以及创建和运行Java应用程序。

的gradle init --type = java应用程序命令将创建您将需要的所有文件夹以及用于编写代码的示例类。

在命令行中,使用以下命令创建一个新的Java项目:

mkdir record-a-message
cd record-a-message
gradle init --type=java-application

Using the Spark Framework for Controlling the Call

当您的号码收到呼叫时,您将使用Spark框架接收Nexmo发出的HTTP呼叫,并接收Nexmo在记录消息后发送的请求。

Adding the Dependencies

将以下内容添加到依存关系封锁你的build.gradle文件:

// Spark Framework
compile 'com.sparkjava:spark-core:2.7.2'

// Nexmo Java Client
compile 'com.nexmo:client:4.0.1'

你的依存关系块应如下所示:

dependencies {
    compile 'com.sparkjava:spark-core:2.7.2'
    compile 'com.nexmo:client:4.0.1'

    // Use JUnit test framework
    testCompile 'junit:junit:4.12'
}

Gradle将创建应用程式中的课程src /主/ java夹。 在这堂课里面是getGreeting还有一个主要方法。 您将不需要getGreeting方法,因此随时将其删除。

Define the Answer Route

First, you will define the route that will be used for answering the call. When a call is received, Nexmo will send a request to a pre-defined webhook URL. It expects to receive a Nexmo Call Control Object (NCCO) containing a list of actions to execute.

接听电话后,您的应用程序将指示Nexmo执行以下三个操作:

  1. 一种谈论问候呼叫者并指导他们如何留言的操作。一种记录 action which will instruct the Voice API to start 记录ing.一种谈论感谢他们留言的行动。

这是您的应用程序将创建的最终NCCO:

[
  {
    "text": "Please leave a message after the tone, then press #. We will get back to you as soon as we can.",
    "action": "talk"
  },
  {
    "endOnSilence": 3,
    "endOnKey": "#",
    "beepStart": true,
    "eventUrl": [
      "http://your-web-address/webhooks/recordings"
    ],
    "action": "record"
  },
  {
    "text": "Thank you for your message. Goodbye",
    "action": "talk"
  }
]

将以下内容添加到主要的方法应用程式类,请确保解决所有导入问题:

/*
* Route to answer and connect incoming calls with recording.
*/
Route answerRoute = (req, res) -> {
    String recordingUrl = String.format("%s://%s/webhooks/recordings", req.scheme(), req.host());

    TalkAction intro = new TalkAction.Builder("Please leave a message after the tone, then press #. We will get back to you as soon as we can.")
            .build();

    RecordAction record = new RecordAction.Builder()
            .eventUrl(recordingUrl)
            .endOnSilence(3)
            .endOnKey('#')
            .beepStart(true)
            .build();

    TalkAction outro = new TalkAction.Builder("Thank you for your message. Goodbye").build();

    res.type("application/json");

    return new Ncco(intro, record, outro).toJson();
};

的记录 action has a few different properties. For example, you can define the event url that will be sent a request when the 记录ing is finished, end the 记录ing on a specific key press, and play a beep at the start of the 记录ing.

Define the Recordings Route

的记录动作具有称为eventUrl which is used to communicate when the 记录ing is finished. 的following is an example of the return parameters sent to the event url:

{
  "start_time": "2020-01-01T12:00:00Z",
  "recording_url": "https://api.nexmo.com/media/download?id=aaaaaaaa-bbbb-cccc-dddd-0123456789ab",
  "size": 12345,
  "recording_uuid": "aaaaaaaa-bbbb-cccc-dddd-0123456789ab",
  "end_time": "2020-01-01T12:01:00Z",
  "conversation_uuid": "bbbbbbbb-cccc-dddd-eeee-0123456789ab",
  "timestamp": "2020-01-01T14:00:00.000Z"
}

The Nexmo Java Client has a class called RecordEvent which can be used to deserialize the JSON sent to eventUrl. For now, you're going to output the recording_url to the console.

将以下内容添加到主要的方法应用程式类,解决所有导入问题:

/*
* Route which prints out the recording URL it is given to stdout.
*/
Route recordingRoute = (req, res) -> {
    RecordEvent recordEvent = RecordEvent.fromJson(req.body());
    System.out.println(recordEvent.getUrl());

    res.status(204);
    return "";
};

Register the Routes

到目前为止,您已经定义了两条路线:

  • 当Nexmo应答来电时,第一条路由将使用NCCO响应Nexmo。当Nexmo完成记录消息时,第二条路线将记录记录URL。

为了使用这些路由,我们需要配置Spark。 您的应用程序将在端口上监听3000,并且路由将在/ webhooks / answer和/ webhooks / recordings。

将以下内容添加到主要的方法应用程式类:

Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/recordings", recordingRoute);

Purchasing a Number

您需要一个Nexmo号码才能接听电话。 如果您没有电话号码,则可以使用Nexmo CLI购买:

nexmo number:buy --country_code US

记下分配给您的购买号码。 您将需要此号码来链接您的应用程序并进行测试。

Exposing Your Application

为了向您的应用程序发送HTTP请求,Nexmo需要知道您的应用程序正在运行的URL。

Instead of configuring your local network or hosting your application on an external service, you can use ngrok to safely expose your application to the internet.

下载ngrok并运行以下命令:

ngrok http 3000

请记下转发地址,因为在配置帐户时将需要它。 在下图中,转发地址为http://99cad2de。ngrok。io。

使用Java记录电话消息_第1张图片

Configure Your Nexmo Account

如果您没有应用程序,则可以使用Nexmo CLI使用您的ngrok转发地址创建一个应用程序:

nexmo app:create "Record Message Demo" http://your-ngrok-forwarding-address/webhooks/answer http://your-ngrok-forwarding-address/webhooks/events --keyfile private.key

运行此命令后,将显示一个应用程序ID。 例如:notreal-1111-2222-3333-appid。 您将需要此应用程序ID将您的电话号码链接到该应用程序。

您可以使用Nexmo CLI链接您的电话号码和应用程​​序:

nexmo link:app your-nexmo-phone-number your-application-id

此命令指示Nexmo在您的帐户上创建一个新应用程序。 当收到电话时,该应用程序将向第一个URL发送请求。 通话状态更改时,应用程序会将请求发送到第二个URL。

Test Your Application

使用以下命令启动您的应用程序Gradle运行您内部的命令记录一条消息目录。

拨打您的Nexmo号码并测试您的应用程序。 您将听到以下消息:“请在提示音后留言,然后按#。我们将尽快与您联系。” 听到提示音后,留言,然后按#。 然后,您应该听到“谢谢您的消息。再见。” 录制URL将显示在您的控制台上。

Conclusion

在几行代码中,您创建了一个应用程序,可以接收电话,记录消息,然后显示该记录的URL。

Check out our documentation on Nexmo Developer where you can learn more about call flow or Nexmo Call Control Objects. See our Nexmo Quickstart Examples for Java for full code examples on this tutorial and more.

The post Record a Phone Message with Java appeared first on Nexmo.

from: https://dev.to//vonagedev/record-a-phone-message-with-java-56j3

你可能感兴趣的:(使用Java记录电话消息)