我能够找到一个可行的解决方案来解决这个问题。更改的详细信息可以在此Commit
中找到
我不得不重写声明类型“Microsoft.BeginSkill”的默认功能。具体来说,我覆盖了“RepromptDialogAsync”方法。
我采取的步骤:
添加 BeginSkillNonRePrompting
此类覆盖声明性类型“BeginSkill”的默认行为,即 .dialog 文件中的 $kind = Microsoft.BeginSkill。它检查技能的配置以确定是否应允许重新提示,如果不允许则阻止。
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Adaptive.Actions;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Extensions.Configuration;
namespace Microsoft.BotFramework.Composer.WebApp.Dialogs
{
public class BeginSkillNonRePrompting : BeginSkill
{
private readonly string _dialogOptionsStateKey = $"{typeof(BeginSkill).FullName}.DialogOptionsData";
public BeginSkillNonRePrompting([CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
: base(callerPath, callerLine)
{
}
public override Task RepromptDialogAsync(ITurnContext turnContext, DialogInstance instance, CancellationToken cancellationToken = default)
{
//get the skill endpoint - this contains the skill name from configuration - we need this to get the related skill:{SkillName}:disableReprompt value
var skillEndpoint = ((SkillDialogOptions)instance.State["Microsoft.Bot.Builder.Dialogs.Adaptive.Actions.BeginSkill.DialogOptionsData"]).Skill.SkillEndpoint;
//get IConfiguration so that we can use it to determine if this skill should be reprompting or not
var config = turnContext.TurnState.Get<IConfiguration>() ?? throw new NullReferenceException("Unable to locate IConfiguration in HostContext");
//the id looks like this:
//BeginSkillNonRePrompting['=settings.skill['testLoopingPrompt'].msAppId','']
//parse out the skill name
var startingSearchValue = "=settings.skill['";
var startOfSkillName = instance.Id.IndexOf(startingSearchValue) + startingSearchValue.Length;
var endingOfSkillName = instance.Id.Substring(startOfSkillName).IndexOf("']");
var skillName = instance.Id.Substring(startOfSkillName, endingOfSkillName);
//if we do not want to reprompt call EndDialogAsync instead of RepromptDialogAsync
if (Convert.ToBoolean(config[$"skill:{skillName}:disableReprompt"]))
{
//this does not actually appear to remove the dialog from the stack
//if I call "Call Skill 1" again then this line is still hit
//so it seems like the dialog hangs around but shouldn't actually show to the user
//not sure how to resolve this but it's not really an issue as far as I can tell
return EndDialogAsync(turnContext, instance, DialogReason.EndCalled, cancellationToken);
}
else
{
LoadDialogOptions(turnContext, instance);
return base.RepromptDialogAsync(turnContext, instance, cancellationToken);
}
}
private void LoadDialogOptions(ITurnContext context, DialogInstance instance)
{
var dialogOptions = (SkillDialogOptions)instance.State[_dialogOptionsStateKey];
DialogOptions.BotId = dialogOptions.BotId;
DialogOptions.SkillHostEndpoint = dialogOptions.SkillHostEndpoint;
DialogOptions.ConversationIdFactory = context.TurnState.Get<SkillConversationIdFactoryBase>() ?? throw new NullReferenceException("Unable to locate SkillConversationIdFactoryBase in HostContext");
DialogOptions.SkillClient = context.TurnState.Get<BotFrameworkClient>() ?? throw new NullReferenceException("Unable to locate BotFrameworkClient in HostContext");
DialogOptions.ConversationState = context.TurnState.Get<ConversationState>() ?? throw new NullReferenceException($"Unable to get an instance of {nameof(ConversationState)} from TurnState.");
DialogOptions.ConnectionName = dialogOptions.ConnectionName;
// Set the skill to call
DialogOptions.Skill = dialogOptions.Skill;
}
}
}
添加 AdaptiveComponentRegistrationCustom
该类允许将 AdaptiveBotComponentCustom 添加到 Startup.cs 中的 ComponentRegistration
using System;
using System.Linq;
using Microsoft.Bot.Builder.Dialogs.Adaptive;
using Microsoft.Bot.Builder.Dialogs.Adaptive.Actions;
using Microsoft.Bot.Builder.Dialogs.Declarative;
using Microsoft.Bot.Builder.Dialogs.Declarative.Obsolete;
using Microsoft.BotFramework.Composer.WebApp.Dialogs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.BotFramework.Composer.WebApp.Components
{
/// <summary>
/// <see cref="ComponentRegistration"/> implementation for adaptive components.
/// </summary>
[Obsolete("Use `AdaptiveBotComponent` instead.")]
public class AdaptiveComponentRegistrationCustom : DeclarativeComponentRegistrationBridge<AdaptiveBotComponentCustom>
{
}
}
添加 AdaptiveBotComponentCustom
这个类覆盖了默认的 AdaptiveBotComponent 行为并用 DeclarativeType 替换了 DeclarativeType 中的依赖注入
using System;
using System.Linq;
using Microsoft.Bot.Builder.Dialogs.Adaptive;
using Microsoft.Bot.Builder.Dialogs.Adaptive.Actions;
using Microsoft.Bot.Builder.Dialogs.Declarative;
using Microsoft.Bot.Builder.Dialogs.Declarative.Obsolete;
using Microsoft.BotFramework.Composer.WebApp.Dialogs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.BotFramework.Composer.WebApp.Components
{
public class AdaptiveBotComponentCustom : AdaptiveBotComponent
{
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
base.ConfigureServices(services, configuration);
//this is the replace the DeclarativeType BeginSkill so that we can handle the reprompting situation differently
//in some cases we don't want to reprompt when there has been an interruption
//this will be configured in appSettings.json under skill:{SkillName}:disableReprompt
var beginSkillDI = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(DeclarativeType<BeginSkill>));
if (beginSkillDI != null)
{
services.Remove(beginSkillDI);
}
//adding the cust class which will deal with use the configuration setting skill:{SkillName}:disableReprompt
services.AddSingleton<DeclarativeType>(sp => new DeclarativeType<BeginSkillNonRePrompting>(BeginSkill.Kind));
}
}
}
添加 appSettings.json
添加将触发功能以防止重新提示的设置
{
"skill": {
"testLoopingPrompt": {
"disableReprompt": "true"
}
}
}
修改 Program.cs
添加了一行来加载 appSettings.json 文件,其中包含阻止重新提示特定技能的设置。
builder.AddJsonFile("appSettings.json");
修改 Startup.cs
更改配置 ComponentRegistration 的部分。这将使用新的 BeginSkillNonRePrompting 类而不是普通的 BeginSkill 类。
ComponentRegistration.Add(new AdaptiveComponentRegistration());
到
ComponentRegistration.Add(new AdaptiveComponentRegistrationCustom());
遗留问题
虽然这解决了用户方面的问题,但“RepromptDialogAsync”中的“EndDialogAsync”实际上并没有使对话框脱离堆栈。它仍然继续进入“RepromptDialogAsync”方法,尽管从用户的角度来看它总是什么都不做。我现在只有简短的对话。我将很快进入与 if / switch / 多个技能 / 多个用户提示的更长对话,因此需要注意与此更改相关的任何问题。