How to Integrate Cortana in the Menu App

Scope

The purpose of this article is to show how to integrate Cortana in the Menu App.

Introduction

One of the interesting features in Windows Phone 8.1 is Cortana. Cortana is an intelligent personal assistant, that will help users in basic tasks, like call to a friend, schedule an appointment and others tasks.

Cortana is not available in all languages, for this reason some non-English users have changed their devices to support it. For see more about how to have Cortana in our Windows Phone 8.1 devices see the following article.

Integrating Cortana

Cortana will use Voice Commands for interacting with the apps, these voice commands are pre-installed in the apps so Cortana knows how to launch each app.

The first step to integrate Cortana, in the Menu App, is to define the Voice Command Definition (VCD) file, that represents an XML file with the commands that Cortana will recognize and will match with the app.

For the Menu App we will define “Menu” as the name for talking with the Menu App and define the following two commands:

  • Show Command: that will allow you to choose which menu we want to see: Beverages, Starters, Mains, Desserts and Special Offers.

  • Natural Language Command: that will allow to recognize an expression like “I am hungry”, “I want to eat” and “I want to drink”.

In this sample, the VCD is defined for English (en-US) but more languages could be added.

The VCD file

Here is the VCD file:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <!-- Be sure to use the new v1.1 namespace to utilize the new PhraseTopic feature -->  
  4. <VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">  
  5. <!-- The CommandSet Name is used to programmatically access the CommandSet -->  
  6. <CommandSet xml:lang="en-us" Name="englishCommands">  
  7. <!-- The CommandPrefix provides an alternative to your full app name for invocation -->  
  8. <CommandPrefix>Menu</CommandPrefix>  
  9. <!-- The CommandSet Example appears in the global help alongside your app name -->  
  10. <Example> I am hungry </Example>  
  11.   
  12. <Command Name="ShowCommand">  
  13. <Example> Show Mains </Example>  
  14. <ListenFor> Show {dictatedShowTerms} </ListenFor>  
  15. <Feedback> Showing in Menu ... </Feedback>  
  16. <Navigate Target="MainPage.xaml" />  
  17. </Command>  
  18.   
  19. <Command Name="NaturalLanguageCommand">  
  20. <Example> I want to eat </Example>  
  21. <ListenFor> {naturalLanguage} </ListenFor>  
  22. <Feedback> Starting Menu ... </Feedback>  
  23. <Navigate Target="MainPage.xaml" />  
  24. </Command>  
  25.   
  26.   
  27. <PhraseTopic Label="dictatedShowTerms" Scenario="Search">  
  28. <Subject> Starters </Subject>  
  29. <Subject> Mains </Subject>  
  30. <Subject> Desserts </Subject>  
  31. <Subject> Beverages </Subject>  
  32. <Subject> Special Offers </Subject>  
  33. </PhraseTopic>   
  34.   
  35. <PhraseTopic Label="naturalLanguage" Scenario="Natural Language">  
  36. <Subject> I want to eat </Subject>  
  37. <Subject> I want to drink </Subject>  
  38. <Subject> I am hungry </Subject>  
  39. </PhraseTopic>  
  40. </CommandSet>  
  41. </VoiceCommands>  
Note: In the manifest file, check for the capability for Microphone.

The InstallVoiceCommandsAsync method.

Now we define the commands, we need to install it in the app each time the app starts.

Create the method
  1. private async Task InstallVoiceCommandsAsync()  
  2. {  
  3.       var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Cortana.xml"));  
  4.       await VoiceCommandManager.InstallCommandSetsFromStorageFileAsync(storageFile);  
  5. }  
And then call it in the OnNavigationTo from the MainPage:
  1. protected override async void OnNavigatedTo(NavigationEventArgs e)  
  2. {  
  3.       _dataTransferManager = DataTransferManager.GetForCurrentView();  
  4.       _dataTransferManager.DataRequested += OnDataRequested;  
  5.       _navigationHelper.OnNavigatedTo(e);  
  6.       await MainViewModel.LoadDataAsync();  
  7.       if (e.NavigationMode == NavigationMode.New)  
  8.       {  
  9.             await InstallVoiceCommandsAsync();  
  10.       }  
  11. }  
The OnActivated method

We then need to define what the app will be done each time it receives a voice command from Cortana. When Cortana recognizes a command the Menu App will send the data to the Menu App and the OnActivated method in App.xaml.cs will be called.

Here is the complete code for the OnActivated method:
  1. protected override void OnActivated(IActivatedEventArgs args)  
  2. {  
  3.       base.OnActivated(args);  
  4.   
  5.       if (args.Kind == ActivationKind.VoiceCommand)  
  6.       {  
  7.             var commandArgs = args as VoiceCommandActivatedEventArgs;  

  8.             if (commandArgs != null)  
  9.             {  
  10.                   SpeechRecognitionResult speechRecognitionResult = commandArgs.Result;  
  11.   
  12.                   var voiceCommandName = speechRecognitionResult.RulePath[0];  
  13.                   var textSpoken = speechRecognitionResult.Text;  
  14.   
  15.                   switch (voiceCommandName)  
  16.                   {  
  17.                         case "ShowCommand":  
  18.                         if (textSpoken.ToLower().Contains("starters"))  
  19.                         {  
  20.                               RootFrame.Navigate(typeof (StartersPage));  
  21.                         }  
  22.                         if (textSpoken.ToLower().Contains("mains"))  
  23.                         {  
  24.                               RootFrame.Navigate(typeof(Main1Page));  
  25.                         }  
  26.                         if (textSpoken.ToLower().Contains("desserts"))  
  27.                         {  
  28.                               RootFrame.Navigate(typeof(DessertsPage));  
  29.                         }  
  30.                         if (textSpoken.ToLower().Contains("beverages"))  
  31.                         {  
  32.                               RootFrame.Navigate(typeof(BeveragesPage));  
  33.                         }  
  34.                         if (textSpoken.ToLower().Contains("special") || textSpoken.ToLower().Contains("offer"))  
  35.                         {  
  36.                               RootFrame.Navigate(typeof(MainPage), "SpecialOffers");  
  37.                         }  
  38.                         break;  

  39.                         case "NaturalLanguageCommand":  
  40.                         if (textSpoken.ToLower().Contains("eat") || textSpoken.ToLower().Contains("hungry"))  
  41.                         {  
  42.                               RootFrame.Navigate(typeof(Main1Page));  
  43.                         }  
  44.   
  45.                         if (textSpoken.ToLower().Contains("drink"))  
  46.                         {  
  47.                               RootFrame.Navigate(typeof (BeveragesPage));  
  48.                         }  
  49.                         if (textSpoken.ToLower().Contains("special"))  
  50.                         {  
  51.                               RootFrame.Navigate(typeof (MainPage), "SpecialOffers");  
  52.                         }  
  53.                         break;  
  54.                   }  
  55.             }  
  56.       }  

  57.       Window.Current.Activate();  
  58. }  
Each possible voice command defined in VCD must be handled so the results shown to the user won´t be weird.

Using Cortana

Before we talk with Cortana, we need to run the Menu App in the device, because we need to install the VCD. After it, we can start Cortana.

menu app

And then click in “see more” to see what we can say:

Using Cortana menu in windows phone   Using Cortana in windows phone

Each app will show some examples to help users using Cortana. Clicking the Menu App it will show the samples defined In the VCD file:

menu in contana

Playing with Cortana, we will see the following:

Saying “Menu I am hungry”.

         contra menu           playing with contra

And then:

                                                main menu

Saying “Menu I want to drink”:

         what would you like           start menu

                                                 menu beverages

Saying “Menu Show offers”:

        saying menu offer          search menu show offer

                                                 menu of special offer

Code

See the code for this sample in:

Step 9: How to integrate Cortana with Menu App

See also: