使用Java处理键盘输入(DTMF)

Introduction

In a previous tutorial, we showed you how to create an application that can Receive a Phone Call with Java. In this tutorial, you will create an application that can receive a phone call and respond to user input using 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 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.

Handle User Input 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

You will use Gradle to manage your dependencies and to create and run your Java application. From the command line, create a new Java project with the following commands:

mkdir handle-user-input
cd handle-user-input
gradle init --type java-application

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

Using the Spark Framework for Controlling the Call

You will use the Spark framework to intercept the HTTP call that Nexmo uses when your number receives a call, and for the request that Nexmo sends when input is received.

Adding the Dependencies

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

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

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

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    compile 'com.google.guava:guava:23.0'
    compile 'com.sparkjava:spark-core:2.7.2'
    compile 'com.nexmo:client:4.0.1'

    // Use JUnit test framework
    testCompile 'junit:junit:4.12'
}
Setup the Answer Route

Gradle will create the App class in the src/main/java folder. Inside of this class is a getGreeting and a main method. You won't need the getGreeting method, so feel free to remove it.

替换内容主要方法,使用以下方法解决所有进口问题:

/*
* Route to answer incoming calls.
*/
Route answerRoute = (req, res) -> {
    TalkAction intro = new TalkAction
            .Builder("Hello. Please press any key to continue.")
            .build();

    InputAction input = new InputAction.Builder()
            .eventUrl(String.format("%s://%s/webhooks/dtmf", req.scheme(), req.host()))
            .maxDigits(1)
            .build();


    res.type("application/json");

    return new Ncco(intro, input).toJson();
};

/*
* Route to print out call event info.
*/
Route eventRoute = (req, res) -> {
    System.out.println(req.body());
    return "";
};

Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/events", eventRoute);

This code will setup a route on http://localhost:3000/webhooks/answer which will respond with the following Nexmo Call Control Object (NCCO):

[
  {
    "text": "Hello please press any key to continue.",
    "action": "talk"
  },
  {
    "maxDigits": 1,
    "action": "input",
    "eventUrl": [
      "http://localhost:3000/webhooks/dtmf"
    ]
  }
]

谈话动作将指示Nexmo说出文本属性返回给调用者。 输入操作将指示Nexmo捕获呼叫者输入的单个数字并将POST请求发送到eventUrl有了这些信息。

A route will also be setup on http://localhost:3000/webhooks/events which Nexmo will use to communicate call status changes.

Setup the DTMF Route

当呼叫者按其设备上的数字时,会创建双音多频(DTMF)信号。 Nexmo使用此DTMF信号来确定按下了哪组键。 一旦发生这种情况,Nexmo将POST请求发送到eventUrl在定义输入Ncco。

这是一个包含JSON正文的POST请求示例:

{
    "dtmf": "5",
    "timed_out": false,
    "uuid": "some-uuid",
    "conversation_uuid": "some-conversation",
    "timestamp": "2018-08-14T19:59:02.528Z"
}

为了用Java读取此信息,您将需要一个将JSON属性映射到Java属性的类。 Nexmo Java客户端库包含InputEvent用于处理此映射的类。

在里面主要的方法应用程式类在下面添加以下路线eventRoute:

/*
* Route which returns NCCO saying which DTMF code was received.
*/
Route inputRoute = (req, res) -> {
    InputEvent event = InputEvent.fromJson(req.body());

    TalkAction response = new TalkAction
            .Builder(String.format("You pressed %s, Goodbye.", event.getDtmf()))
            .build();

    res.type("application/json");

    return new Ncco(response).toJson();
};

接下来,通过在末尾添加以下内容来注册路线主要方法:

Spark.post("/webhooks/dtmf", inputRoute);

This route will respond with the following NCCO:

[
  {
    "text": "You pressed 6, Goodbye.",
    "action": "talk"
  }
]

其中6是dtmf发送到的json的属性/ webhooks / dtmf。

Purchasing a Number

You will need a Nexmo number in order to receive phone calls. If you do not have a number you can use the Nexmo CLI to purchase one:

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.

Download ngrok and run the following command:

ngrok http 3000

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

Screenshot of ngrok running in terminal with forwarding address http://99cad2de.ngrok.io

Configure Your Nexmo Account

If you do not have an application you can use the Nexmo CL一世 to create one using your ngrok forwarding address:

nexmo app:create "Receive Call 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将您的电话号码链接到该应用程序。

You can use the Nexmo CLI to link your phone number and application:

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

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

Test Your Application

使用以下命令启动您的应用程序Gradle运行您内部的命令处理用户输入目录。

拨打您的Nexmo号码并测试您的应用程序。 您将听到以下消息:“您好,请按任意键继续。” 按电话小键盘上的数字,然后您将听到“您按了6,再见”的消息,其中6是您按的数字。

Conclusion

在几行代码中,您创建了一个应用程序,可以接收电话,捕获用户输入并使用该输入进行响应。 尝试使用其他方式来使用用户输入来控制呼叫。

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.

from: https://dev.to//vonagedev/handle-keypad-input-dtmf-with-java-333b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值