Junaid Shabbir
Junaid Shabbir

Reputation: 23

How to dynamically bind data to adaptive card template in Java

I am developing a Microsoft teams bot using java (spring boot) which send the messages to the teams users, I can sent the messages using cards. Now I am trying to dynamically generate the adaptive card. I have created the adaptive card template and I have the data, In adaptive card designer everything work fine.

The problem is there is no Java SDK for adaptive card templating, there is only C# and JavaScript SDKs https://learn.microsoft.com/en-us/adaptive-cards/templating/sdk

Card look like this in designer enter image description here

Using JavaScript we can simple pass the data to template to generate the card but how can I do it in java spring boot application.

Here is my template:

{
"$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"
        }
    }
]

}

and the sample data:

{
"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"
}
]

}

Upvotes: 0

Views: 1441

Answers (2)

John
John

Reputation: 197

Not ideal, but I am using Thymeleaf to do the similar thing. Of course, it won't support conditional expansion such as "$when". It only applies variables and i18n messages strings.

Upvotes: 0

ChetanSharma-msft
ChetanSharma-msft

Reputation: 863

Adaptive Cards Templating SDK's are currently available in .NET and NodeJS: enter image description here

Reference doc link: https://learn.microsoft.com/en-us/adaptive-cards/templating/#sdk-support

Upvotes: 1

Related Questions