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

天涯浪子 提交于 2019-12-24 15:01:35

问题


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.


回答1:


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:

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"
});


来源:https://stackoverflow.com/questions/52264144/how-to-save-and-retrieve-user-responses-from-formflow-with-adaptive-cards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!