MS Bot框架与决策的对话

我已经成功地使用Luis在Skype和Twilio上运行了一个简单的“天气是什么”机器人,它非常棒。

我现在的任务是设置我只能想象描述为“对话”机器人的东西。

我已经查看了尽可能多的示例,我可以在interweb上找到但我不确定如何开发它,我不知道我是否应该使用FormBuilder来实现我的场景。

这是我想要做的部分内容的流程图…

在此处输入图像描述

我已经将我的表格工作到了“Bill Available”分支的部分……

我无法根据答案找出如何“改变方向”的方法。

据我所知,这一点比我想象的要容易得多。

这是我的解决方案……

这是我的控制器:

///  /// POST: api/Messages /// Receive a message from a user and reply to it ///  public async Task Post([FromBody]Activity activity) { // check if activity is of type message if (activity != null && activity.GetActivityType() == ActivityTypes.Message) { await Conversation.SendAsync(activity, () => new Dialogs.BillSharkDialog()); } else { HandleSystemMessage(activity); } return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted); } 

这里只有两步的“对话”……

 [Serializable] public class BillSharkDialog : IDialog { Model.Customer customer = new Model.Customer(); public async Task StartAsync(IDialogContext context) { context.Wait(WelcomeMessageAsync); } public async Task WelcomeMessageAsync(IDialogContext context, IAwaitable argument) { IMessageActivity message = await argument; await context.PostAsync("We're excited to start helping you save! Let's start by getting your name?"); context.Wait(CaptureCustomerNameAsync); } public async Task CaptureCustomerNameAsync(IDialogContext context, IAwaitable argument) { IMessageActivity message = await argument; customer.customerName = message.Text; await context.PostAsync($"Thanks {message.Text}. Now we need your email address?"); context.Wait(CaptureCustomerEmailAsync); } } 

您可以通过检查传入消息显然更改路由。

这是一个例子:

 public async Task DoesCustomerHaveBillAsync(IDialogContext context, IAwaitable argument) { IMessageActivity message = await argument; switch (message.Text.ToLower()) { case "yes": await context.PostAsync($"Great. Go ahead and take a picture of the first couple of pages and attach them to this conversation.\n\n\nWhen you have finished, please send the message 'finished'."); context.Wait(CustomerHasBillAsync); break; case "no": await context.PostAsync($"That's OK. Do you happen to have the login information for your provider account?"); context.Wait(CustomerDoesntHaveBillAsync); break; default: await context.PostAsync($"Sorry, I didn't undestand. Please reply with 'yes' or 'no'."); context.Wait(DoesCustomerHaveBillAsync); break; } } 

您可以考虑使用FormBuilder字段的SetNext方法根据另一个字段值来决定接下来会发生什么。

您可以查看ContosoFlowers示例。 在订单表格中; 正在做类似的事情。

  public static IForm BuildOrderForm() { return new FormBuilder() .Field(nameof(RecipientFirstName)) .Field(nameof(RecipientLastName)) .Field(nameof(RecipientPhoneNumber)) .Field(nameof(Note)) .Field(new FieldReflector(nameof(UseSavedSenderInfo)) .SetActive(state => state.AskToUseSavedSenderInfo) .SetNext((value, state) => { var selection = (UseSaveInfoResponse)value; if (selection == UseSaveInfoResponse.Edit) { state.SenderEmail = null; state.SenderPhoneNumber = null; return new NextStep(new[] { nameof(SenderEmail) }); } else { return new NextStep(); } })) .Field(new FieldReflector(nameof(SenderEmail)) .SetActive(state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit) .SetNext( (value, state) => (state.UseSavedSenderInfo == UseSaveInfoResponse.Edit) ? new NextStep(new[] { nameof(SenderPhoneNumber) }) : new NextStep())) .Field(nameof(SenderPhoneNumber), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit) .Field(nameof(SaveSenderInfo), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit) .Build(); } } }