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

Limit Number of Choices shown in Form

Open jigneshs123 opened this issue 10 years ago • 2 comments

I'm adding some dynamic fields to the form which doesn't show choices at the start, but if user provides invalid input then I show the choices as buttons by defining TemplateUsage.NotUnderstood. This works great but I've many choices to chose from so want to avoid showing long set of choices, I want to only show limited number of choices to the user, instead of showing all the choices. Is there a way to control that using Pattern?

Scenario: bot: Describe the style? user: foo bot: Sorry, I didn't understand that. Try one of the following:

Choice 1 Choice 2 ..so on till Choice 20

Desired Behavior: Show only say Choice 1 to 6.

Here is how BuildForm code looks like:

return new FormBuilder<StyleSuggestForm>()
                    .Field(new FieldReflector<StyleSuggestForm>("Style")
                        .SetType(null)
                        .SetPrompt(new PromptAttribute("Describe the style?"))
                        .ReplaceTemplate(new TemplateAttribute(TemplateUsage.NotUnderstood, "Sorry, I didn't understand that. Try one of the following: {||}"))
                        .SetActive((state) => state.StyleOptions != null)
                        .SetDefine(async (state, field) =>
                        {
                            foreach (ImageStyle option in state.StyleOptions)
                            {
                                field
                                    .AddDescription(option.Value, option.Description)
                                    .AddTerms(option.Value, option.Terms);
                        }
                        return true;
                    }))
                .AddRemainingFields()
                .Build();

jigneshs123 avatar Sep 28 '16 19:09 jigneshs123

Hi @chrimc62 any idea how we can achieve this?

jigneshs123 avatar Sep 30 '16 19:09 jigneshs123

The only way to control that is to replace the prompter, i.e.

builder.SetPrompter(async (context, prompt) =>
                {
                    var msg = context.MakeMessage();
                    if (prompt.Buttons?.Count > 0)
                    {
                        var style = prompt.Style;
                        if (style == ChoiceStyleOptions.Auto)
                        {
                            foreach (var button in prompt.Buttons)
                            {
                                // Images require carousel
                                if (button.Image != null)
                                {
                                    style = ChoiceStyleOptions.Carousel;
                                    break;
                                }
                            }
                        }
                        if (style == ChoiceStyleOptions.Carousel)
                        {
                            msg.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                            msg.Attachments = prompt.GenerateHeroCards();
                        }
                        else
                        {
                            msg.AttachmentLayout = AttachmentLayoutTypes.List;
                            msg.Attachments = prompt.GenerateHeroCard();
                        }
                    }
                    else if (prompt.Description?.Image != null)
                    {
                        msg.AttachmentLayout = AttachmentLayoutTypes.List;
                        var card = new HeroCard() { Title = prompt.Prompt, Images = new List<CardImage> { new CardImage(prompt.Description.Image) } };
                        msg.Attachments = new List<Attachment> { card.ToAttachment() };
                    }
                    else
                    {
                        msg.Text = prompt.Prompt;
                    }
                    await context.PostAsync(msg);
                    return prompt;
                };);

Where you add logic to truncate the number of buttons you see.

chrimc62 avatar Oct 05 '16 01:10 chrimc62