在堆栈中添加一个对话框

问题描述 投票:0回答:1

我有一个简单的机器人,它可以监听facebook事件的触发(不是消息),当它收到触发时,它应该启动一个新的Dialog(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个回答
0
投票

有两件事。

  1. 它没有返回对话框的原因是: OnMessageActivity 在用户响应时被触发,并调用了 Dialog.RunAsync 在你的主对话框上。

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

我建议:

  1. 了解如何 CoreBot实现其对话框特别是处理中断的CancelAndHelp对话框。查看如何 BookingDialog扩展了CancelAndHelpDialog。 所以,每次BookingDialog被调用。CancelAndHelpDialog检查是否需要中断。
  2. 在您的ActivityHandler中,调用正常的 Dialog.Run,而不是BeginDialog
  3. 创建一个处理中断和事件的对话框;它应该分析turnContext,如果它看到了 Activity.Type === ActivityTypes.Event 以及其他任何你需要的条件)。然后 它称之为 BeginDialog. 请务必使用 AddDialog 在这个对话框的构造函数中。
© www.soinside.com 2019 - 2024. All rights reserved.