Amanda Lange
Amanda Lange

Reputation: 743

Using FormFlow Bots Framework Quiz Program

Our bot build does a ‘personality quiz’ for the user. Think Buzzfeed.

I have a variety of attributes I want to increase, just integers, based on the user’s selections on a form, then return an end result. Using Sandwichbot as a template, this is asking something like (paraphrased):

Do you like to help other people? Yes No

Code is like:

                       .Confirm(async (state) =>
                       {

                        switch (state.HelpYesNo)
                        {
                            case true: HelpfulValue++; break;
                            case false: HurtfulValue++; break;
                        }
                        return new PromptAttribute("Thanks, choose OK to continue.");

It works fine, but I hate that I have to make the user ‘Confirm’ by typing OK. It’s an extra step, especially if they have to do it after each question.

I tried writing this with a validate instead, eg validate: async (state, response) => Which gives a better user experience, but doesn’t actually run the switch-case. I think the formatting of the switch is in the wrong place for a validate? I'm not sure of the syntax here to get 'validate' to process the case.

What’s the right way to do something like this in FormFlow?

Upvotes: 1

Views: 279

Answers (1)

Xeno-D
Xeno-D

Reputation: 2213

Try something like this. Boolean fields also result in a Yes/No question.

[Serializable]
public class QuizForm
{
    public int HelpfulValue;
    public int HurtfulValue;

    [Prompt("Do you like to help people? {||}")]
    public bool HelpPeople;

    public static IForm<QuizForm> BuildForm()
    {
        return new FormBuilder<QuizForm>()
                .Message("Let the quiz begin...")
                .Field(nameof(HelpPeople), validate: ValidateBool)
                // other fields
                .Build();
    }

    private static async Task<ValidateResult> ValidateBool(QuizForm state, object value)
    {
        var TrueOrFalse = (bool) value;

        switch (TrueOrFalse)
        {
            case true: state.HelpfulValue++; break;
            case false: state.HurtfulValue++; break;
        }

        return new ValidateResult{IsValid = true, Value = value};
    }
}

Upvotes: 2

Related Questions