如何将自定义json转换为自适应卡json格式 [英] How to convert custom json to adaptive card json format

查看:188
本文介绍了如何将自定义json转换为自适应卡json格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为聊天机器人寻找 https://adaptivecards.io/.我看到我们可以创建任何类型的聊天机器人.卡,我们可以使用自适应json呈现ui.但是如何将自定义json转换为自适应卡json格式.我是否应该根据我的自定义json响应类型进行手动转换,或者有什么方法可以将特定的json转换为首选的自适应卡.

I am looking https://adaptivecards.io/ for my chatbot.I saw we can create any type of card and we can render ui using adaptive json.But how can i convert my custom json to adaptive card json format. Should i need to manually convert based on my custom json response type or is there any way that convert specific json to preferred adaptive card.

adaptive card response:

{
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "type": "AdaptiveCard",
    "version": "1.0",
    "body": [
        {
            "type": "TextBlock",
            "text": "What type of food do you prefer?",
            "wrap": true
        },
        {
            "type": "ImageSet",
            "imageSize": "medium",
            "images": [
                {
                    "type": "Image",
                    "url": "https://contososcubademo.azurewebsites.net/assets/steak.jpg",
                    "size": "Medium"
                },
                {
                    "type": "Image",
                    "url": "https://contososcubademo.azurewebsites.net/assets/chicken.jpg",
                    "size": "Medium"
                },
                {
                    "type": "Image",
                    "url": "https://contososcubademo.azurewebsites.net/assets/tofu.jpg",
                    "size": "Medium"
                }
            ]
        }
    ]
}

json:
{
    "title": "Publish Adaptive Card Schema",
    "description": "Now that we have defined the main rules and features of the format, we need to produce a schema and publish it to GitHub. The schema will be the starting point of our reference documentation.",
    "creator": {
        "name": "Matt Hidinger",
        "profileImage": "https://pbs.twimg.com/profile_images/3647943215/d7f12830b3c17a5a9e4afcc370e3a37e_400x400.jpeg"
    },
    "createdUtc": "2017-02-14T06:08:39Z",
    "viewUrl": "https://adaptivecards.io",
    "properties": [
        {
            "key": "Board",
            "value": "Adaptive Cards"
        },
        {
            "key": "List",
            "value": "Backlog"
        },
        {
            "key": "Assigned to",
            "value": "Matt Hidinger"
        },
        {
            "key": "Due date",
            "value": "Not set"
        }
    ]
}

推荐答案

某些情况下可能会有所帮助:原则上,卡片不仅可以发送原始文本回复,还可以让您发送更有条理,更格式化的消息,并且它们还允许您非常重要,它们还允许您以按钮的形式添加交互.还要注意的是,团队中可以使用不同种类的卡,包括但不限于自适应卡,它们本身就是整个类别.

Some background in case it helps: In principle, Cards allow you to send a more structured, formatted message than just a raw text response, and they also allow you to add images, hyperlinks, formatting, etc. Very important, they also let you add interaction, in the form of buttons. Also of note, there are different kinds of Cards available in Teams, including, but not limited to, Adaptive Cards which are kind of an entire category on their own.

一天结束时,您将发送卡作为JSON响应.但是,有多种方法可以存储,然后在发送前构造/填充卡.第一种方法是将其存储为JSON ,在这种情况下,例如,您可以将卡作为某种资源存储在项目中,并带有占位符(例如"title":"## Title ##),然后在您的应用程序代码中,然后发送卡.在这种情况下,您基本上是在进行字符串/正则表达式类型的替换.

At the end of the day, you'll be sending the card as a JSON response. However, there are different ways to store and then construct/populate the cards themselves before sending. The first way is to store them as JSON, in which case you might, for instance, store a card as a resource of some sort in your project, with placeholders (like "title": "##Title##") and then in your application code, before sending the card. You're basically doing string/regex type replacement in this case.

构造卡片的另一种常见方法是使用机器人的平台语言(C#或JS).这是 C#中的示例,这里的一个JS示例.无论哪种情况,都需要创建卡并在运行时使用要显示给用户的任何数据填充卡的值.对于C#,您正在使用此nuget包)中的强类型对象.该库中的此类还包括必需的序列化属性,以便为您的卡创建正确的最终JSON,但是,这当然是为您完成的.

Another common way to construct the cards is in your bot's platform language (C# or JS). Here's an example in C# and here's a JS example. In either case, you're creating the card and populating it's values at runtime with whatever data you want to show to the user. In the case of C#, you're using strongly typed objects from this nuget package). This classes in this library also includes the required serialization attributes so that the correct final JSON is created for your cards, but it's all done for you of course.

以上两种都是完全有效的方法.但是,还有一个新选项当前被标记为实验性",它使用的是您在问题中提出的方法-使用自适应卡模板",其中您有使用占位符标记卡的格式,并且然后使用模板引擎进行替换.实际上,这与我上面提供的第一个示例相同,只是其他人为您编写了令牌替换引擎".在这种情况下,您具有标记(卡定义),数据(数据的JSON)和将它们组合在一起以构造最终JSON响应的引擎.您可以在此处了解更多信息,但再次提醒一下,这只是现阶段的实验.这是有关JS和C#的更多信息.

Both of the above are perfectly valid approaches. However, there's another new option that's currently marked as "experimental", which is to use the approach you're asking about in your question - using "Adaptive Card Templating", where you have a format to marking up your Card with placeholders, and then using a templating engine to do the replacements. It's actually kind of the same as the first example I supplied above, it's just that someone else has written the token replacement 'engine' for you. In this case, you have the markup (the card definition), the data (the JSON for your data) and the engine that brings them together to construct the final JSON response). You can read more about this here, but again a reminder, it's just experimental at this stage. Here's more info for both JS and C#.

我已经使用了这两种方法,但是我发现,因为我使用的是C#,所以在代码中完成所有操作都更加容易-我已经将模板" +数据"放在一起,并且进行了强类型化处理以避免出现错误最终输出.

I've used both approaches but I find, because I'm using C#, it's easier to do it all in code - I get the "template" + "data" together already, and it's strongly typed to avoid errors in the final output.

顺便说一句,App Studio包括一个名片设计器,这很有用,因为名片在团队中的呈现有时会与其他客户端有所不同. App Studio还可以让您(通过App Studio机器人本身)将卡发送给自己,这样您就可以直接在Teams中看到真实"卡.

Incidentally, App Studio includes a card designer, which is useful because Cards sometimes render a little differently in Teams versus other clients. App Studio also let's you send the card to yourself (via the App Studio bot itself) so you can see the "real" card directly in Teams.

这篇关于如何将自定义json转换为自适应卡json格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆