1

Ok, I've got an application with Cortana support. I have some command to navigate to specific page. But in some cases I'd like just to provide a reply (text or pronounced by Cortana) instead of launching whole application. Is there any way to do that?

For example, when you're asking Cortana "What is capital of USA?", she just replies you "Washington". I wanna do something like that.

3 Answers 3

2

Have a look at the bottom of this tutorial, it details how to set what she says and shows.

Basically you need to have a background task that runs executes from a voice command and then during the execution you'll need create a VoiceCommandUserMessage.

Grab the VoiceServiceConnection using the taskInstance.TriggerDetails

    voiceServiceConnection = 
      VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
        triggerDetails); 

wait for the voice command to complete

    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

then add

VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
userMessage.DisplayMessage = "Here’s your trip.";
userMessage.SpokenMessage = "Your trip to Vegas is on August 3rd.";

wrap it in a response

var response = 
  VoiceCommandResponse.CreateResponse(
    userMessage);

finally, ask Cortana to display it:

await voiceServiceConnection.ReportSuccessAsync(response);

Also take a look at the design guidelines for cortana

0
2

From my understanding, this is possible only in Windows 10 apps.

https://channel9.msdn.com/Events/Build/2015/2-691

2
  • You're right. To do something like that, you have to create Windows.ApplicationModel.AppService -> minimum support is Windows 10 (both for mobile and desktop).
    – Vlad
    Jul 1, 2015 at 18:22
  • Correct, Windows 10 only.
    – ActiveNick
    Jul 10, 2015 at 19:25
0

The Windows applications allow you to iteracte with Cortana at foreground and background. I believe that the background is what you are looking for:

First you need to create a XML file to represet your VCD (Voice Command Definitions). This is the place where you declare the command you want to use:

<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
  <CommandSet xml:lang="en-us" Name="HomeControlCommandSet_en-us">
    <CommandPrefix>HomeControl</CommandPrefix>
    <Example>Control alarm, temperature, light and others</Example>

    <Command Name="CheckTemperature">
      <Example>Check temperature</Example>
      <ListenFor>check [current] temperature</ListenFor>
      <Feedback>Checking temperature</Feedback>
      <VoiceCommandService Target="VoiceCommandService" />
    </Command>

  </CommandSet>
</VoiceCommands>

After you need to register the VCD at App.OnLaunched inside App.xaml.cs:

protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    // Install the VCD
    try
    {
        StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(@"HomeControlCommands.xml");
        await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("There was an error registering the Voice Command Definitions", ex);
    }
}

After, you need to create a new Windows Runtime Component project and create a class that implements the IBackgroundTask:

public sealed class HomeControlVoiceCommandService : IBackgroundTask
{
    private VoiceCommandServiceConnection voiceServiceConnection;
    private BackgroundTaskDeferral serviceDeferral;

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        // Create the deferral by requesting it from the task instance
        serviceDeferral = taskInstance.GetDeferral();

        AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

        if (triggerDetails != null && triggerDetails.Name.Equals("VoiceCommandService"))
        {
            voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

            VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

            // Perform the appropriate command depending on the operation defined in VCD
            switch (voiceCommand.CommandName)
            {
                case "CheckTemperature":
                    VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
                    userMessage.DisplayMessage = "The current temperature is 23 degrees";
                    userMessage.SpokenMessage = "The current temperature is 23 degrees";

                    VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, null);
                    await voiceServiceConnection.ReportSuccessAsync(response);
                    break;

                default:
                    break;
            }
        }

        // Once the asynchronous method(s) are done, close the deferral
        serviceDeferral.Complete();
    }
}

Don't forget to add this new project as a reference from the main one. Also, you need to register the service inside Package.appxmanifest:

<Extensions>
<uap:Extension Category="windows.appService" EntryPoint="CortanaComponent.HomeControlVoiceCommandService">
  <uap:AppService Name="VoiceCommandService" />
</uap:Extension>
</Extensions>

This should work!

For a complete guide, you can check out this post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.