无法使用 C# 将 AdaptiveCard Json 呈现为 BotFramework 消息
Posted
技术标签:
【中文标题】无法使用 C# 将 AdaptiveCard Json 呈现为 BotFramework 消息【英文标题】:Cant render AdaptiveCard Json as a BotFramework Message with C# 【发布时间】:2018-03-20 12:14:59 【问题描述】:我正在尝试在发送到 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
]
当我将它发布到频道模拟器时,这个 json 不起作用。
【问题讨论】:
请贴出卡片JSON 我已经添加了自适应卡片json。 模拟器目前不支持“版本”:自适应卡的“1.0”。请尝试删除该行。 我删除了那行,但模拟器仍然无法正确显示卡。 我对试图在模拟器中显示的卡片进行了一些更改,所以它更简单了。 【参考方案1】:使用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"
);
【讨论】:
谢谢!这不适用于我使用的 Adaptive Cards 0.5.1,我已经更新到版本 1.0 BETA 3。(nuget.org/packages/AdaptiveCards/1.0.0-BETA3) 现在我可以将 json 作为 Adaptive Card 发布到 Botframework Emulator。以上是关于无法使用 C# 将 AdaptiveCard Json 呈现为 BotFramework 消息的主要内容,如果未能解决你的问题,请参考以下文章