4

我正在尝试在发送到 Bot Framework Channel Emulator 的消息中使用 Adaptive Card 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();
        }
    }
}
}

我正在使用:

自适应卡 0.5.1.0

频道模拟器 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"
     }
   ]
 }
 ]
}

我将这张卡片作为 C# AdaptiveCard 对象制作。

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

当我将该对象发布到 Channel Emulator 时,它确实显示正确。

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

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

当我将它发布到 Channel Emulator 时,这个 json 不起作用。

4

1 回答 1

5

使用该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"
});
于 2017-10-24T02:13:22.827 回答