MS Chat Bot-如何从我的C#代码访问自定义自适应卡属性 [英] MS Chat Bot --How to access custom adaptive card properties from my C# code

查看:108
本文介绍了MS Chat Bot-如何从我的C#代码访问自定义自适应卡属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C#代码中动态更改自定义自适应卡的文本属性值?

How to dynamically change my custom adaptive card's text property value from within my C# code?

这是我的C#代码

public static Attachment CreateMySearchCardAttachment()
        {
            // combine path for cross platform support
            string[] paths = { ".", "Resources", "MySearchCard.json" };
            var MySearchCardJson = File.ReadAllText(Path.Combine(paths));

            var adaptiveCardAttachment = new Attachment()
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content = JsonConvert.DeserializeObject(MySearchCardJson),

            };

            return adaptiveCardAttachment;
        }

我的 MySearchCard.json 文件位于下面

{
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "ColumnSet",
      "columns": [
        {
          "type": "Column",
          "items": [
            {
              "type": "Image",
              "horizontalAlignment": "Right",
              "spacing": "None",
              "url": "",
              "size": "Medium",
              "width": "2px",
              "height": "2px"
            },
            {
              "type": "TextBlock",
              "size": "Medium",
              "weight": "Bolder",
              "text": "Knowledgebase Search"
            },
            {
              "type": "Input.Text",
              "id": "searchText",
              "placeholder": "Type your search text and click Search"
            }
          ],
          "width": 2
        }
      ]
    }
  ],
  "actions": [
    {
      "type": "Action.Submit",
      "title": "Search"
    }
  ],
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.0"
}

我可以将这种自适应卡显示在聊天机器人中,但不确定如何动态更改文本标签或其值.我们想要在显示之前动态更改某些文本标签,然后在显示之后根据用户响应动态显示或隐藏.稍后,我们希望将聊天BoT与MS Teams集成在一起.但是在此之前,我需要显示相同的内容从我的模拟器中

I am able to get this adaptive card to display inside my chat bot.But not sure how to dynamically change the text labels or their values. We want to dynamically change some of the text labels before displaying, then after displaying,dynamically show or hide based on user response.At a later point we want to integrate chat BoT with MS Teams.But prior to that I need to show the same from my emulator

如果您查看我的json,有一个文本属性为"text":"Knowledgebase Search".我的问题是如何从我的C#代码中动态更改此文本值?

If you look at my json, there is a text property with "text": "Knowledgebase Search". My question is how to change this text value dynamically from within my C# code?

推荐答案

在显示之前发生动态变化

有几种不同的方法可以做到这一点.第一个选项可能是最好的,但是它们都应该起作用.

Dynamically Changing Before Being Displayed

There's a few different ways to do this. The first option is probably the best, but they should all work.

注意:此软件包与 Microsoft.AdaptiveCards

Note: This package is different from, and newer than, Microsoft.AdaptiveCards -- Don't use this one

由于您知道要更改的卡片的确切部分,因此可以:

Since you know the exact part of the card that you'd like to change, you can:

string[] paths = { ".", "AdaptiveCard.json" };
var cardJson = File.ReadAllText(Path.Combine(paths));
var card = AdaptiveCard.FromJson(cardJson).Card;
var columnSet = (card.Body[0] as AdaptiveColumnSet);
var column = (columnSet.Columns[0] as AdaptiveColumn);
var knowledgeBlock = (column.Items[1] as AdaptiveTextBlock);
knowledgeBlock.Text = "Whatever You Want";

var attachment = new Attachment()
{
    Content = card,
    ContentType = "application/vnd.microsoft.card.adaptive"
};

var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>();

reply.Attachments.Add(attachment);

await stepContext.Context.SendActivityAsync(reply);

结果:

这是预览版,您仍然需要使用#1中的NuGet程序包,但可以更轻松地修改特定字段.

This is in preview and you still need to use the NuGet package from #1, but makes it easier to modify particular fields.

这可能会更简单一些,但不太灵活.像这样的东西工作,并产生与#1相同的结果:

This is probably a little simpler, but less flexible. Something like this works and produces the same result as #1:

string[] paths = { ".", "AdaptiveCard.json" };
var cardJsonObject = JObject.Parse(File.ReadAllText(Path.Combine(paths)));
var knowledgeToken = cardJsonObject.SelectToken("body[0].columns[0].items[1]");
knowledgeToken["text"] = "Whatever You Want";

var attachment = new Attachment()
{
    Content = cardJsonObject,
    ContentType = "application/vnd.microsoft.card.adaptive"
};

var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>();

reply.Attachments.Add(attachment);


await stepContext.Context.SendActivityAsync(reply);
return await stepContext.NextAsync();

显示后发生动态变化

在显示卡后更改卡要困难一些.如上所述,您首先必须在代码中更改卡.然后,您必须使用 UpdateActivityAsync() .基本上,您发送具有相同id的活动,但是发送了一张新卡,它会完全覆盖前一张卡.

Dynamically Changing After Being Displayed

Changing the card after being displayed is a little more difficult. You first have to change the card in the code, as done above. You then have to use UpdateActivityAsync(). Basically, you send an activity with the same id, but a new card and it overwrites the previous card completely.

注意:您只能在支持更新活动的渠道中使用此功能.这通常很容易告诉您,因为即使没有漫游器,该频道也将允许或不允许您编辑消息.听起来好像您想使用团队,这样就可以正常工作.

Note: You can only use this in channels that support updating activities. It's usually pretty easy to tell, because even without bots, the channel either will or won't let you edit messages. It sounds like you want to use Teams, so this will work fine.

您可以在此处使用我的回答,了解如何更新团队的卡片活动.请注意,这是在Node中的,但是您仍然可以以相同的方式在C#中完成它.

You can use my answer here for how to update card activities with Teams. Note that this one is in Node, but you can still do it in C# the same way.

您还可以使用此 查看全文

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