c# – 针对未确定数量的问题的PromptDialog
作者:互联网
我正在尝试编写一个允许用户创建请求的机器人(例如.我想买一件T恤或者我想修理我的电脑).根据他们选择的要求,他们会被问到与该请求相关的一系列问题;这些问题与他们选择的请求直接相关.目前,我试图迭代这个问题列表,并使用PromptDialog.Text方法来获得答案.但是,这只询问列表中的最后一个问题,当给出答案时,我得到一个无效需要的错误:预期等待,完成消息.
private async Task ListOfferings(IDialogContext context, IAwaitable<string> result)
{
string name = await result;
var offerings = Offerings.Where((x) =>
CultureInfo.CurrentCulture.CompareInfo.IndexOf(x.Name, name,
CompareOptions.IgnoreCase) >= 0
);
if (offerings.Count() != 0)
{
PromptDialog.Choice(context, CreateOffering, offerings, "Which of these would you like?");
}
else
{
await context.PostAsync(string.Format("Could not find an offering with name {0}", name));
context.Wait(MessageReceived);
}
}
private async Task CreateOffering(IDialogContext context, IAwaitable<Offering> result)
{
var offering = result.GetAwaiter().GetResult();
await context.PostAsync(string.Format("***CREATING {0}***", offering.Name.ToUpper()));
foreach (var question in offering.Questions)
{
PromptDialog.Text(context, null, question.Question);
}
}
有可能做这样的事情,我想动态地决定用户在运行时会被问到的问题,还是我需要采用更静态的脚本方法?
解决方法:
你不能只在循环中多次调用PromptDialog.Text,因为你必须在每个问题后等待用户回复.尝试这样做:
private int questionNumber;
private Offering offering;
private async Task CreateOffering(IDialogContext context, IAwaitable<Offering> result)
{
offering = await result;
await context.PostAsync($"***CREATING {offering.Name.ToUpper()}***");
questionNumber = 0;
// fix access to a particular question in case Questions is not an IList
PromptDialog.Text(context, OnQuestionReply, offering.Questions[questionNumber].Question);
}
private async Task OnQuestionReply(IDialogContext context, IAwaitable<string> result)
{
var answer = await result;
// handle the answer for questionNumber as you need
questionNumber++;
// use Count instead of Length in case it is not an array
if (questionNumber < offering.Questions.Length)
{
PromptDialog.Text(context, OnQuestionReply, offering.Questions[questionNumber].Question);
}
else
{
// do what you need when all the questions are answered
await context.PostAsync("I have no more questions for this offering.");
context.Wait(MessageReceived);
}
}
标签:c,bots,botframework,luis,botbuilder 来源: https://codeday.me/bug/20190623/1268740.html