c# - 将对话框添加到堆栈
问题描述
我有一个简单的机器人来监听 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);
}
解决方案
两件事情:
它不返回对话框的原因是因为
OnMessageActivity
在用户响应时触发,并且会调用Dialog.RunAsync
您的主对话框。对话框不能/不应该动态添加到机器人中。它们必须添加到构造函数中,然后使用
BeginDialogAsync()
.
我建议:
- 了解CoreBot 如何实现其对话框,尤其是处理中断的 CancelAndHelp 对话框。查看BookingDialog 如何扩展 CancelAndHelpDialog以便每次调用 BookingDialog 时,CancelAndHelpDialog 检查是否需要中断
- 在您的 ActivityHandler 中,调用 normal
Dialog.Run
,而不是 BeginDialog - 创建一个处理中断和/或事件的对话框;它应该分析 turnContext 并且如果它看到
Activity.Type === ActivityTypes.Event
(以及您需要的任何其他条件),那么它会调用BeginDialog
. 确保AddDialog
在此对话框的构造函数中使用。
推荐阅读
- ruby-on-rails - 单击另一个用户的用户名链接到我的个人资料而不是用户的个人资料
- javascript - 主体中的 Javascript 代码不适用于角度模板
- csv - Hive 将数据从 csv 导入表中的错误列
- android - 从后端服务器返回的非同质 JSON 数据
- qt - 如何同时获得 qgraphicsscene 和 qgraphicsitem 滴?
- sql - 是否可以限制 SQL 视图的值?
- php - “在哪里”在 Laravel 的选择查询中不起作用
- python - 如何在 Python 中处理用户输入?
- sql - 如何使用单个 Insert 语句插入数十万条记录?
- r - grep 和/或收集数据混淆