QnA Maker Bot AdaptiveCards:如何在C#中添加数据对象



我使用"无代码"的方式在Azure中生成Bot,并将其连接到QnA Maker知识库。

然后我修改了代码,使Bot使用AdaptiveCards而不是HeroCards来支持MS Teams频道中的Markdown格式(QnA Maker使用的格式(。

当知识库中出现一些提示时,我正试图将SubmitActions添加到这些自适应卡中。目标是,如果用户点击这些SubmitActions,它会自动向Bot.发送一条消息

请在下面找到我实现的代码:

// adaptive card creation
var plCardBis = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0));
plCardBis.Body.Add(new AdaptiveTextBlock()
{
Text = result.Answer,
Wrap = true
});
// Add all prompt
foreach (var prompt in result.Context.Prompts)
{
plCardBis.Actions.Add(new AdaptiveCards.AdaptiveSubmitAction()
{
Title = prompt.DisplayText,
Data = prompt.DisplayText
});
}
//create the the attachment
var attachmentBis = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = plCardBis
};
//add the attachment
chatActivity.Attachments.Add(attachmentBis);
return chatActivity;

这在WebChat中运行良好,但在Teams中,如果我点击提示,就会产生错误。在互联网上,我发现我应该为团队的数据字段使用一个对象,而不是一个简单的字符串:

"data": {
"msteams": {
"type": "imBack",
"value": "Text to reply in chat"
},
}

你知道我怎么能在C#中做到这一点吗?如何更新代码以将此对象添加到"数据"字段?根据用户提出的问题,操作的数量可能会有所不同。。。

如有任何帮助,将不胜感激

基本上,有两个选项可以附加到"Data"上——一个纯字符串值,或者任何自定义对象。对于您的场景,您需要一个自定义对象,因此您需要在项目中定义一个类来匹配您需要的内容,例如:

public class MsTeamsDataResponseWrapper
{
[JsonProperty("msteams")]
public MsTeamsResponse MsTeamsResponse { get; set; }
}
public class MsTeamsResponse
{
[JsonProperty("type")]
public string Type { get; set; } = "imBack";
[JsonProperty("value")]
public string Value { get; set; }
}

那么你可以这样使用它:

...
Data = new MsTeamsDataResponseWrapper() { MsTeamsResponse = new MsTeamsResponse() { Value = prompt.DisplayText } }
...

在这种情况下,"Type"已经默认为"imBack",但如果您想覆盖默认值,您也可以在稍后阶段将其用于"messageBack"。

最新更新