Cant使用C#将AdaptiveCard Json呈现为BotFramework消息
作者:互联网
我正在尝试在发送给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不起作用.
解决方法:
使用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"
});
标签:botframework,adaptive-cards,json,c 来源: https://codeday.me/bug/20191025/1930152.html