【问题标题】:Botframework v4: Can't render cardsBotframework v4:无法渲染卡片
【发布时间】:2019-01-06 08:48:37
【问题描述】:

这是 Botframework v4 文档上的示例。但它不起作用。 它在 Microsoft 机器人模拟器上显示“无法渲染卡”。 我正在尝试做的是 carouselCard,但 Microsoft 示例中的这张简单卡片已经无法使用。

    {
  "type": "message",
  "text": "Plain text is ok, but sometimes I long for more...",
  "attachments": [
    {
      "contentType": "application/vnd.microsoft.card.adaptive",
      "content": {
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "type": "AdaptiveCard",
        "version": "1.0",
        "body": [
          {
            "type": "TextBlock",
            "text": "Hello World!",
            "size": "large"
          },
          {
            "type": "TextBlock",
            "text": "*Sincerely yours,*"
          },
          {
            "type": "TextBlock",
            "text": "Adaptive Cards",
            "separation": "none"
          }
        ],
        "actions": [
          {
            "type": "Action.OpenUrl",
            "url": "http://adaptivecards.io",
            "title": "Learn More"
          }
        ]
      }
    }
  ]
}

但是,如果我删除代码的顶部,则此代码有效:

  {
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "type": "AdaptiveCard",
  "version": "1.0",
  "body": [
    {
      "type": "TextBlock",
      "text": "Hello World!",
      "size": "large"
    },
    {
      "type": "TextBlock",
      "text": "*Sincerely yours,*"
    },
    {
      "type": "TextBlock",
      "text": "Adaptive Cards",
      "separation": "none"
    }
  ],
  "actions": [
    {
      "type": "Action.OpenUrl",
      "url": "http://adaptivecards.io",
      "title": "Learn More"
    }
  ]
}

这就是我所说的卡片。有没有更好的方法来做到这一点?

 public class GetNameAndAgeDialog : WaterfallDialog
    {

        private readonly string _cards = @".\Resources\TryCarouselCard.json";

        private static Attachment CreateAdaptiveCardAttachment(string filePath)
        {
            var adaptiveCardJson = File.ReadAllText(filePath);
            var adaptiveCardAttachment = new Attachment()
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content = JsonConvert.DeserializeObject(adaptiveCardJson),
            };
            return adaptiveCardAttachment;
        }

        public GetNameAndAgeDialog(string dialogId, IEnumerable<WaterfallStep> steps = null) : base(dialogId, steps)
        {

            AddStep(async (stepContext, cancellationToken) =>
            {
                var cardAttachment = CreateAdaptiveCardAttachment(_cards);
                var reply = stepContext.Context.Activity.CreateReply();
                reply.Attachments = new List<Attachment>() { cardAttachment };
                await stepContext.Context.SendActivityAsync(reply, cancellationToken);
                return await stepContext.ContinueDialogAsync();
            });
         }
      }

【问题讨论】:

    标签: c# botframework carousel jsonschema adaptive-cards


    【解决方案1】:

    您发布的第一个 JSON 块的“顶部”是活动中包含的卡片。发布的第二个 JSON 块确实只是卡片本身以及您想要放入 Attachment 的内容。

    至于您的代码,我觉得它是正确的。我可能会考虑缓存附件 JSON,因为您可能不想每次显示卡片时都访问文件系统,但这只是一种优化。

    我不清楚您是否遇到任何进一步的问题,或者现在只是在寻找对该方法的验证。如果您仍然遇到问题,请更新问题并提供更多详细信息,我会更新我的答案以尝试提供帮助。

    【讨论】:

    • 我遇到问题先生,因为第一个代码不会在机器人模拟器上呈现。当我在模拟器上运行它时,它说“可以渲染卡”。但是当我删除顶部部分时,这是机器人模拟器渲染它的第二个代码,我可以看到卡片。我的问题是我想制作一个轮播卡,所以我认为我需要代码的顶部。
    • 我设法使用 C# 而不是 json 文件制作了轮播卡。顺便说一句,这是我的回购先生,如果您有时间,您可以检查一下是否有改进。谢谢github.com/bnj123/azureBot
    • 我很高兴你能成功。看起来你已经开始使用英雄卡而不是自适应卡了。您经常会发现某些渠道/客户根本还没有完全支持自适应卡。模拟器和网络客户端往往支持大部分功能,但您之前所做的某些事情可能还不支持。
    • 这很有意义,感谢 Drew 先生为我提供的一切帮助!顺便问一下,“cancellationToken:cancellationToken”和“cancellationToken”有什么区别?它的重要性是什么?
    • cancellationToken: cancellationToken 中,冒号前面的名称是您正在调用的方法中的参数名称。当特定方法有很多可选参数并且您不一定要按顺序传递它们时,需要消除歧义。不幸的是,Bot Builder SDK 中的一些 API 在使用可选参数而不是提供多个重载时变得有点太舒服了。现在,开发人员必须更加详细地消除参数歧义。其中一些 API 可以随着时间的推移而变得平滑,但其他 API 则无法在不进行重大更改的情况下修复。 ?
    猜你喜欢
    • 2019-02-11
    • 2018-02-20
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    相关资源
    最近更新 更多