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

南楼画角 提交于 2019-12-06 10:24:31

Actually it's quite easy to cancel a form. If you type "help" or "choices" you can see a list of builtin form commands, and one of these is "quit." There are many terms you can use to quit such as "finish" or "bye." If you want to define your own terms, you can can configure the form commands like this:

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

Keep in mind that when a form is canceled, a FormCanceledException<T> is thrown. If you don't want this to display a message like "Sorry, my bot code is having an issue," you can catch the exception like this:

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>
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!