如何将Cortana命令连接到自定义脚本?

时间:2015-05-25 05:21:57

标签: scripting nlp windows-10 cortana

这可能有点早,但我正在运行Windows 10 Technical Preview Build 10122.我想设置Cortana以获得自定义命令。以下是她的工作方式:

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

Microsoft将处理该命令,如果没有任何内容,她只会在bing上搜索输入。但是,我希望能够说出类似的内容,例如

Hey Cortana, I'm going to bed now

让输入I'm going to bed now触发器运行批处理脚本,VBScript,命令或任何某种自定义响应,基本上执行以下操作。

C:\> shutdown -s

有没有办法为Cortana设置预定义的自定义命令?

更新

我根据talkitbr的优秀且非常有用的答案basic YouTube tutorial创建了此this more advanced oneGitHub repo以及相应的below

起初他的回答超出了我的理解,所以我决定将其细节分解为像我这样的未来用户。

2 个答案:

答案 0 :(得分:42)

您可以为Cortana创建命令以进行侦听。 这些命令需要在名为Voice Command Definitions或VCD。

的XML文件中描述

以下是一个例子:

<?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>

创建此定义后,您需要在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);
    }
}

然后覆盖App.OnActivated方法来处理触发事件的时间:

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

完成所有这些操作后,您可以使用ProcessStartInfoSystem.Diagnostics.Process.Start来调用批处理脚本。

此外,如果您有兴趣通过Cortana窗口回复用户,请查看post regarding Cortana in background

答案 1 :(得分:1)

你可以做的是写一个.bat文件并将文件的快捷方式添加到文件夹:C:\ ProgramData \ Microsoft \ Windows \ Start Menu \ Programs 您可以根据需要命名快捷方式,并通过说:&#34;嘿Cortana打开/启动[快捷方式名称]&#34;来触发关机。 确保Cortana只听你不要恶作剧#34;