如何在每次输入退出或取消时中断表单流?

How to break formflow in every moment i type exit or cancel?

我正在使用 BotFramework 在 .Net C# 中创建一个聊天机器人。在我的一个对话框中,当我开始填写表单流程时,我无法退出流程,直到我将填写所有流程为止。是否存在退出和离开表格的可能性?

这是我的代码:

LuisDialog.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);

BalanceForm.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();
          }

         }
      }

其实取消表格很容易。如果您键入 "help" 或 "choices",您可以看到内置表单命令列表,其中之一是 "quit." 您可以使用许多术语来退出,例如 "finish"或 "bye." 如果您想定义自己的术语,可以像这样配置表单命令:

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>。如果你不希望它显示像 "Sorry, my bot code is having an issue," 这样的消息,你可以像这样捕获异常:

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>
    });