代码之家  ›  专栏  ›  技术社区  ›  SuperJMN

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

  •  1
  • SuperJMN  · 技术社区  · 6 年前

    我用的是 自适应卡 在创建bot的上下文中,使用multiselect 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);
    

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

    1 回复  |  直到 6 年前
        1
  •  2
  •   Fei Han    6 年前
    < Buff行情>

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

    < /块引用>

    您可以从message value property中获得用户的选择,以下代码段对我有效,请参阅它。

    if(message.value!=空)
    {
    var user_selections=newtonsoft.json.jsonconvert.deserializeObject<user selections>(message.value.toString());
    
    wait context.postsync($“您选择了用户选择。选项集1!!”);
    context.wait(messagereceivedAsync);
    }
    < /代码> 
    
    

    userselections的定义class:。

    公共类用户选择
    {
    公共字符串选项et1 get;set;
    }
    < /代码> 
    
    

    测试结果:

    update:code snippet of adding adaptivechoicesetinput and adaptivesubmitaction

    card.body.add(new adaptivechoicesetinput())
    {
    id=“choiceset1”,
    choices=新列表<adaptivechoice>()
    {
    新建adaptivechoice()。{
    title=“答案1”,
    值=“回答1”
    }
    新建adaptivechoice()。{
    title=“答案2”,
    值=“应答器2”
    }
    新建adaptivechoice()。{
    title=“答案3”,
    值=“应答器3”
    }
    }
    style=adaptivechoiceInputStyle.expanded,
    IsMultiSelect=真
    (});
    
    
    card.actions.add(新的adaptiveSubmitAction())
    {
    title=“提交”
    (});
    < /代码> 
    属性,以下代码段对我有效,请参阅它。

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