如何使用自适应卡从FormFlow保存和检索用户响应?

问题描述 投票:1回答:1

在使用BotBuilderBot 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# .net botframework
1个回答
2
投票

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

您可以从消息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; }
}

测试结果:

enter image description here

更新:添加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"
});
© www.soinside.com 2019 - 2024. All rights reserved.