c# - 如何从 Microsoft Bot Framework 中的 PromptValidator 获取 stepContext?

标签 c# botframework

我正在使用 DialogPromptBot sample .示例中有以下代码:

// Create the dialog set and add the prompts, including custom validation.
_dialogSet = new DialogSet(_accessors.DialogStateAccessor);
_dialogSet.Add(new NumberPrompt<int>(PartySizePrompt, PartySizeValidatorAsync));
_dialogSet.Add(new ChoicePrompt(LocationPrompt));
_dialogSet.Add(new DateTimePrompt(ReservationDatePrompt, DateValidatorAsync));

// Define the steps of the waterfall dialog and add it to the set.
WaterfallStep[] steps = new WaterfallStep[]
{
    PromptForPartySizeAsync,
    PromptForLocationAsync,
    PromptForReservationDateAsync,
    AcknowledgeReservationAsync,
};
_dialogSet.Add(new WaterfallDialog(ReservationDialog, steps));

它设置一些提示,验证用户的输入并保存他们的响应。我不喜欢代码的工作方式,因为用户的输入会在下一步中保存(即派对人数保存在位置提示中,位置保存在日期提示中)。我想在相应的验证步骤中保存用户的输入。这将消除请求之间的纠缠,并允许我在不进行大量无关更改的情况下重新排列问题。它还允许我对相同类型的问题使用相同的验证器。

如何从提示验证器访问 WaterfallStepContext?一旦我确定它是有效的,这将允许我保存用户的输入。此外,ChoicePrompt 也应该采用 Prompt Validator,但我似乎无法让它工作。它似乎有内置验证,但我也想在那里保存用户的输入。

最佳答案

不建议在执行提示值验证时存储任何状态。相反,提示的调用者有责任使用该值执行某些操作。具体来说,在使用 WaterfallDialog 时,推荐的模式是“下一步”始终负责存储上一步的结果。

想象一下,有一个数字提示可以确保选择 1 到 100 之间的值:

Add(new NumberPrompt<int>("myNumberPrompt", async (pvc, ct) => pvc.Succeeded && pvc.Recognized.Value > 1 && pvc.Recognized.Value < 100); 

然后是一些利用该提示的瀑布式步骤:

private async Task<DialogTurnResult> FirstStep(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    return await stepContext.PromptAsync("myNumberPrompt", new PromptOptions { Prompt = MessageFactory.Text("Please pick a number between 1 and 100") }, cancellationToken);    
}

private async Task<DialogTurnResult> SecondStep(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
   // Get the result of the previous step which will be the value from the NumberPrompt<int>
   var chosenNumber = (int)stepContext.Result;

   // Store the value into the WaterfallStepContext's Values dictionary
   stepContext.Values["ChosenNumber"] = chosenNumber;

   await stepContext.Context.SendActivityAsync($"Ok, {chosenNumber}, got it.");

   // ... more code here, maybe prompt for other values, whatever ...
}

关于c# - 如何从 Microsoft Bot Framework 中的 PromptValidator 获取 stepContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54484033/

相关文章:

c# - 如何在 .net core 2.0 中捕获 AccessViolationException

c# - 反汇编 C#

node.js - Azure Bot Framework(Node.js) - waterfall 在使用网络聊天的对话框中不起作用

botframework - 微软 Botframework : How to use Telegram Parameters or sending NewLine?

c# - .aspx 中的 URL 路由

c# - 使用 HttpWebRequest 类上传和下载时以百分比显示进度

azure - 在 Microsoft Azure 聊天机器人中发送图像附件

c# - Ms Bot 框架 - 在哪里存储数据?

azure - 虚拟助理 -> LUIS、QnA、调度程序最佳实践

c# - 如何使用 .NET 获取位于两个 '{' 括号'}' 之间的文本字符串?