自适应卡提交操作

时间:2018-11-19 15:41:07

标签: c# adaptive-cards

我已经使用JSON格式生成了一个自适应卡,带有两个提交和取消按钮,分别返回“ messageBack”消息,分别是提交和取消。 我正在使用C#访问答复,但无法弄清楚如何从自适应卡访问答复。

我的json是

    {
      "type": "AdaptiveCard",
      "selectAction": {        
        "type": "Action.Submit"
      },
      "body": [
        {
          "type": "TextBlock",
          "horizontalAlignment": "Center",
          "size": "Large",
          "weight": "Bolder",
          "color": "Accent",
          "text": "Meeting Composer Create"
        },
        {
          "type": "ColumnSet",
          "columns": [
            {
              "type": "Column",
              "items": [
                {
                  "type": "TextBlock",
                  "horizontalAlignment": "Left",
                  "spacing": "Medium",
                  "size": "Medium",
                  "weight": "Bolder",
                  "color": "Accent",
                  "text": "Attendees:"
                 }
              ],
              "width": "stretch"
            },
            {
              "type": "Column",
              "items": [
                {
                  "type": "TextBlock",
                  "id": "attendeeVal",
                  "text": "a"
                }
              ],
              "width": "stretch"
            }
          ]
        },
        {
          "type": "ColumnSet",
      "columns": [
        {
          "type": "Column",
          "items": [
            {
              "type": "TextBlock",
              "size": "Medium",
              "weight": "Bolder",
              "color": "Accent",
              "text": "Subject:"
            }
          ],
          "width": "stretch"
        },
        {
          "type": "Column",
          "items": [
            {
              "type": "TextBlock",
              "id": "subVal",
              "text": "meeting"
            }
          ],
          "width": "stretch"
        }
      ]
    },
    {
      "type": "ColumnSet",
      "columns": [
        {
          "type": "Column",
          "items": [
            {
              "type": "TextBlock",
              "size": "Medium",
              "weight": "Bolder",
              "color": "Accent",
              "text": "Date:"
            }
          ],
          "width": "stretch"
        },
        {
          "type": "Column",
          "items": [
            {
              "type": "TextBlock",
              "id": "dateVal",
              "text": "17/11/2018 10.30 AM"
            }
          ],
          "width": "stretch"
        }
      ]
    },
    {
      "type": "ColumnSet",
      "columns": [
        {
          "type": "Column",
          "items": [
            {
              "type": "TextBlock",
              "size": "Medium",
              "weight": "Bolder",
              "color": "Accent",
              "text": "Document Name:"
            }
          ],
          "width": "stretch"
        },
        {
          "type": "Column",
          "items": [
            {
              "type": "TextBlock",
              "id": "docVal",
              "text": "Document1"
            }
          ],
          "width": "stretch"
        }
      ]
    }
  ],
  "actions": [
    {
      "type": "Action.Submit",
      "title": "Submit",
      "data": {
        "msteams": {
          "type": "messageBack",
          "displayText": "I clicked this button",
          "text": "text to bots",
          "value": "Submit"
        }
      }
    },
    {
      "type": "Action.Submit",
      "title": "Cancel",
      "data": {
        "msteams": {
          "type": "messageBack",
          "displayText": "I clicked this button",
          "text": "text to bots",
          "value": "Cancel"
        }
      }
    }
  ],
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.0"
}

我的C#是

 var response = getCard(stepContext, "Aditya Rao, Vishal Subramaniam" , "Scrum Meeting" , "17/11/1028, 10:30AM" , "Scrum Sprint.pptx");
 await stepContext.Context.SendActivityAsync(response).ConfigureAwait(false);

我要支持的其他功能

private static Attachment CreateAdaptiveCardAttachment(string filePath, string names, string subj , string datee, string docs)
    {
        var adaptiveCardJson = File.ReadAllText(filePath);
        dynamic obj = JsonConvert.DeserializeObject(adaptiveCardJson);
        obj["body"][1]["columns"][1]["items"][0]["text"] = names;
        obj["body"][2]["columns"][1]["items"][0]["text"] = subj;
        obj["body"][3]["columns"][1]["items"][0]["text"] = datee;
        obj["body"][4]["columns"][1]["items"][0]["text"] = docs;
        var adaptiveCardAttachment = new Attachment()
        {
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = obj,
        };
        return adaptiveCardAttachment;
    }


    // Create an attachment message response.
    private Activity CreateResponse(Activity activity, Attachment attachment)
    {
        var response = activity.CreateReply();
        response.Attachments = new List<Attachment>() { attachment };
        return response;
    }


    private Activity getCard(WaterfallStepContext stepContext, string names, string subj , string datee, string docs)
    {
        var jsonFilePath = @".\Dialogs\CardTemplates\MeetingComposerCreate.json";
        var activity = stepContext.Context.Activity;
        var adCard = CreateAdaptiveCardAttachment(jsonFilePath,names,subj,datee,docs);
        var response = CreateResponse(activity, adCard);
        return response;
    }

单击提交或取消后如何访问值响应?

如果有人可以帮助我找到如何从input.choice中获取答复,以获取所选卡的复选框数据。

1 个答案:

答案 0 :(得分:0)

  

单击提交或取消后如何访问值响应?

我们可以通过Activity.Value属性从用户从自适应卡提交的值。

if(turnContext.Activity.Value!= null)
{

    reply.Text = $"submit data: {turnContext.Activity.Value}";

}

测试结果:

enter image description here

模拟器中的输出:

enter image description here

  

如果有人可以帮助我找到如何从input.choice中获取答复,以获取所选卡的复选框数据。

要显示供用户选择的选择选项并获取选择的选项,可以参考以下代码片段。

在json文件中:

{
  "type": "Input.ChoiceSet",
  "id": "optionSelection",
  "isMultiSelect": true,
  "style": "compact",
  "choices": [
    {
      "title": "option1",
      "value": "option1"
    },
    {
      "title": "option2",
      "value": "option2"
    },
    {
      "title": "option3",
      "value": "option3"
    }
  ]
}

使用相同的代码来获取用户的选择:

reply.Text = $"submit data: {turnContext.Activity.Value}";

测试结果:

enter image description here