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

How to connect Cortana commands to custom scripts?

现在问这个问题可能有点早,但我正在 运行ning 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 设置预定义的自定义命令?

更新:

我创建了这个 basic YouTube tutorial and this more advanced one with a corresponding GitHub repo based on talkitbr's excellent and very helpful answer

起初他的回答超出了我的理解范围,所以我决定为像我这样的未来用户更详细地分解它。

您可以创建 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>

创建此定义后,您需要在应用程序启动时注册它:

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 [web archive].

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

此外,如果您有兴趣通过 Cortana window 回复用户,请查看此 post regarding Cortana in background [web archive]

你可以做的是写一个.bat 文件,然后将文件的快捷方式添加到文件夹:C:\ProgramData\Microsoft\Windows\Start Menu\Programs 您可以随意命名快捷方式,并通过说:"Hey Cortana open/start [shortcut name]" 触发关机。 确保 Cortana 只听你说话而不是 "pranked".