Skip to content

Invoking a subdialogue

Jean-Philippe Gariépy edited this page Apr 25, 2015 · 2 revisions

This example shows how to invoke a VoiceXML subdialogue as defined in the VoiceXML specification.

The output turn required to invoke a subdialogue is a SubdialogueCall. We pass the URL of the subdialogue in the constructor:

Dialogue.java:

SubdialogueCall subdialogueCall = new SubdialogueCall("invoke-subdialogue", "subdialogue.vxml");

The HTTP request to get the subdialogue will use the POST method:

Dialogue.java:

subdialogueCall.setMethod(SubmitMethod.post);

The turn will be executed:

Dialogue.java:

VoiceXmlInputTurn inputTurn = DialogueUtils.doTurn(subdialogueCall, context);

The subdialogue return values will be placed in the value property of the InputTurn. Let's assume here that our dialogue returned multiple named values. This will be available in the dialogue as a JsonObject:

Dialogue.java:

if (inputTurn.getJsonValue() != null) {
    JsonObject subdialogueReturnValues = (JsonObject) inputTurn.getJsonValue();

Each property of the JSON object correspond to the return value:

Dialogue.java:

    for (Entry<String, JsonValue> returnValue : subdialogueReturnValues.entrySet()) {
        context.getLogger().info("Subdialogue return value:"
                                 + returnValue.getKey()
                                 + "="
                                 + returnValue.getValue());
    }

}

The sample comes with a sample VoiceXML subdialogue:

subdialogue.vxml:

<?xml version="1.0" encoding="UTF-8"?>
<vxml version="2.1" xmlns="http://www.w3.org/2001/vxml">
  <form id="test">
    <field name="numberField" type="number">
      <prompt>Say a number</prompt>
    </field>
    <filled>
      <var name="number" expr="numberField" />
      <var name="status" expr="'success'" />
      <return namelist="number status" />
    </filled>
  </form>
</vxml>

Running this example

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 subdialogue-invocation 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.