【发布时间】:2018-11-09 17:24:42
【问题描述】:
我正在学习 Microsoft Bot Framework,因此我正在学习教程并处理一些示例对话框。我正在尝试编写一个计算器机器人。 (机器人要求一个整数、一个操作和另一个整数)。
但是,我无法让机器人真正询问用户的输入。如果用户在启动对话框后键入任何内容,则响应为$invalid need: expected Call, have Poll。我看到一些问题说你不能在同一个函数调用中使用提示符和context.Wait,但是我没有找到任何关于如何处理这个问题的信息。
public class CalculatorDialog : IDialog<object>
{
protected readonly String[] operators = { "+", "-", "*", "/" };
private long Left { get; set; }
private long Right {get; set; }
private String Operator { get; set; }
public async Task StartAsync(IDialogContext context)
{
context.Wait(Calculate);
}
private async Task Calculate(IDialogContext context,
IAwaitable<IMessageActivity> result)
{
await result;
PromptDialog.Number(
context,
async (ctx, lhs) => { Left = await lhs; },
"Left hand side"
);
PromptDialog.Choice<string>(
context,
async (ctx, op) => { Operator = await op; },
new PromptOptions<string>("Operator:", options: operators)
);
PromptDialog.Number(
context,
async (ctx, rhs) => { Right = await rhs; },
"Right hand side"
);
long res;
switch (Operator) { /* res = Left Operator Right */}
await context.PostAsync($"The result is: ${res}");
}
}
调用这个的代码是
switch (activity.Text)
{
case "math":
context.Call(new CalculatorDialog(), this.ResumeAfterSubDialog);
break;
}
ResumeAferSubDialog 继续主循环。
【问题讨论】:
标签: c# botframework