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

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

问题描述

这可能还为时过早,但是我正在运行Windows 10 Technical Preview Build10122.我想将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教程 下方.

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

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收听. 这些命令需要在名为语音命令定义或VCD的XML文件中进行描述

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.

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

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