代码之家  ›  专栏  ›  技术社区  ›  user8689373

无法使用C将AdaptiveCard Json呈现为BotFramework消息#

  •  4
  • user8689373  · 技术社区  · 7 年前

    我试图在发送给Bot Framework通道模拟器的消息中使用自适应卡json。 然而,模拟器给出消息“无法渲染卡”。

    http://adaptivecards.io/visualizer/

    使用以下代码发送自适应卡即时消息: 使用以下示例: https://github.com/Microsoft/AdaptiveCards/issues/411

    using System;
    using System.Threading.Tasks;
    using Microsoft.Bot.Builder.Dialogs;
    using Microsoft.Bot.Connector;
    using AdaptiveCards;
    using System.IO;
    using System.Diagnostics;
    using System.Web.Hosting;
    
    namespace Chatbot_Proberen.Dialogs
    {
        [Serializable]
        public class RootDialog : IDialog<object>
        {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);
    
            return Task.CompletedTask;
        }
    
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var activity = await result as Activity;
    
            var returnMessage = context.MakeMessage();
    
            returnMessage.Attachments.Add(new Attachment()
            {
                Content = await GetCardText("ParkMogelijkheden"),
                ContentType = AdaptiveCard.ContentType,
                Name = "Card"
            });
    
            Debug.WriteLine(returnMessage.Attachments[0].Content);
            await context.PostAsync(returnMessage);
        }
    
        public async Task<string> GetCardText(string cardName)
        {
            var path = HostingEnvironment.MapPath($"/{cardName}.json");
            if (!File.Exists(path))
                return string.Empty;
    
            using (var f = File.OpenText(path))
            {
                return await f.ReadToEndAsync();
            }
        }
    }
    }
    

    即时消息使用:

    通道模拟器v3.5.31

    编辑:

    Json自适应卡:

    {
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "type": "AdaptiveCard",
    "version": "1.0",
    "body": [
        {
            "type": "Container",
            "items": [
                {
                    "type": "TextBlock",
                    "text": "Publish Adaptive Card schema",
                    "weight": "bolder",
                    "size": "medium"
                },
                {
                    "type": "ColumnSet",
                    "columns": [
                        {
                            "type": "Column",
                            "width": "auto",
                            "items": [
                                {
                                    "type": "Image",
                                    "url": "https://pbs.twimg.com/profile_images/3647943215/d7f12830b3c17a5a9e4afcc370e3a37e_400x400.jpeg",
                                    "size": "small",
                                    "style": "person"
                                }
                            ]
                        },
                        {
                            "type": "Column",
                            "width": "stretch",
                            "items": [
                                {
                                    "type": "TextBlock",
                                    "text": "Matt Hidinger",
                                    "weight": "bolder",
                                    "wrap": true
                                },
                                {
                                    "type": "TextBlock",
                                    "spacing": "none",
                                    "text": "Created {{DATE(2017-02-14T06:08:39Z,Short)}}",
                                    "isSubtle": true,
                                    "wrap": true
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "type": "Container",
            "items": [
                {
                    "type": "TextBlock",
                    "text": "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.",
                    "wrap": true
                },
                {
                    "type": "FactSet",
                    "facts": [
                        {
                            "title": "Board:",
                            "value": "Adaptive Card"
                        },
                        {
                            "title": "List:",
                            "value": "Backlog"
                        },
                        {
                            "title": "Assigned to:",
                            "value": "Matt Hidinger"
                        },
                        {
                            "title": "Due date:",
                            "value": "Not set"
                        }
                    ]
                }
            ]
        }
    ],
    "actions": [
        {
            "type": "Action.ShowCard",
            "title": "Set due date",
            "card": {
                "type": "AdaptiveCard",
                "body": [
                    {
                        "type": "Input.Date",
                        "id": "dueDate",
                        "title": "Select due date"
                    }
                ],
                "actions": [
                    {
                        "type": "Action.Submit",
                        "title": "OK"
                    }
                ]
            }
        },
        {
            "type": "Action.ShowCard",
            "title": "Comment",
            "card": {
                "type": "AdaptiveCard",
                "body": [
                    {
                        "type": "Input.Text",
                        "id": "comment",
                        "isMultiline": true,
                        "placeholder": "Enter your comment"
                    }
                ],
                "actions": [
                    {
                        "type": "Action.Submit",
                        "title": "OK"
                    }
                ]
            }
        },
        {
            "type": "Action.OpenUrl",
            "title": "View",
            "url": "http://adaptivecards.io"
        }
    ]
    }
    

    我尝试更改json,以获得更简单的卡:

    {
     "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
     "type": "AdaptiveCard",
     "body": [
     {
       "type": "Container",
       "items": [
         {
           "type": "TextBlock",
           "text": "Publish Adaptive Card schema",
           "weight": "bolder",
           "size": "medium"
         }
       ]
     }
     ]
    }
    

            AdaptiveCard ac = new AdaptiveCard()
            {
                Body =
                {
                    new Container()
                    {
                        Items =
                        {
                            new TextBlock()
                            {
                                Text = "Publish Adaptive Card schema",
                                Weight = TextWeight.Bolder,
                                Size = TextSize.Medium
                            }
                        }
                    }
                }
            };
    

    当我将该对象发布到通道模拟器时,它确实显示正确。

    当我序列化C#对象时,它会生成以下json:

    {
    "type": "AdaptiveCard",
    "body": [{
        "type": "Container",
        "items": [{
            "type": "TextBlock",
            "size": "medium",
            "weight": "bolder",
            "text": "Publish Adaptive Card schema"
        }],
        "style": null
    }]
    }
    

    当我将此json发布到通道模拟器时,它不起作用。

    1 回复  |  直到 7 年前
        1
  •  6
  •   Ratul Sharker    7 年前

    使用 AdaptiveCard.FromJson() 方法设置附件的 Content 所有物

    var json = await GetCardText("ParkMogelijkheden");
    var results = AdaptiveCard.FromJson(json);
    var card = results.Card;
    returnMessage.Attachments.Add(new Attachment()
    {
        Content = card,
        ContentType = AdaptiveCard.ContentType,
        Name = "Card"
    });