voicexmlrivr

How to use just one DialogueFactory for different Dialogs in Rivr VoiceXML


I am using VoiceXmlDialogueFactory to create some Dialogue's. I wonder how I can check which is the class name configured in the Dialog (in web.xml) so I don't have to create a DialogueFactory class for every Dialog and I can just if/then in the create method. I've checked that method:

public VoiceXmlDialogue create(DialogueInitializationInfo<VoiceXmlInputTurn, VoiceXmlOutputTurn, VoiceXmlDialogueContext> initializationInfo) throws DialogueFactoryException {

But didn't find where to get the com.nuecho.rivr.voicexml.dialogue.class parameter. A specific parameter I can read some other way may do the trick too.

thanks for any advice.


Solution

  • If I understand correctly, you want a single DialogueFactory class that can creates different dialogues (of different VoiceXmlDialogue classes) based on some external data. That's the purpose of the DialogueFactory, really.

    The most straightforward way to do that is to use something in the initial HTTP request (like the parameter or the path) to determine what dialogue is to be built. You can get the HttpServletRequest by casting the initializationInfo parameter of the DialgueFactory.create() into a WebDialogueInitializationInfo. In this object, you will find additional properties that you can use to perform the required logic. You can even access the servlet context.

    So, using a query parameter, you can create the right kind of dialog. http://server.exemple.com/application/dialogue?type=abc

    public class DialogueFactory implements VoiceXmlDialogueFactory {
    
        @Override
        public VoiceXmlDialogue create(
                DialogueInitializationInfo<VoiceXmlInputTurn, VoiceXmlOutputTurn, VoiceXmlDialogueContext> initializationInfo)
                throws DialogueFactoryException {
    
            if (!(initializationInfo instanceof WebDialogueInitializationInfo))
                throw new DialogueFactoryException("Can only work in web mode.");
    
            WebDialogueInitializationInfo<?, ?, ?> webInitializationInfo = 
                (WebDialogueInitializationInfo<?, ?, ?>) initializationInfo;
    
            String dialogueType = webInitializationInfo.getHttpServletRequest().getParameter("type");
    
            // Then use dialogueType to build the right kind of Dialogue.
        }
    }
    

    You can also use the path info: http://server.exemple.com/application/dialogue/abc

    See this cookbook entry for something similar.