首页 > 解决方案 > 将对话框添加到堆栈

问题描述

我有一个简单的机器人来监听 facebook 事件触发器(不是消息)。当它获得触发器时,它应该启动一个新的对话框(RegisterPledgeDialog)并将其推送到堆栈中。但我不知道怎么做?

public class DialogBot<T> : ActivityHandler where T : Dialog

protected override async Task OnEventAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
       // How do i start a new Dialog and push it to the top of an exiting dialog stack?
       // The code below is what I tried. It starts the new Dialog but doesn't return to it after the turn

       var set = new DialogSet();
       set.Add(_pledgeDialog);
       DialogContext dc = new DialogContext(set , turnContext, new DialogState());
       await dc.BeginDialogAsync(nameof(RegisterPledgeDialog), null, cancellationToken);

}

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
      Logger.LogInformation("Running dialog with Message Activity.");  
      await Dialog.RunAsync(turnContext, _conversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);          
}

标签: c#botframework

解决方案


两件事情:

  1. 它不返回对话框的原因是因为OnMessageActivity在用户响应时触发,并且会调用Dialog.RunAsync您的主对话框。

  2. 对话框不能/不应该动态添加到机器人中。它们必须添加到构造函数中,然后使用BeginDialogAsync().

我建议:

  1. 了解CoreBot 如何实现其对话框,尤其是处理中断的 CancelAndHelp 对话框。查看BookingDialog 如何扩展 CancelAndHelpDialog以便每次调用 BookingDialog 时,CancelAndHelpDialog 检查是否需要中断
  2. 在您的 ActivityHandler 中,调用 normal Dialog.Run,而不是 BeginDialog
  3. 创建一个处理中断和/或事件的对话框;它应该分析 turnContext 并且如果它看到Activity.Type === ActivityTypes.Event(以及您需要的任何其他条件),那么它会调用BeginDialog. 确保AddDialog在此对话框的构造函数中使用。

推荐阅读