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

QnA Maker Bot AdaptiveCards: how to add Data object in C#

我使用 "no code" 方式在 Azure 中生成了一个机器人并将其连接到 QnA Maker 知识库。

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

当知识库出现一些提示时,我正在尝试将 SubmitActions 添加到这些自适应卡片。 objective 是,如果用户单击这些 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" - 纯字符串值或任何自定义对象。对于你的场景,你需要一个自定义对象,所以你需要在你的项目中定义一个 class 来匹配你需要的东西,比如:

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"。