botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

FormFlow string options as buttons

Open MichalDurista opened this issue 8 years ago • 5 comments

Bot Info

  • Deployment Environment: local development with Emulator

Issue Description

I am using bot framework and I am trying to define a form dynamically using FormFlow. I have a problem with one specific field (see the code). The problem is that ProductPlatform depends on ProductName, therefore it is a string. This works fine, however, this way, the bot does not show the options of possible platforms (although there is {||} in SetPrompt).

When I set type to null SetType(null), the bot now shows possible platforms as buttons, however, it never goes to ValidateProductPlatform when user decides to type a wrong platform instead of clicking on the correct one (I guess the validation itself is done already on SetDefine level). The only reason I need to validate user input through ValidateProductPlatform is that I want to cancel the form after 3 unsuccessful attempts.

So, is there any way to achieve this?: User has options (as buttons) for ProductPlaftorm based on ProductName, but instead of clicking, they (might) type a wrong platform, and after 3 wrong attempts, the form ends.

Code Example

.Field(new FieldReflector<IssueFormModel>(nameof(IssueFormModel.ProductPlatform))
.SetType(typeof(string))
.SetDefine(async (issue, field) =>
{
    if (issue.ProductName != null)
    {
        foreach (var platform in productsWithPlatforms[issue.ProductName])
        {
                        field.AddDescription(platform, platform).AddTerms(platform, platform);
        }
    }

   return await Task.FromResult(true);
   })
.SetPrompt(new PromptAttribute("Which platform of {ProductName}{||}?"))
.SetAllowsMultiple(false)
.SetValidate(async (state, value) => await ValidateProductPlatform(value, state)))

MichalDurista avatar Apr 21 '18 10:04 MichalDurista

Please share the rest of the IssueFormModel, including the ValidateProductPlatform

EricDahlvang avatar Apr 24 '18 01:04 EricDahlvang

Hello. I don't see how that might be helpful since as I tried to describe, the problem is that it either gets into ValidateProductPlatform but platform options were not shown (as buttons), or it doesn't get there at all (when SetType(null)). Anyway, here it is:

[Serializable]
public class IssueFormModel
{
    public string Description { get; set; }
    public string ProductName { get; set; }
    public string ProductPlatform { get; set; }
}

And here is Validation:

private async Task<ValidateResult> ValidateProductPlatform(object value, IssueFormModel state)
{
    var match = await _luisProvider.GetFromUtterance(value as string);
    if (match.topScoringIntent.intent == Resource.Intent_AnswerEnd && match.topScoringIntent.score > .75)
    {
        throw new FormCanceledException("You have canceled the form", null);
    }

    var validPlatform = FindValidProductPlatform(match.entities.FirstOrDefault(e => e.type == Resource.Entity_ProductPlatform)?.entity);
    var isValidPlatform = validPlatform != null;
    var validateResult = new ValidateResult
    {
        Value = validPlatform,
        IsValid = isValidPlatform
    };
    if (!isValidPlatform)
    {
        validateResult.Feedback = $"\"{value as string}\" is not a valid platform";
        if (++platformAttempts == 3)
        {
            throw new TooManyAttemptsException("Too many attempts in platform");
        }
    }

    return await Task.FromResult(validateResult);
}

MichalDurista avatar Apr 24 '18 07:04 MichalDurista

I'm sorry, but I'm trying to reproduce the issue here and I don't have everything needed. Would you please supply a small project that demonstrates the problem? I've tried to reconstruct the issue, but there's just too many pieces missing.

EricDahlvang avatar Apr 25 '18 23:04 EricDahlvang

Thanks for the reply. Here is a small project, please ignore everything that is not handled and focus only on validation of ProductPlatform :) https://github.com/MichalDurista/FormFlowBot

MichalDurista avatar Apr 26 '18 15:04 MichalDurista

This is now answered/tracked over on stackoverflow: https://stackoverflow.com/questions/49817654/formflow-define-options-for-string-field/50477910#50477910

sbuffkin avatar May 22 '18 23:05 sbuffkin