How to break formflow in every moment i type exit

2019-05-26 06:35发布

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条回答
迷人小祖宗
2楼-- · 2019-05-26 07:09

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>
    });
查看更多
登录 后发表回答