首页 > 解决方案 > 如何使用自适应卡片从 FormFlow 中保存和检索用户响应?

问题描述

在使用BotBuilder ( Bot Framework )创建的 Bot 上下文中,我正在使用带有多选功能的自适应卡

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

现在,我想知道用户选择了哪些元素。

标签: c#.netbotframework

解决方案


我想知道用户选择了哪些元素。

您可以从消息Value属性中获取用户的选择,以下代码片段适用于我,请参考。

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

类的定义userselections

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

测试结果:

在此处输入图像描述

更新: 添加 AdaptiveChoiceSetInput 和 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"
});

推荐阅读