How to connect Cortana commands to custom scripts?

与世无争的帅哥 提交于 2019-12-04 07:46:07

问题


This may be a little early to ask this, but I'm running Windows 10 Technical Preview Build 10122. I'd like to set up Cortana to have custom commands. Here's how she works:

Hey Cortana, <she'll listen and process this command>

Microsoft will process the command and if there isn't anything for it, she'll just search the input on bing. However, I'd like to be able to say something like, just for example

Hey Cortana, I'm going to bed now

And have the input I'm going to bed now trigger run a batch script, a VBScript, a command, or any some sort some of custom response that basically does the following.

C:\> shutdown -s

Is there a way to set up a predefined custom commands for Cortana?

Update:

I created this basic YouTube tutorial and this more advanced one with a corresponding GitHub repo based on talkitbr's excellent and very helpful answer below.

At first his answer was beyond my understanding so I decided to break it down in a bit more detail for future users like myself.


回答1:


You can create commands for Cortana to listen for. These commands need to be described in a XML file called Voice Command Definitions or VCD.

Here's an example:

<?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="Activate_Alarm">
            <Example>Activate alarm</Example>
            <ListenFor>[Would] [you] [please] activate [the] alarm [please]</ListenFor>
            <ListenFor RequireAppName="BeforeOrAfterPhrase">Activate alarm</ListenFor>
            <ListenFor RequireAppName="ExplicitlySpecified">Activate {builtin:AppName} alarm</ListenFor>
            <Feedback>Activating alarm</Feedback>
            <Navigate />
        </Command>
        ...
    </CommandSet>
</VoiceCommands>

After create this definition, you need to register it at App Startup:

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);
    }
}

An then override the App.OnActivated method to handle when the events are triggered:

protected override void OnActivated(IActivatedEventArgs e)
{
    // Handle when app is launched by Cortana
    if (e.Kind == ActivationKind.VoiceCommand)
    {
        VoiceCommandActivatedEventArgs commandArgs = e as VoiceCommandActivatedEventArgs;
        SpeechRecognitionResult speechRecognitionResult = commandArgs.Result;

        string voiceCommandName = speechRecognitionResult.RulePath[0];
        string textSpoken = speechRecognitionResult.Text;
        IReadOnlyList<string> recognizedVoiceCommandPhrases;

        System.Diagnostics.Debug.WriteLine("voiceCommandName: " + voiceCommandName);
        System.Diagnostics.Debug.WriteLine("textSpoken: " + textSpoken);

        switch (voiceCommandName)
        {
            case "Activate_Alarm":
                System.Diagnostics.Debug.WriteLine("Activate_Alarm command");
                break;

The tutorial shows the complete code.

After you do all of this, you can call your batch scripts using ProcessStartInfo or System.Diagnostics.Process.Start.

Also, if you are interested in responding to the user through Cortana window, check this post regarding Cortana in background.




回答2:


What you can do is write a .bat file and add a shortcut to the file to the Folder: C:\ProgramData\Microsoft\Windows\Start Menu\Programs you can name the shortcut whatever you want and trigger the shutdown by saying: "Hey Cortana open/start [shortcut name]". Make sure that Cortana only listens to you to not get "pranked".



来源:https://stackoverflow.com/questions/30431688/how-to-connect-cortana-commands-to-custom-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!