【问题标题】:How to dynamically bind data to adaptive card template in Java如何在 Java 中将数据动态绑定到自适应卡片模板
【发布时间】:2022-07-25 15:45:21
【问题描述】:

我正在使用 java (spring boot) 开发一个 Microsoft 团队机器人,它将消息发送给团队用户,我可以使用卡片发送消息。现在我正在尝试动态生成自适应卡。我已经创建了自适应卡片模板并且拥有数据,在自适应卡片设计器中一切正常。

问题是没有用于自适应卡片模板的Java SDK,只有C#和JavaScript SDK https://docs.microsoft.com/en-us/adaptive-cards/templating/sdk

卡片在设计器中看起来像这样

使用 JavaScript,我们可以简单地将数据传递给模板以生成卡片,但我如何在 java spring boot 应用程序中做到这一点。

这是我的模板:

{
"$schema": "https://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.3",
"body": [
    {
        "id": "messageBlock",
        "type": "TextBlock",
        "text": "${msg.message}",
        "wrap": true
    },
    {
        "id": "messageSeparator",
        "type": "TextBlock",
        "text": " ",
        "separator": true,
        "spacing": "Medium"
    },
    {
        "id": "mediaContainer",
        "type": "Container",
        "$data": "${media}",
        "items": [
            {
                "type": "ColumnSet",
                "columns": [
                    {
                        "type": "Column",
                        "width": "auto",
                        "items": [
                            {
                                "type": "Image",
                                "$when": "${not(empty(icon))}",
                                "url": "${icon}",
                                "size": "Small"
                            }
                        ]
                    },
                    {
                        "type": "Column",
                        "width": "stretch",
                        "items": [
                            {
                                "type": "TextBlock",
                                "$when": "${not(empty(fileName))}",
                                "text": "${fileName}",
                                "size": "Medium",
                                "wrap": true,
                                "weight": "Bolder",
                                "color": "Accent",
                                "height": "stretch"
                            }
                        ],
                        "selectAction": {
                            "type": "Action.OpenUrl",
                            "url": "${url}",
                            "title": "View"
                        }
                    }
                ]
            },
            {
                "type": "TextBlock",
                "text": " ",
                "wrap": true,
                "separator": true,
                "spacing": "Medium"
            }
        ]
    }
],
"actions": [
    {
        "$when": "${direction == 'Inbound'}",
        "type": "Action.Submit",
        "title": "Reply",
        "data": {
            "type": "task/fetch",
            "submitLocation": "task/fetch"
        }
    }
]

}

和样本数据:

{
"direction": "Inbound",
"message": "test message",
"media": [
    {
    "url": "https://example.com/imageUrl1",
    "icon" : "https://example.com/icon1",
    "fileName": "file1.png",
    "fileType": "png"
},
{
    "url": "https://example.com/imageUrl2",
    "icon" : "https://example.com/icon2",
    "fileName": "image1.png",
    "fileType": "png"
}
]

}

【问题讨论】:

标签: java botframework microsoft-teams adaptive-cards


【解决方案1】:

自适应卡片模板 SDK 目前在 .NET 和 NodeJS 中可用:

参考文档链接:https://docs.microsoft.com/en-us/adaptive-cards/templating/#sdk-support

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2020-12-21
    • 2020-12-23
    • 2021-12-05
    • 2020-07-29
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多