C# MS Bot框架与决策的对话
我已经成功地得到了一个简单的“天气如何”的机器人,它使用Luis启动并在Skype和Twilio上运行,非常棒 我现在的任务是建立一个我只能想象的“对话”机器人 我已经看过了尽可能多的interweb上的例子,但我不确定如何开发它,我也不知道是否应该在我的场景中使用FormBuilder 这是我正在尝试做的部分工作的流程图 我已经让我的表格下到了“可用账单”的分支部分C# MS Bot框架与决策的对话,c#,botframework,C#,Botframework,我已经成功地得到了一个简单的“天气如何”的机器人,它使用Luis启动并在Skype和Twilio上运行,非常棒 我现在的任务是建立一个我只能想象的“对话”机器人 我已经看过了尽可能多的interweb上的例子,但我不确定如何开发它,我也不知道是否应该在我的场景中使用FormBuilder 这是我正在尝试做的部分工作的流程图 我已经让我的表格下到了“可用账单”的分支部分 我无法根据答案回答如何“改变方向”。 < P>你可以考虑使用FraveBuudRead字段的SETFEXT方法来决定下一步应该
我无法根据答案回答如何“改变方向”。 < P>你可以考虑使用FraveBuudRead字段的SETFEXT方法来决定下一步应该是基于另一个字段值。 你可以看看样品。在形式上;类似的事情正在发生
public static IForm<Order> BuildOrderForm()
{
return new FormBuilder<Order>()
.Field(nameof(RecipientFirstName))
.Field(nameof(RecipientLastName))
.Field(nameof(RecipientPhoneNumber))
.Field(nameof(Note))
.Field(new FieldReflector<Order>(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<Order>(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();
}
}
}
publicstaticiformbuildorderform()
{
返回新的FormBuilder()
.字段(收件人姓名)
.字段(姓名(收件人姓氏))
.字段(姓名(收件人电话号码))
.字段(名称(注释))
.Field(新的FieldReflector(名称(UseSavedSenderInfo))
.SetActive(state=>state.AskToUseSavedSenderInfo)
.SetNext((值、状态)=>
{
变量选择=(UseSaveInfoResponse)值;
if(selection==UseSaveInfoResponse.Edit)
{
state.SenderEmail=null;
state.SenderPhoneNumber=null;
返回新的NextStep(new[]{nameof(SenderEmail)});
}
其他的
{
返回新的NextStep();
}
}))
.Field(新的FieldReflector(名称(发送邮件))
.SetActive(state=>!state.UseSavedSenderInfo.HasValue | | state.UseSavedSenderInfo.Value==UseSaveInfoResponse.Edit)
SetNext先生(
(值,状态)=>(state.UseSavedSenderInfo==UseSaveInfoResponse.Edit)
?新的下一步(新[]{nameof(SenderPhoneNumber)})
:new NextStep()))
.Field(name of(SenderPhoneNumber),state=>!state.UseSavedSenderInfo.HasValue | | state.UseSavedSenderInfo.Value==UseSaveInfoResponse.Edit)
.Field(name of(SaveSenderInfo),state=>!state.UseSavedSenderInfo.HasValue | | | state.UseSavedSenderInfo.Value==UseSaveInfoResponse.Edit)
.Build();
}
}
}
正如我所怀疑的那样,这比我想象的要容易得多
这是我的解决方案
这是我的控制器:
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> 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);
}
//
///帖子:api/Messages
///接收来自用户的消息并回复
///
公共异步任务发布([FromBody]活动)
{
//检查活动是否为消息类型
if(activity!=null&&activity.GetActivityType()==ActivityTypes.Message)
{
wait Conversation.sendaync(活动,()=>newdialogs.BillSharkDialog());
}
其他的
{
HandleSystemMessage(活动);
}
返回新的HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
}
这里是“对话”,只有两个步骤
[Serializable]
public class BillSharkDialog : IDialog<object>
{
Model.Customer customer = new Model.Customer();
public async Task StartAsync(IDialogContext context)
{
context.Wait(WelcomeMessageAsync);
}
public async Task WelcomeMessageAsync(IDialogContext context, IAwaitable<IMessageActivity> 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<IMessageActivity> argument)
{
IMessageActivity message = await argument;
customer.customerName = message.Text;
await context.PostAsync($"Thanks {message.Text}. Now we need your email address?");
context.Wait(CaptureCustomerEmailAsync);
}
}
[可序列化]
公共类BillSharkDialog:IDialog
{
Model.Customer=新Model.Customer();
公共异步任务StartAsync(IDialogContext上下文)
{
Wait(WelcomeMessageAsync);
}
公共异步任务WelcomeMessageAsync(IDialogContext上下文,IAwaitable参数)
{
IMessageActivity message=等待参数;
wait context.PostAsync(“我们很高兴开始帮助您节省开支!让我们从您的名字开始吧?”);
Wait(CaptureCustomerNameAsync);
}
公共异步任务CaptureCustomeNameAsync(IDialogContext上下文,IAwaitable参数)
{
IMessageActivity message=等待参数;
customer.customerName=message.Text;
wait context.PostAsync($“谢谢{message.Text}。现在我们需要您的电子邮件地址吗?”);
Wait(CaptureCustomeMailAsync);
}
}
显然,您可以通过检查传入消息来更改路由
以下是一个例子:
public async Task DoesCustomerHaveBillAsync(IDialogContext context, IAwaitable<IMessageActivity> 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;
}
}
公共异步任务DoesCustomerHaveBillAsync(IDialogContext上下文,IAwaitable参数)
{
IMessageActivity message=等待参数;
开关(message.Text.ToLower())
{
案例“是”:
wait context.PostAsync($“太好了。请继续拍摄前两页的照片并将其附加到此对话。\n\n\n完成后,请发送消息“finished”。”;
Wait(CustomerHasBillAsync);
打破
案例“否”:
wait context.PostAsync($“没关系。您有提供商帐户的登录信息吗?”);
Wait(CustomerDoesntHaveBillAsync);
打破
违约:
wait context.PostAsync($“对不起,我没有理解。请回答‘是’或‘否’”);
Wait(doescustomerhavebillsync);
打破
}
}