The goal
I want to create (and then edit) Adaptive Cards designs with the Adaptive Cards Designer and use them in my bot.
The steps
1) I created my Adaptive card in the Designer, with sample values, and copied its JSON representation. For example:
{
"type": "AdaptiveCard",
"body": [
{
"type": "FactSet",
"id": "myFactSet",
"facts": [
{
"title": "Name:",
"value": "John Doe"
}
]
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
2) Now I want to use this card's design in my C# Bot Framework bot. I put this JSON into my project, where AdaptiveCards v1.1.0 nuget package is included.
3) I parse the card JSON into AdaptiveCards library classes with this line of code:
var card = AdaptiveCard.FromJson(card).Card;
4) Before sending the card to the user I have to fill it with real values, replacing the sample data.
The questions
How do I replace
"John Doe"
with a real user name? What is the simplest approach? (I already wrote an extension method to get anAdaptiveElement
by its id, so I can easily change its values. But tell me please if there is a simpler approach)And how do I clone an existing
AdaptiveElement
and put other values into it? For example, if I want to add anotherFactSet
to the card by copying existingmyFactSet
with all the same style?AdaptiveElement
has noClone()
method.
Why other solutions don't fit
Write it in C#
I want to be able to easily edit the card's design with the Designer, but if I rewrite the whole card in C# code, I'll have to do make a change to it every time the card's design changes, and I won't see the result in the Designer.
Do it in javascript
I understand it's easily done with javascript, but I use C# in my bot.
Parse JSON and work with it
I also understand that I can parse the card into JsonObject
and deal directly with JsonObject
s, clone them, change their properties, but
they are not typed, so there's no IntelliSense support and I can make a typo
I still have to walk all the elements to find the one with the given id (by the way, is there an extension method to easily do this?)
Using
JsonObject
will not eliminate the need of AdaptiveCards library and the need to callAdaptiveCard.FromJson(card)
method (because settingAttachment.Content
to aJsonObject
doesn't work for some reason).