Collecting DTMF
This example shows how to collect a DTMF string from the caller. It involves the Interaction
turn which is created using the Interaction.Builder
.
The first thing we need to do is to create the DtmfRecognition
. This object contains the DTMF grammar used for DTMF recognition.
There are a few built-in grammars specified in the VoiceXML specification. We are going to use digits
.
GrammarItem dtmfGrammar = new GrammarReference("builtin:dtmf/digits");
DtmfRecognition dtmfRecognition = new DtmfRecognition(dtmfGrammar);
Then we need to build the interaction. We are going to instanciate an Interaction.Builder
for this task. There is two ways to get the Interaction.Builder
object:
- by calling the static method
interaction
- by using the constructor of
Interaction.Builder
directly
We'll use the first method.
Interaction interaction = interaction("get-dtmf")
Note that we've used a static import for the interaction
method:
import static com.nuecho.rivr.voicexml.turn.output.OutputTurns.*;
Then we need to specify the prompts that we are going to use. This is done with the addPrompt
method. Here we just use a SpeechSynthesis
message.
.addPrompt(new SpeechSynthesis("Type a number."))
To create the Interaction
itself, we invoke the build
method and we pass the DtmfRecognition
object, meaning that we want to
perform DTMF recognition immediately after prompts are played. We also need to specify the time-out value, i.e. the time before a no-input
event is raised.
.build(dtmfRecognition, Duration.seconds(5));
Now that we have an Interaction
, we can execute it and receive the InputTurn
which contains the result of this interaction.
VoiceXmlInputTurn inputTurn = DialogueUtils.doTurn(interaction, context);
We now need to inspect the result in order to know what has been done by the user. We are going to deal with the following two outcomes
- The user has typed a number
- The user hasn't enter anything
Actually, other outcomes are possible (e.g. hang-up, error) but we'll ignore that for now for the sake of simplicity.
The InputTurn
contains a recognitionInfo
property which gives us acces to the recognition result.
Logger logger = context.getLogger();
if (inputTurn.getRecognitionInfo() != null) {
JsonArray recognitionResult = inputTurn.getRecognitionInfo().getRecognitionResult();
//Extracting the "interpretation" of the first recognition hypothesis.
String number = recognitionResult.getJsonObject(0).getString("interpretation");
logger.info("Number entered: " + number);
} else if (VoiceXmlEvent.hasEvent(VoiceXmlEvent.NO_INPUT, inputTurn.getEvents())) {
logger.info("Timeout.");
}
You can download or browse the complete code for this example at GitHub.This is a complete working application that you can build and run for yourself.
You can also clone the Rivr Cookbook repository and checkout this example:
git clone -b simple-dtmf-interaction git@github.com:nuecho/rivr-cookbook.git
Then, to build and run it:
cd rivr-cookbook
./gradlew jettyRun
The VoiceXML dialogue should be available at http://localhost:8080/rivr-cookbook/dialogue
To stop the application, press Control-C in the console.