Search code examples
c#botframeworkdropdownchatbotadaptive-cards

can adaptive cards in bot framework v3 contain dynamic dropdowns


I'm using BOT Framework v 3 i have an adaptive card that takes input from the user and i want the values in Dropdown to be dynamic is it possible.here is the adaptive card design code as you can see I have entered the choices manually instead it want it to be dynamic from the database

var card = new AdaptiveCard()
{
    Body = new List<CardElement>()
    {
        new TextBlock()
        {
            Color = TextColor.Attention,
            Weight = TextWeight.Bolder,
            Size = TextSize.Medium,
            Text = "Select a title",
        },
        new ChoiceSet()
        {
            Id = "title",
            Style = ChoiceInputStyle.Compact,
            IsRequired = false,
            IsMultiSelect = false,
            Value = "1",
            Choices = new List<Choice>()
            {
                new Choice()
                {
                    Title = "Swiss cargo",
                    Value = "Swiss cargo",
                },
                new Choice()
                {
                    Title = "ticket booking",
                    Value = "ticket booking",
                },
            },
        },
    },
};

Solution

  • Assuming you can get your data into a list of strings, your Adaptive Card can easily be constructed dynamically using Linq. If you want to keep using the same Adaptive Cards library, it would look like this:

    var data = new List<string> { "Swiss cargo", "ticket booking" };
    
    var card = new AdaptiveCard()
    {
        Body = new List<CardElement>()
        {
            new TextBlock()
            {
                Color = TextColor.Attention,
                Weight = TextWeight.Bolder,
                Size = TextSize.Medium,
                Text = "Select a title",
            },
            new ChoiceSet()
            {
                Id = "title",
                Style = ChoiceInputStyle.Compact,
                IsRequired = false,
                IsMultiSelect = false,
                Value = "1",
                Choices = data.Select(item => new Choice { Title = item, Value = item }).ToList(),
            },
        },
    };