我正在使用BotFramework在.Net C#中创建一个聊天机器人。在我的一个对话框中,当我开始填写表单流时,我无法退出流表单,直到我将填充所有流的那一刻。存在退出和离开形式的可能性吗?
这是我的代码:
路易斯对话框.cs :
[LuisIntent("balance")]
public async Task balance(IDialogContext context, LuisResult result)
{
var balanca = new FormDialog<BalanceForm>(
new BalanceForm(),
BalanceForm.BuildForm,
FormOptions.PromptInStart,
result.Entities);
context.Call<BalanceForm>(balanca, BalanceCompleted);
平衡形式.cs
namespace BasicMultiDialog
{
[Serializable]
public class BalanceForm
{
[Prompt("What is your contract number?")]
public string contract;
public static IForm<BalanceForm> BuildForm()
{
OnCompletionAsyncDelegate<BalanceForm> wrapUpRequest = async
(context, state) =>
{
string wrapUpMessage = "Dear " + house.Firstname + "," + "your balance is " + house.Balance;
await context.PostAsync(wrapUpMessage);
}
};
return new FormBuilder<BalanceForm>().Message
("We have to ask you some information")
.Field(nameof(contract), validate: async (state, response) =>
{
var result = new ValidateResult();
return result;
}
})
.OnCompletion(wrapUpRequest)
//.Confirm("Are you sure: Yes or No ")
.Build();
}
}
}
实际上,取消表单很容易。如果您键入"帮助"或"选择",您可以看到内置表单命令的列表,其中之一是"退出"。您可以使用许多术语来退出,例如"完成"或"再见"。如果要定义自己的术语,可以按如下方式配置表单命令:
var builder = new FormBuilder<BalanceForm>().Message
("We have to ask you some information")
.Field(nameof(contract), validate: async (state, response) =>
{
var result = new ValidateResult();
return result;
})
.OnCompletion(wrapUpRequest)
// Set the command term configuration on its own line
builder.Configuration.Commands[FormCommand.Quit].Terms = new[] { "exit", "cancel" };
return builder.Build();
请记住,当表单被取消时,会抛出一个FormCanceledException<T>
。如果不希望显示类似"抱歉,我的机器人代码有问题"之类的消息,可以捕获如下所示的异常:
var balanca = new FormDialog<BalanceForm>(
new BalanceForm(),
BalanceForm.BuildForm,
FormOptions.PromptInStart,
result.Entities)
.Catch<BalanceForm, FormCanceledException<BalanceForm>>((dialog, ex) =>
{
// Handle the cancellation here and return an IDialog<BalanceForm>
});