QNA Maker Bot AdaptiveCards:如何在C#中添加数据对象 [英] QnA Maker Bot AdaptiveCards: how to add Data object in C#

查看:21
本文介绍了QNA Maker Bot AdaptiveCards:如何在C#中添加数据对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当知识库出现一些提示时,我正在尝试将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;

这在网络聊天中工作得很好,但是在团队中,如果我单击提示,它会生成一个错误。在Internet上查看时,我发现我应该为团队的数据字段使用一个对象,而不是一个简单的字符串:

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

这篇关于QNA Maker Bot AdaptiveCards:如何在C#中添加数据对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆