Search code examples
c#.netbotframework

How to save and retrieve user responses from FormFlow with Adaptive Cards?


I'm using an Adaptive Card with a multiselect, in the context of Bot created with BotBuilder (Bot Framework):

var card = new AdaptiveCard();
card.Body.Add(new AdaptiveTextBlock()
{
    Text = "Q1:xxxxxxxx?",
    Size = AdaptiveTextSize.Default,
    Weight = AdaptiveTextWeight.Bolder
});

card.Body.Add(new AdaptiveChoiceSetInput()
{
    Id = "choiceset1",
    Choices = new List<AdaptiveChoice>()
    {
        new AdaptiveChoice(){
            Title="answer1",
            Value="answer1"
        },
        new AdaptiveChoice(){
            Title="answer2",
            Value="answer2"
        },
        new AdaptiveChoice(){
            Title="answer3",
            Value="answer3"
        }
    },
    Style = AdaptiveChoiceInputStyle.Expanded,
    IsMultiSelect = true
});

var message = context.MakeMessage();

message.Attachments.Add(new Attachment() { Content = card, ContentType =  "application/vnd.microsoft.card.adaptive"});    
await context.PostAsync(message);

Now, I would like to know which elements the user has selected.


Solution

  • I would like to know which elements the user has selected.

    You can get user's selections from message Value property, the following code snippets work for me, please refer to it.

    if (message.Value != null)
    {
        var user_selections = Newtonsoft.Json.JsonConvert.DeserializeObject<userselections>(message.Value.ToString());
    
        await context.PostAsync($"You selected {user_selections.choiceset1}!");
        context.Wait(MessageReceivedAsync);
    }
    

    The definition of userselections class:

    public class userselections
    {
        public string choiceset1 { get; set; }
    }
    

    Test result:

    enter image description here

    Update: Code snippet of adding AdaptiveChoiceSetInput and AdaptiveSubmitAction

    card.Body.Add(new AdaptiveChoiceSetInput()
    {
        Id = "choiceset1",
        Choices = new List<AdaptiveChoice>()
        {
            new AdaptiveChoice(){
                Title="answer1",
                Value="answer1"
            },
            new AdaptiveChoice(){
                Title="answer2",
                Value="answer2"
            },
            new AdaptiveChoice(){
                Title="answer3",
                Value="answer3"
            }
        },
        Style = AdaptiveChoiceInputStyle.Expanded,
        IsMultiSelect = true
    });
    
    
    card.Actions.Add(new AdaptiveSubmitAction()
    {
        Title = "submit"
    });