如何将 Cortana 命令连接到自定义脚本? [英] How to connect Cortana commands to custom scripts?

查看:28
本文介绍了如何将 Cortana 命令连接到自定义脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 将处理该命令,如果没有任何内容,她只会在 bing 上搜索输入.但是,我希望能够说一些类似的话,例如

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

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

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

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

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

更新:

我创建了这个 YouTube 基础教程这个更高级的和相应的GitHub repo 基于talkitbr 的优秀且非常有用的答案下面.

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

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.

推荐答案

您可以创建命令供 Cortana 监听.这些命令需要在名为 语音命令定义的 XML 文件中进行描述 或 VCD.

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.

这是一个例子:

<?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 处注册:

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

然后重写App.OnActivated方法来处理事件触发时:

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;

教程展示完整代码[网络存档].

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

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

另外,如果您有兴趣通过 Cortana 窗口回复用户,请查看此 在后台发布有关 Cortana 的帖子 [网络存档].

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

这篇关于如何将 Cortana 命令连接到自定义脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆