带有LUIS和WaterFall对话框的Azure Bot Framework Bot以异常方式执行。意外的对话流

Jam*_*son 6 c# botframework azure-language-understanding azure-bot-service

我正在为我自己的目的,为Bot Framework V4(使用LUIS)破解GitHub'CoreBot'示例代码,并且在响应和瀑布对话框步骤的执行方式上遇到了麻烦。

我有一个顶级对话框。我的期望是,该对话框基于输入对LUIS进行初始调用,并基于该输入路由到其他对话框。目前,只能问候机器人并报告危险。我的对话框设置如下(忽略BookingDialog,它是示例的一部分)。

public MainDialog(IConfiguration configuration, ILogger<MainDialog> logger)
    : base(nameof(MainDialog))
{
    Configuration = configuration;
    Logger = logger;

    AddDialog(new TextPrompt(nameof(TextPrompt)));
    AddDialog(new BookingDialog());
    AddDialog(new HazardDialog());
    AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
    {
        MainStepAsync,
        EndOfMainDialogAsync
    }));

    // The initial child Dialog to run.
    InitialDialogId = nameof(WaterfallDialog);
}
Run Code Online (Sandbox Code Playgroud)

我的期望是MainStepAsync被执行,并运行以下命令:

   private async Task<DialogTurnResult> MainStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {

        CoreBot.StorageLogging.LogToTableAsync($"Main step entered. I am contacting LUIS to determine the intent");

        string what_i_got = await LuisHelper.ExecuteMyLuisQuery(Configuration, Logger, stepContext.Context, cancellationToken);

        CoreBot.StorageLogging.LogToTableAsync($"LUIS intent matched: {what_i_got}");
        //await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text($"Hi! My name is Sorella. Your intent was {what_i_got}") }, cancellationToken);

        switch (what_i_got)
        {
            case "Hazard":
                StorageLogging.LogToTableAsync($"We have been asked to report a hazard");
                StorageLogging.LogToTableAsync(stepContext);
                var hazardDetails = new ResponseSet.Hazard();
                return await stepContext.BeginDialogAsync(nameof(HazardDialog), hazardDetails, cancellationToken);
            case "Greeting":
                StorageLogging.LogToTableAsync($"We have been asked to provide a greeting. After this greeting the waterfall will end");
                StorageLogging.LogToTableAsync(stepContext);
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("Hi there! :). What can I help you with today? "), cancellationToken);
                return await stepContext.EndDialogAsync(null, cancellationToken);
            default:
                StorageLogging.LogToTableAsync($"We got an intent we haven't catered for. After this the waterfall will end");
                StorageLogging.LogToTableAsync(stepContext);
                return await stepContext.NextAsync(null, cancellationToken);
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果意图是危险,则开始“危险对话”。否则,如果他们在问候机器人,请打个招呼并结束此顶级瀑布对话框。如果将用户路由到HazardDialog,请启动下一个Waterfall,其设置如下:

   public class HazardDialog : CancelAndHelpDialog 
    {
        public HazardDialog()
        : base(nameof(HazardDialog))
        {
            AddDialog(new TextPrompt(nameof(TextPrompt)));
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                GetInitialHazardInfoAsync,
                GetHazardUrgencyAsync,
                FinalStepAsync
            }));
            // The initial child Dialog to run.
            InitialDialogId = nameof(WaterfallDialog);
        }
Run Code Online (Sandbox Code Playgroud)

首先要求他们提供危害的描述:

    private async Task<DialogTurnResult> GetInitialHazardInfoAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        CoreBot.StorageLogging.LogToTableAsync("Entered hazard step 1. Asking for hazard type");

        var hazardDetails = (ResponseSet.Hazard)stepContext.Options;

        hazardDetails.HazardType = (string)stepContext.Result;

        if (hazardDetails.HazardType == null)
        {
            CoreBot.StorageLogging.LogToTableAsync($"No hazard type provided. Asking");
            return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("What kind of hazard would you like to report? Provide me a brief description") }, cancellationToken);
        }
        else
        {
            CoreBot.StorageLogging.LogToTableAsync($"Hazard provided. Moving on");
            return await stepContext.NextAsync(hazardDetails.HazardType, cancellationToken);
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后紧急:

   private async Task<DialogTurnResult> GetHazardUrgencyAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        CoreBot.StorageLogging.LogToTableAsync($"Entered hazard step 2. Asking for urgency");
        var hazardDetails = (ResponseSet.Hazard)stepContext.Options;
        hazardDetails.HazardType = (string)stepContext.Result;
        var hazardAsJson = JsonConvert.SerializeObject(hazardDetails);
        StorageLogging.LogToTableAsync(hazardAsJson);

        if (hazardDetails.HarzardUrgency == null)
        {
            CoreBot.StorageLogging.LogToTableAsync($"No urgency provided. Asking");
            return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text($"Thanks. So your hazard is {hazardDetails.HazardType}? How urgent is it?") }, cancellationToken);
        }
        else
        {
            CoreBot.StorageLogging.LogToTableAsync($"Urgency given. We're all done");
            var guid = Guid.NewGuid();
            var ticketId = "HAZ" + Convert.ToString(guid).ToUpper().Substring(1,4);
            await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Thanks! I've got all the informatio I need. I'll raise this with the API team on your behalf. Your Ticket ID is: {ticketId} "), cancellationToken);
            return await stepContext.NextAsync(cancellationToken, cancellationToken);
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我们既有紧迫感又有类型,那么我们“举起票”并进入最后一个步骤,该步骤刚刚结束堆栈。

  private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        CoreBot.StorageLogging.LogToTableAsync($"Entered hazard step 3. Final step");
        var hazardDetails = (ResponseSet.Hazard)stepContext.Options;
        hazardDetails.HarzardUrgency = (string)stepContext.Result;
        var hazardAsJson = JsonConvert.SerializeObject(hazardDetails);
        StorageLogging.LogToTableAsync(hazardAsJson);
        return await stepContext.EndDialogAsync(hazardDetails, cancellationToken);
    }
Run Code Online (Sandbox Code Playgroud)

我的期望是结束HarzardDialog,然后应返回到“ Parent”瀑布对话框的下一步,即EndOfMainDialogAsync,它只是说我们已经完成了,我还能做些什么来帮助您?

  private async Task<DialogTurnResult> EndOfMainDialogAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        StorageLogging.LogToTableAsync($"Ending the main dialog");
        StorageLogging.LogToTableAsync(stepContext);
        await stepContext.Context.SendActivityAsync(MessageFactory.Text("Ok, I think we're all done with that. Can I do anything else to help?"), cancellationToken);
        return await stepContext.EndDialogAsync(null, cancellationToken);
    }
Run Code Online (Sandbox Code Playgroud)

但是,在我实际的对话中,该流最终会出现异常,实际上(如果您查看下面的对话)是从子瀑布中触发GetHazardUrgencyAsync,然后从父瀑布中触发MainStepAsync,然后是第二次从子瀑布中触发GetHazardUrgencyAsync?

在此处输入图片说明

更新:根据建议,我已经更新了WaterFallDialogs以使其具有唯一的名称,并进行了重新测试。我仍然有错误的行为。请参见下面的屏幕截图:

在此处输入图片说明

我的期望是,在描述了危害之后,我接下来被问到它有多紧急。相反,我在“块”中得到以下对话框响应。

  • 问它有多紧急(正确)
  • 再次欢迎我(不正确-这是来自父瀑布)
  • 再次询问紧急程度(不正确)

我在这里迷路了。我会从代码中想象/想到,子瀑布对话框在返回到父对话框之前需要完全实现/结束。

Mik*_*ney 2

对话名称在机器人内是全局唯一的。您的两个瀑布对话框都被命名为“WaterfallDialog”。所以你基本上是在动态地更换瀑布。

将它们更改为唯一的名称。

    AddDialog(new WaterfallDialog("MainDialogWaterfall", new WaterfallStep[]
    {
        MainStepAsync,
        EndOfMainDialogAsync
    }));
Run Code Online (Sandbox Code Playgroud)
    AddDialog(new WaterfallDialog("HazardInfoWaterfallDialog", new WaterfallStep[]
    {
        GetInitialHazardInfoAsync,
        GetHazardUrgencyAsync,
        FinalStepAsync
    }));
Run Code Online (Sandbox Code Playgroud)