自适应卡在小组c#

时间:2019-09-09 06:52:33

标签: c# botframework microsoft-teams adaptive-cards

我正在尝试将自适应卡发送给Microsoft Teams,并且它在Teams中返回未定义的信息。在模拟器中工作正常。我正在使用c#。下面是来自模拟器的图像和用于向团队发送响应的代码。

Emulator:

代码:

public async Task EventCardMessage(Activity activity, JToken row)
{
    using (var connector = new ConnectorClient(new Uri(activity.ServiceUrl), _config["MicrosoftAppId"], _config["MicrosoftAppPassword"]))
    {
        //adaptive card
        Activity replyToConversation = activity.CreateReply();
        replyToConversation.Attachments = new List<Attachment>();

        AdaptiveCards.AdaptiveCard card = new AdaptiveCards.AdaptiveCard();
        Card adaptiveCard = new Card();
        adaptiveCard.Count = row["total_count"].ToString();
        // Add text to the card.
        card.Body.Add(new TextBlock()
        {
            Text = "There are " + adaptiveCard.Count + " Holidays this Year",
            Size = TextSize.Normal,
            Weight = TextWeight.Bolder
        });

        // Add Columns to card.
        var list = row["card"]["data"]["list"];
        foreach (var li in list)
        {
            adaptiveCard.Name = li["label3"].ToString();
            adaptiveCard.Date = li["label1"].ToString() + " " + li["label2"].ToString();
            adaptiveCard.Time = li["label5"].ToString();
            adaptiveCard.Address = li["label4"].ToString();

            var current = new ColumnSet();
            card.Body.Add(current);
            var column1 = new Column();
            current.Columns.Add(column1);
            var column2 = new Column();
            current.Columns.Add(column2);
            column1.Size = "40";
            AddTextBlock(column1, adaptiveCard.Date, TextSize.Large, TextColor.Accent, false);
            column2.Size = "60";
            AddTextBlock(column2, adaptiveCard.Name, TextSize.Normal, TextColor.Default, false);
            AddTextBlock(column2, adaptiveCard.Address, TextSize.Normal, TextColor.Default, false);
            AddTextBlock(column2, adaptiveCard.Time, TextSize.Normal, TextColor.Default, false);
        }

        // Create the attachment.
        Attachment attachment = new Attachment()
        {
            ContentType = AdaptiveCards.AdaptiveCard.ContentType,
            Content = card
        };
        replyToConversation.Attachments.Add(attachment);

        var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);
    }
}

1 个答案:

答案 0 :(得分:1)

Microsoft Teams currently only supports Adaptive Cards 1.0.这意味着,如果您尝试发送版本高于1.0的卡,则不会渲染。使用AdaptiveCards NuGet软件包时,默认情况下,将为您的卡提供与该软件包相同的版本。这意味着,如果您使用AdaptiveCards 1.2.0,则将使用1.2.0版构造您的卡。尝试明确设置卡的版本:

var card = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0));

如果您想了解有关自适应卡的更多信息,可以查看我的最新博客文章:https://blog.botframework.com/2019/07/02/using-adaptive-cards-with-the-microsoft-bot-framework/