Dynamic Menus with VoiceModel

There is a new example in the VoiceModel project that shows how to create dynamic menus. Dynamic menus can be very useful in voice applications where you want to configure how the menus work in some persistence mechanism, allowing you to change the flow of the application without recompiling and redeploying, or if you want to tailor it to a specific user based based on their personal preferences.

Here is the code in this new example:

public override CallFlow BuildCallFlow()
{
    CallFlow flow = new CallFlow();

    flow.AddState(ViewStateBuilder.Build("greeting", "myMenu", new Say("greeting", "Welcome to the dynamic menu example.")), true);
    //This is a fake service that mimics getting meta-data for the menus from a web service or database
    DynamicMenuService service = new DynamicMenuService();
    VoiceMenu myMenu = service.GetMenu("myMenu");
    Prompt menuOptions = new Prompt();
    //Build the prompts for the menu options form the meta-data
    foreach (MenuOption option in myMenu.Options)
        menuOptions.audios.Add(new TtsMessage(option.PromptMsg + service.GetSelectionPrompt(option.Number)));
    //Create the initial state and view model without any transitions
    State myMenuState = ViewStateBuilder.Build("myMenu", new Ask("myMenu", menuOptions,
        new Grammar(new BuiltinGrammar(BuiltinGrammar.GrammarType.digits, 1, 1))));
    //Add the transitions to the state
    foreach (MenuOption option in myMenu.Options)
        myMenuState.AddTransition("continue", option.TransitionTarget, new Condition("result == '" + option.Number.ToString() + "'"));
    //Add the state to the call flow
    flow.AddState(myMenuState);
    flow.AddState(ViewStateBuilder.Build("doThis", new Exit("doThis", "You selected to do this. Goodbye.")));
    flow.AddState(ViewStateBuilder.Build("doThat", new Exit("doThat", "You selected to do that. Goodbye.")));
    flow.AddState(ViewStateBuilder.Build("doWhatever", new Exit("doWhatever", "You selected to do whatever. Goodbye.")));
    flow.AddState(ViewStateBuilder.Build("invalidSelect", new Exit("invalidSelect", "That was an invalid selection. Goodbye.")));
    return flow;
}

The comments in the code provide the step by step process on how to create the prompt for your dynamic menu and how to create the transitions in the call flow based on the caller's input.  There is a mock service in this example that simulates getting the meta-data that represents the menu structure to use for the voice application.  In a real-world application you would substitute this mock service with a web service or database access through a repository.  This new example demonstrates the flexibility of VoiceModel when creating a voice application.

If you run into an issue with VoiceModel in a particular context that you would like to use it in just create a new issue in the VoiceModel project  and we will either provide a solution for using VoiceModel in that context or it may turn into a new feature request.

Comments

Popular posts from this blog

Using Claims in ASP.NET Identity

Seeding & Customizing ASP.NET MVC SimpleMembership

Customizing Claims for Authorization in ASP.NET Core 2.0