1

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

1 回答 1

1

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

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

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

于 2019-01-06T23:14:00.550 回答