Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VoiceXmlDocumentAdapter usage #15

Open
Vamsi0412 opened this issue Sep 1, 2021 · 1 comment
Open

VoiceXmlDocumentAdapter usage #15

Vamsi0412 opened this issue Sep 1, 2021 · 1 comment
Labels

Comments

@Vamsi0412
Copy link

Vamsi0412 commented Sep 1, 2021

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

@gawi gawi changed the title In what way we can use voicexmldocumentadapter and documentTurn, Is there any sample code ? In what way we can use VoiceXmlDocumentAdapter and documentTurn, Is there any sample code ? Sep 1, 2021
@gawi gawi added the question label Sep 1, 2021
@gawi gawi changed the title In what way we can use VoiceXmlDocumentAdapter and documentTurn, Is there any sample code ? VoiceXmlDocumentAdapter usage Sep 1, 2021
@gawi
Copy link
Member

gawi commented Sep 3, 2021

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants