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

Cha*_*ton 40 scripting nlp cortana windows-10

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

Hey Cortana, <she'll listen and process this command>
Run Code Online (Sandbox Code Playgroud)

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

Hey Cortana, I'm going to bed now
Run Code Online (Sandbox Code Playgroud)

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

C:\> shutdown -s
Run Code Online (Sandbox Code Playgroud)

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

更新:

我创建了这个基本的YouTube教程,这个更高级的教程与相应的GitHub回购基于talkitbr的优秀且非常有用的答案如下.

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

小智 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>
Run Code Online (Sandbox Code Playgroud)

创建此定义后,您需要在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后覆盖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;
Run Code Online (Sandbox Code Playgroud)

本教程显示了完整的代码.

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

此外,如果您有兴趣通过Cortana窗口回复用户,请在后台查看有关Cortana的帖子.