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

时间:2018-09-10 19:17:46

标签: c# .net botframework

在使用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);

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

1 个答案:

答案 0 :(得分: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"
});