我仍在尝试掌握机器人框架 v4 和对话框的概念。我正在尝试创建一个与 QnAMaker 和 LUIS 集成的简单机器人。两种集成都单独工作,但我无法将它们都集成。我也是第一次使用 ASP.NET Core。所以我的问题也可能与此有关。
所以我根据这个示例创建了一个机器人。
我有一个继承 Dialog 类的基本对话框。基类公开了一个子对话框类继承的抽象函数。子类称为 RootDialog。
函数对话框库:
public FunctionDialogBase(string dialogId, IConfiguration configuration, ILogger logger) : base(dialogId)
{
Configuration = configuration;
Logger = logger;
}
public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dialogContext, object options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return await RunStateMachineAsync(dialogContext, cancellationToken).ConfigureAwait(false);
}
public override async Task<DialogTurnResult> ContinueDialogAsync(DialogContext dialogContext, CancellationToken cancellationToken = default(CancellationToken))
{
return await RunStateMachineAsync(dialogContext, cancellationToken).ConfigureAwait(false);
}
//abstract method
protected abstract Task<(object newState, IEnumerable<Activity> output, object result)> ProcessAsync(object oldState,
Activity activity,
DialogContext dialogContext,
SPEntityDetails spEntityDetails,
CancellationToken cancellationToken);
private async Task<DialogTurnResult> RunStateMachineAsync(DialogContext dialogContext, CancellationToken cancellationToken)
{
var (newState, output, result) = await ProcessAsync(oldState, dialogContext.Context.Activity, dialogContext, spEntityDetails, cancellationToken).ConfigureAwait(false);
//do some more logic here
}
}
RootDialog 从它的抽象方法实现中调用另一个名为 CreateSiteDialog 的对话框,如下所示:
根对话框:
protected override async Task<(object newState, IEnumerable<Activity> output, object result)> ProcessAsync(
object oldState,
Activity activity,
DialogContext dialogContext,
SPEntityDetails spEntityDetails,
CancellationToken cancellationToken)
{
dialogContext.Dialogs.Add(new CreateSiteDialog());
await dialogContext.BeginDialogAsync(nameof(CreateSiteDialog),spEntityDetails, cancellationToken);
return (null, new Activity[] { activity.CreateReply("Site created successfully.") }, null);
}
最后是有问题的 CreateSiteDialog。此对话框遵循瀑布模型,理论上应该从一个瀑布步骤移动到下一个瀑布步骤直到结束。这没有发生。当我在第一步中使用提示时,它会执行并退出瀑布对话框。下一个瀑布步骤永远不会执行。
创建站点对话框:
public class CreateSiteDialog : CancelAndHelpDialog
{
public CreateSiteDialog() : base(nameof(CreateSiteDialog))
{
AddDialog(new TextPrompt(nameof(TextPrompt)));
AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
ConfirmStepAsync,
FinalStepAsync
}));
// The initial child Dialog to run.
InitialDialogId = nameof(WaterfallDialog);
}
private async Task<DialogTurnResult> ConfirmStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var spEntityDetails = (SPEntityDetails)stepContext.Options;
var msg = @"Creating site: http://yoursiteurl/" + spEntityDetails.SiteName + ". Confirm Yes/No.";
return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = MessageFactory.Text(msg) }, cancellationToken);
}
private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
//handle propmt result from user action
}
}
也没有错误。所以我假设我在使用对话框时做错了什么。请。任何帮助将不胜感激。
我正在使用 Visual Studio 2017、Bot Framework Emulator、Bot framework V4 和 Core Bot 项目。