我刚刚接触 Bot Framework 几天,非常陌生。我一直在努力理解管理状态,老实说,我就是不明白。最近似乎关于此的框架和建议发生了很大变化,似乎没有任何明确的建议或示例。
这page说:
Save the information to bot state. This would require you to design your dialog to have access to the bot's state property accessors.
但是没有例子说明如何实现这一点。
我的一个 Waterfall 对话框的最后一步如下所示:
AddStep(async (stepContext, cancellationToken) =>
{
var response = stepContext.Result as FoundChoice;
stepContext.Values["maxPrice"] = response;
return await stepContext.BeginDialogAsync(SearchDialog.Id, null, cancellationToken);
});
它基本上启动了一个新对话框,我想将收集的数据从这个对话框传递到 SearchDialog
通过传递对象,或者最好将其保存到我的 BotAccessors
中然后是 SearchDialog
检索并使用它。
所有 MS 示例都将瀑布步骤定义为 async
IBot
上的方法类(class)。这也不是他们推荐的将机器人对话放在一起的方式,这使得该示例总体上毫无用处。
此外,似乎连 Microsoft v4 文档都已过时,例如 this doc ,这仍然告诉我们使用已弃用的代码,例如:
options.State.Add(new ConversationState(storage));
不幸的是,目前关于这个主题的文档似乎更令人困惑而不是帮助。管理这种状态的最佳方法是什么?
最佳答案
注意:Basic Bot 样本已被 Core Bot 样本取代,因此此答案已过时
在 samples repo 中查看基 native 器人的设置方式。或通过 creating a basic bot来自 Azure 中的模板。
状态属性访问器在 BasicBot
中声明类:
private readonly IStatePropertyAccessor<GreetingState> _greetingStateAccessor;
然后它被分配给 BasicBot
constructor :
_greetingStateAccessor = _userState.CreateProperty<GreetingState>(nameof(GreetingState));
然后是passed到 GreetingDialog
构造函数:
Dialogs.Add(new GreetingDialog(_greetingStateAccessor, loggerFactory));
然后将其分配给 GreetingDialog
的属性类:
UserProfileAccessor = userProfileStateAccessor ?? throw new ArgumentNullException(nameof(userProfileStateAccessor));
然后在 GreetingDialog
类的许多地方使用 GetAsync
和 SetAsync
方法。例如:
var greetingState = await UserProfileAccessor.GetAsync(stepContext.Context, () => null);
关于c# - 在对话框中使用机器人状态访问器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53778670/