简体   繁体   English

使用 c# 将数据数组发送到 AdaptiveCard

[英]Send data array to an AdaptiveCard with c#

i'm trying to develop a dynamic adaptive card to mention users in Teams, but i having a problem to render data inside the template.我正在尝试开发一个动态自适应卡片来提及 Teams 中的用户,但我在呈现模板内的数据时遇到问题。

{
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.0",
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "TextBlock",
      "$data": [
        { "name": "Matt" },
        { "name": "David" },
        { "name": "Thomas" }
      ],
      "text": "hi, <at>${name}</at>"
    }
  ],
  "msteams": {

    "entities": [
      {
        "type": "mention",
        "$data": [
          { "name": "Matt" },
          { "name": "David" },
          { "name": "Thomas" }
        ],
        "text": "<at>${name}</at>",
        "mentioned": {
          "id": "${name}",
          "name": "${name}"
        }
      }
    ]
  }
}

if i use this template everything works fine, but i need to dinamically render the data, i serialized a json with the same $data structure with the name of the user mentioned but it doesn't render anything.如果我使用此模板,一切正常,但我需要动态呈现数据,我使用相同的 $data 结构序列化了一个 json,其中提到了用户的名称,但它不呈现任何内容。

i tryed this way but doesn't work我试过这种方式但不起作用

{
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.0",
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "TextBlock",
      "$data": ${user}
      "text": "hi, <at>${name}</at>"
    }
  ],
  "msteams": {

    "entities": [
      {
        "type": "mention",
        "$data": ${user},
        "text": "<at>${name}</at>",
        "mentioned": {
          "id": "${name}",
          "name": "${name}"
        }
      }
    ]
  }
}

here's the C# code that serialize the data value这是序列化数据值的 C# 代码

            List<MentionUser> userMentionList = new List<MentionUser>();
            AdaptiveCardTemplate template = new AdaptiveCardTemplate(File.ReadAllText("Resources/Mention.json"));
            var json = new List<MentionedList>();

            foreach (var subscription in SubscriptionMentions)
            {
                var element = new MentionUser
                {
                    name = subscription,
                };

                userMentionList.Add(element);
            }
            
            json.Add(new MentionedList { instructions = userMentionList});
            string jsonString = System.Text.Json.JsonSerializer.Serialize(json);
            var data = new
            {
                user = jsonString,
            };
            string cardJson = template.Expand("");
            var card = SerializeCard(cardJson);

inside the jsonString i serialized this json string在 jsonString 中,我序列化了这个 json 字符串

[{"name":"username"},{"name":"username"}]}]

Can someonene please help to get rid of this problem?有人可以帮忙解决这个问题吗? Thank you a lot and have a nice day非常感谢,祝你有美好的一天

We have tested using $data for array to send data in adaptive card dynamically.我们已经测试过使用 $data 数组来动态发送自适应卡中的数据。 It is supported for adaptive card textblock,factset etc. Currently there is no support for sending dynamic array to entity property in msteams.它支持自适应卡片文本块、事实集等。目前不支持将动态数组发送到 msteams 中的实体属性。 For mention user we have to repeat entity block not text block.对于提及用户,我们必须重复实体块而不是文本块。

Ref Doc: https://learn.microsoft.com/en-us/adaptive-cards/templating/language参考文档: https://learn.microsoft.com/en-us/adaptive-cards/templating/language

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM