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 frameworktestCompile '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。

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

使用Java处理键盘输入(DTMF)相关推荐

  1. java 读取键盘输入

    在工作中其实很少用到java读取键盘输入的情况,但是在各种网站刷题时却经常碰到,同时,在日常写一些测试方法的时候,如果通过键盘读取输入也是十分方便的,因此简要的做一个总结,方便后续查看及使用. Sys ...

  2. java对键盘输入进行读取_JAVA 读取键盘输入

    JAVA 读取键盘输入 第一种方法:System.in.read(); 只能针对一个字符的获取,同时,获取进来的变量的类型只能是char 第二种方法:从控制台接收一个字符串,然后将其打印出来. 在这个 ...

  3. Java如何键盘输入

    导入该类所在的包,Java中键盘输入的函数在所在的包为java.util.Scanner;scanner类表示一个简单的用户扫描器,可以接受用户的输入. 创建该对象,即声明变量. 调用里面的功能,定义 ...

  4. java从键盘输入字符串并求长度_java从键盘输入字符串的方法

    java从键盘输入字符串的方法 发布时间:2020-06-29 09:15:07 来源:亿速云 阅读:142 作者:Leah 本篇文章为大家展示了java从键盘输入字符串的方法,代码简明扼要并且容易理 ...

  5. Java从键盘输入n行字符串_Java十四天零基础入门-Java布尔类型

    不闲聊!!!不扯淡!!!小UP只分享Java相关的资源干货 Java布尔类型 在Java语言中布尔类型的值只包括true和false,没有其他值,不包括1和0,布尔类型的数据在开发中主要使用在逻辑判断 ...

  6. java从键盘输入一组数据,输出其最大值,平均值,最小值没法输出

    总结::需要耐心,加思考.做事不思考,那就是白做徒劳!!!!! package com.aini;import java.util.Scanner; //操...为什么数组的大小比较我硬是搞不懂,比较 ...

  7. java从键盘输入一个数,并将其倒序输出

    从键盘输入一个数,并将其倒序输出 import java.util.Scanner;/*** 从键盘上录入任意整数,倒叙输出*/ public class Test06 {public static ...

  8. java 键盘输入密码,(JAVA)从键盘输入一批字符,以@结束,按要求加密并输出

    从键盘输入一批字符,以@结束,按要求加密并输出. 输入 从键盘输入一批字符,占一行,以@结束. 输出 输出占一行 加密规则: 1)所有字母均转换为小写. 2)若是字母'a'到'y',则转化为下一个字母 ...

  9. java实现键盘输入_java 键盘输入的多种实现方法

    实例程序: 1.利用 Scanner 实现从键盘读入integer或float 型数据 //import java.io.*; import java.util.*; public class Inp ...

  10. Java 用键盘输入 int型 String型 char型数据 示例:简单计算功能 eclipse

    //Scanner input = new Scanner(System.in); 用键盘输入不同类型的数据: int型数据 = input.nextInt(): String型字符串 = input ...

最新文章

  1. python3.7安装numpy模块-CENTOS7 Python3.7安装numpy
  2. linux阿波罗配置文件放在哪,Apollo阿波罗配置中心
  3. SNMP模型中,网管者、网管代理、网管协议及管理信息库MIB之间的工作流程
  4. java 向上抛异常_Java 异常的处理方式throws
  5. Django中的路由分发
  6. 荷兰搞定原子级数据存储:1平方英寸500TB
  7. 解决POI大数据导出Excel内存溢出、应用假死
  8. c语言不能正确输出最小值,C语言综合测试.doc
  9. mac的一些使用事项
  10. 去除datatable列中重复的值
  11. IEC61850建模说明
  12. AXI总线(top)
  13. swfupload java实例_swfupload例子
  14. 世界上再也找不到第二位程序员大叔能写出这样纯美的数学小说了
  15. 东财《组织行为学B》综合作业
  16. Hive exited with status 1
  17. 全文检索技术Lucene
  18. ISO14229之概述
  19. java中通物流api详解
  20. Go语言圣经 - 第3章 基础数据类型

热门文章

  1. Mac运行exe的几种方法,欢迎补充!
  2. 彩色图片如何转为单色位图bmp :用window画板
  3. 计算机硬盘加密的原理,对硬盘加密的加密技术是什么?
  4. 汽车研发的五大阶段及制造的四大工艺
  5. Unable to open JDBC Connection for DDL execution
  6. k2000显卡相当于gtx_电脑中的显卡是什么样干什么样的?NVDIA推出的两块Quadro显卡K1000M和K2000M性能究竟差多少...
  7. 用Python制作二维码
  8. 《C Prime Plus》(第六版) 第05章 运算符、表达式和语句 例题集和编程练习
  9. 考研数学要背诵的知识点
  10. 电脑端扫描二维码(java)