我目前正在尝试在“瀑布对话框”中为我的一个机器人创建一个自适应卡,该机器人在渲染时将显示名称和搜索项(两个字符串)。我要使用的两个值都存储在对话框的Context.Activity.Value属性中,因此我所需要知道的是如何在创建自适应卡的某个时刻将这些值插入到我的自适应卡中,以便“文本”文本块的值可以包含我的值。
我研究了在自适应卡模式中使用空的JSON对象,该对象可以在自适应卡的创建过程中以某种方式填充,但是还没有弄清楚如何插入所述值。我是C#和Bot Framework的相对入门者,所以我不知道尝试什么。
以下是我的瀑布对话框中制作自适应卡的步骤:
private async Task<DialogTurnResult> AdaptiveCardTest(WaterfallStepContext stepContext,
CancellationToken cancellationToken)
{
var introCard = File.ReadAllText("./Content/AdaptiveCardTest.json");
var card = AdaptiveCard.FromJson(introCard).Card;
var attachment = new Attachment(AdaptiveCard.ContentType, content: card);
var response = MessageFactory.Attachment(attachment, ssml: card.Speak,
inputHint: InputHints.AcceptingInput);
await stepContext.Context.SendActivityAsync(response);
return await stepContext.NextAsync();
}
AdaptiveCardTest.json是自适应卡的json文件。目前,它只是一个带有一些文本的图像弹出窗口,其中包含占位符,我希望用户名和搜索项可以放在其中。占位符链接在那里,因为实际链接太长了。
{
"type": "AdaptiveCard",
"id": "NewUserGreeting",
"backgroundImage": "image_url_placeholder"
"body": [
{
"type": "Container",
"items": [
{
"type": "Image",
"url": "image_url_placeholder_2"",
"size": "Stretch"
}
]
},
{
"type": "Container",
"spacing": "None",
"backgroundImage": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAXCAIAAACAiijJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAqSURBVDhPY1RgL2SgDDBBaQrAqBEIMGoEAowagQCjRiDAqBEIQLERDAwAIisAxhgAwtEAAAAASUVORK5CYII=",
"items": [
{
"type": "TextBlock",
"id": "title",
"spacing": "Medium",
"size": "Large",
"weight": "Bolder",
"color": "Light",
"text": "Hi, I'm **your** Virtual Assistant",
"wrap": true
},
{
"type": "TextBlock",
"id": "body",
"size": "Medium",
"color": "Light",
"text": "The user {{Name}} would like to know more about {{SearchItem}}.",
"wrap": true
}
]
}
],
}
任何帮助将不胜感激,谢谢!
最佳答案
对于您的简单情况,我会接受@MikeP的建议。将来,如果您想做一个模板无法满足的更复杂的事情,那么一旦拥有installed AdaptiveCard NuGet包,就可以使用.NET SDK动态构建自适应卡。
.NET SDK的文档为pretty limited,但是AdaptiveCard对象的属性通常与JSON对应。
一个例子是:
const string ISO8601Format = "yyyy-MM-dd";
string text = "dynamic-text-here;
DateTime today = DateTime.Today;
string todayAsIso = today.ToString(ISO8601Format);
// Create card
AdaptiveCard adaptiveCard = new AdaptiveCard("1.0")
{
Body =
{
new AdaptiveContainer
{
Items =
{
new AdaptiveTextBlock
{
Text = question,
Wrap = true
},
new AdaptiveDateInput
{
// This Id matches the property in DialogValueDto so it will automatically be set
Id = "UserInput",
Value = todayAsIso,
Min = today.AddDays(-7).ToString(ISO8601Format),
Max = todayAsIso,
Placeholder = todayAsIso
}
}
}
},
Actions = new List<AdaptiveAction>
{
new AdaptiveSubmitAction
{
// Data can be an object but this will require the value provided for the
// Content property to be serialised it to a string
// as per this answer https://stackoverflow.com/a/56297792/5209435
// See the attachment block below for how this is handled
Data = "your-submit-data",
Title = "Confirm",
Type = "Action.Submit"
}
}
};
// Create message attachment
Attachment attachment = new Attachment
{
ContentType = AdaptiveCard.ContentType,
// Trick to get Adapative Cards to work with prompts as per https://github.com/Microsoft/botbuilder-dotnet/issues/614#issuecomment-443549810
Content = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(adaptiveCard))
};
cardActivity.Attachments.Add(attachment);
// Send the message
context.SendActivityAsync(cardActivity);
由于
Items
和Actions
是集合,因此您可以在代码中包含条件逻辑,以便在运行时根据某些条件构建这些集合,然后将构建集合传递给Items
或Actions
,这将使您比有一个JSON模板,您可以在已知位置替换占位符标记。