gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:27:59 26 4
gpt4 key购买 nike

现在问这个问题可能有点早,但我正在运行 Windows 10 技术预览版 10122。我想将 Cortana 设置为具有自定义命令。她的工作方式如下:

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

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

Hey Cortana, I'm going to bed now

并让输入我现在要 sleep 触发运行批处理脚本、VBScript、命令或任何某种基本上执行以下操作的自定义响应。

C:\> shutdown -s

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

更新:

我创建了这个basic YouTube tutorialthis more advanced one相应的GitHub repo基于talkitbr的优秀且非常有帮助的答案below

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

最佳答案

您可以创建供 Cortana 监听的命令。这些命令需要在名为 Voice Command Definitions 的 XML 文件中进行描述。或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>

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

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 窗口响应用户,请检查此 post regarding Cortana in background [web archive] .

关于scripting - 如何将 Cortana 命令连接到自定义脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com