Botframework v4:无法渲染卡片 [英] Botframework v4: Can't render cards

查看:74
本文介绍了Botframework v4:无法渲染卡片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is the sample on Botframework v4 docs. But it does not work. It says "Can't Render Card" on the Microsoft bot emulator. What i'm trying to do is a carouselCard but this simple card from Microsoft's sample is already not working.

    {
  "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"
          }
        ]
      }
    }
  ]
}

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

However, if i remove the top part of the code this code works:

  {
  "$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"
    }
  ]
}

这就是我叫卡的方式.有更好的方法吗?

This is how i call the card. Is there a better way to do this?

 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();
            });
         }
      }

推荐答案

您发布的JSON的第一块顶部"是活动中包含的卡片.确实,发布的JSON的第二个块实际上就是卡本身以及您想要放入Attachment的内容.

The "top part" of the first block of JSON you posted is the card contained within an activity. The second block of JSON posted is indeed just the card itself and what you'd want to put into an Attachment.

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

As for your code, it looks correct to me. I might consider caching the attachment JSON as you probably don't want to hit the file system every time you want to display the card, but that would just be an optimization.

我不清楚您是否遇到任何其他问题,或者现在是否正在寻找验证方法.如果您仍然遇到问题,请用更多详细信息更新问题,我将更新答案以寻求帮助.

I'm unclear if you are experiencing any further problems or just looking for validation of the approach now. If you are still experiencing a problem please update the question with some more details and I'll update my answer to try and help.

这篇关于Botframework v4:无法渲染卡片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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