nuecho / rivr

Rivr is a lightweight open-source dialogue engine enabling Java developers to easily create enterprise-grade VoiceXML applications.
http://rivr.nuecho.com
Other
61 stars 23 forks source link

VoiceXmlDocumentAdapter usage #15

Open Vamsi0412 opened 3 years ago

Vamsi0412 commented 3 years ago

In what way we can use VoiceXmlDocumentAdapter and documentTurn, Is there any sample code ?

gawi commented 3 years ago

VoiceXmlDocumentAdapter is useful in cases where you need to customize the VoiceXML generated by Rivr. There are two ways to specify an adapter:

  1. You can set the VoiceXmlDocumentAdapter directly on the turn for which you want the VoiceXML to be tweaked (see the VoiceXmlDocumentTurn.addAdapter method.

  2. You can also set it globally on the servlet using the [VoiceXmlDialogueServlet.setVoiceXmlDocumentAdapters] (https://nuecho.github.io/rivr/javadoc/com/nuecho/rivr/voicexml/servlet/VoiceXmlDialogueServlet.html#setVoiceXmlDocumentAdapters(java.util.List)) method (i.e. create a subclass of VoiceXmlDialogueServlet and call setVoiceXmlDocumentAdapters from the initializeVoiceXmlDialogueServlet method).

A VoiceXmlDocumentAdapter let you do anything you want on the DOM. Just use the org.w3c.dom API to modify the document.

package com.nuecho.rivr.cookbook.dialogue;

import org.w3c.dom.*;

import com.nuecho.rivr.core.channel.*;
import com.nuecho.rivr.core.dialogue.*;
import com.nuecho.rivr.voicexml.dialogue.*;
import com.nuecho.rivr.voicexml.rendering.voicexml.*;
import com.nuecho.rivr.voicexml.turn.*;
import com.nuecho.rivr.voicexml.turn.first.*;
import com.nuecho.rivr.voicexml.turn.last.*;
import com.nuecho.rivr.voicexml.turn.output.*;
import com.nuecho.rivr.voicexml.turn.output.audio.*;

public class Dialogue implements VoiceXmlDialogue {

    @Override
    public VoiceXmlLastTurn run(VoiceXmlFirstTurn firstTurn, VoiceXmlDialogueContext context) throws Timeout,
            InterruptedException {

        Message message = OutputTurns.message("message")
                                     .addAudioItem(new SpeechSynthesis("Some message here..."))
                                     .build();

        VoiceXmlDocumentAdapter voiceXmlDocumentAdapter = new VoiceXmlDocumentAdapter() {

            @Override
            public void adaptVoiceXmlDocument(Document document) throws VoiceXmlDocumentRenderingException {
                Comment commentNode = document.createComment("This is a custom comment at the end of the VoiceXML document!");
                document.getDocumentElement().appendChild(commentNode);
            }
        };

        message.addAdapter(voiceXmlDocumentAdapter);

        DialogueUtils.doTurn(message, context);

        return new Exit("exit");
    }

}

This will generate the following:

<?xml version="1.0" encoding="UTF-8"?><vxml application="/rivr-test/dialogue/root/b7058749-27af-464f-9f2c-b0913c9a24ae" version="2.1" xmlns="http://www.w3.org/2001/vxml">
    <script>application.rivr.localErrorHandling = false; application.rivr.inputTurn = {};</script>
    <form id="form">
        <block>
            <prompt>Some message here...</prompt>
            <goto next="#submitForm"/>
        </block>
    </form>
    <catch>
        <if cond="_event.substring(0, 5) == &quot;error&quot;">
            <if cond="application.rivr.localErrorHandling">
                <goto next="#fatalErrorForm"/>
                <else/>
                <script>application.rivr.localErrorHandling=true</script>
            </if>
        </if>
        <script>application.rivr.addEventResult(_event, _message)</script>
        <goto next="#submitForm"/>
    </catch>
    <form id="fatalErrorForm">
        <block>
            <exit/>
        </block>
    </form>
    <form id="submitForm">
        <block>
            <var expr="application.rivr.toJson(application.rivr.inputTurn)" name="inputTurn"/>
            <if cond="application.rivr.hasRecording(application.rivr.inputTurn)">
                <var expr="application.rivr.inputTurn.recordingMetaData.data" name="recording"/>
                <assign expr="undefined" name="application.rivr.inputTurn.recordingMetaData.data"/>
                <submit enctype="multipart/form-data" method="post" namelist="inputTurn recording" next="/rivr-test/dialogue/b7058749-27af-464f-9f2c-b0913c9a24ae/0/message"/>
                <else/>
                <submit method="post" namelist="inputTurn" next="/rivr-test/dialogue/b7058749-27af-464f-9f2c-b0913c9a24ae/0/message"/>
            </if>
        </block>
    </form>
    <!--This is a custom comment at the end of the VoiceXML document!-->
</vxml>