PromptDialog 用于未确定数量的问题
PromptDialog for an undetermined number of questions
我正在尝试编写一个允许用户创建请求的机器人(例如。我想买一件 T 恤或我想修理我的电脑)。根据他们选择的请求,他们将被问及与该请求相关的一系列问题;这些问题与他们选择的请求直接相关。目前我正在尝试遍历这个问题列表并使用 PromptDialog.Text 方法来获得答案。但是,这只询问列表中的最后一个问题,当给出答案时,我得到一个错误 invalid need: expected Wait, have Done 作为消息。
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);
}
}
我正在尝试编写一个允许用户创建请求的机器人(例如。我想买一件 T 恤或我想修理我的电脑)。根据他们选择的请求,他们将被问及与该请求相关的一系列问题;这些问题与他们选择的请求直接相关。目前我正在尝试遍历这个问题列表并使用 PromptDialog.Text 方法来获得答案。但是,这只询问列表中的最后一个问题,当给出答案时,我得到一个错误 invalid need: expected Wait, have Done 作为消息。
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);
}
}