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

谁说胖子不能爱 提交于 2019-12-10 11:29:23

问题


I'm creating a chatbot in .Net C# using BotFramework. In one of my dialog when i start to fill a form flow i cannot exit from flowform till in the moment i will fill all the flow . Exist any possibility to exit and to leave form ?

This is my code :

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

         }
      }

回答1:


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


来源:https://stackoverflow.com/questions/52622773/how-to-break-formflow-in-every-moment-i-type-exit-or-cancel

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