【问题标题】:Adaptive Cards don't render on Spfx when the card has more than 5 actions当卡片具有超过 5 个操作时,自适应卡片不会在 Spfx 上呈现
【发布时间】:2018-10-05 05:44:14
【问题描述】:

问题:渲染的动作不超过五个

背景:

我正在将 BotFramework-WebChat 与 SharePoint 框架集成,一切正常。但是,当我尝试在 WebChat 中显示具有超过 5 个操作的自适应卡片时,聊天显示给我一张带有“无法呈现卡片”文本的卡片,但如果在 BotFramework-Emulator 中测试可以正常工作或控制台 Directline 可以正常工作。

为了测试建议,我编写了一个 C# 方法,您可以在其中传递要在聊天中显示的操作数。

使用自适应卡测试

问题:如果有五个以上的操作,则呈现带有“无法呈现卡片”消息的卡片

异常:渲染一张具有五个以上动作的自适应卡片 私有异步任务 MessageReceivedAsync(IDialogContext 上下文,IAwaitable 参数) { var message = 等待参数; var 回复 = context.MakeMessage();

    if (message.Text.Equals("3 btn"))
    {
        reply.Attachments.Add(GetAdaptativeCard(3));
        reply.Value = " { nombre : 'BTN'}";
    }
    else if (message.Text.Equals("5 btn"))
    {
        reply.Attachments.Add(GetAdaptativeCard(5));
        reply.Value = " { nombre : 'BTN'}";
    } 
    else if (message.Text.Equals("7 btn"))
    {
        reply.Attachments.Add(GetAdaptativeCard(7));
        reply.Value = " { nombre : 'BTN'}";
    }
    else if (message.Text.Equals("18 btn"))
    {
        reply.Attachments.Add(GetAdaptativeCard(18));
        reply.Value = " { nombre : 'BTN'}";
    }
    else
    if (message.Text.Equals("gif"))
    {
        reply.Attachments.Add(GetAnimationCard());
    await context.PostAsync(reply);
}

private Attachment GetAdaptativeCard(int numberOfButtons) {

    var actions = new List<ActionBase>();
    for (int i = 0; i < numberOfButtons; i++)
    {
        actions.Add(new SubmitAction()
        {
            Title = i.ToString(),
            Speak = "<s>Search</s>",
            DataJson = "{ \"Type\": \"HotelSearch\" }"
        });
    }

    AdaptiveCard card = new AdaptiveCard()
    {
        Body = new List<CardElement>()
        {
            new Container()
            {
                Speak = "<s>Hello!</s><s>How many buttons are in the chat?(18)</s>",
                Items = new List<CardElement>()
                {
                    new ColumnSet()
                    {
                        Columns = new List<Column>()
                        {
                            new Column()
                            {
                                Size = ColumnSize.Auto,
                                Items = new List<CardElement>()
                                {
                                    new Image()
                                    {
                                        Url = "https://placeholdit.imgix.net/~text?txtsize=65&txt=Adaptive+Cards&w=300&h=300",
                                        Size = ImageSize.Medium,
                                        Style = ImageStyle.Person
                                    }
                                }
                            },
                            new Column()
                            {
                                Size = ColumnSize.Stretch,
                                Items = new List<CardElement>()
                                {
                                    new TextBlock()
                                    {
                                        Text =  "Hello!",
                                        Weight = TextWeight.Bolder,
                                        IsSubtle = true
                                    }
                                }
                            }
                        }
                    }
                }
        }
    },
        // Buttons
        Actions = actions
    };
    return new Attachment()
    {
        ContentType = AdaptiveCard.ContentType,
        Content = card
    };

}

截图:

用 HeroCard 测试:

我只是尝试使用 HeroCard 而不是 AdaptiveCard。在这种情况下,当超过 5 个按钮时,只显示前 5 个按钮。 也许这是一个限制?

问题:卡片只渲染前五个动作

异常:渲染一张英雄卡有五个以上的动作

private Attachment GetHeroCard(int numbersOfButtons)
{
    var actions = new List<CardAction>();
    for (int i = 0; i < numbersOfButtons; i++)
    {
        actions.Add(new CardAction(ActionTypes.OpenUrl, $"Get Started {i}", value: "https://docs.microsoft.com/bot-framework"));
    }
    HeroCard card = new HeroCard()
    {
        Title = "BotFramework Hero Card",
        Subtitle = "Your bots — wherever your users are talking",
        Text = "Build and connect intelligent bots to interact with your users naturally wherever they are, from text/sms to Skype, Slack, Office 365 mail and other popular services.",
        Images = new List<CardImage> { new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg") },
        Buttons = actions
    };
    return card.ToAttachment();
}

【问题讨论】:

  • “我正在将 BotFramework-WebChat 与 SharePoint 框架集成,并且一切正常”:您有关于此实施的更多详细信息吗?你在使用github.com/SharePoint/sp-dev-fx-webparts/tree/master/samples/… 吗?
  • 嗨,我将 github.com/Microsoft/BotFramework-WebChat 集成到了 SPFX 的 react 项目中。当机器人返回给我一张卡片超过五个动作时,我遇到的问题,如果卡片是类型 herocard 卡片只显示前五个动作,如果类型是 AdaptiveCard 返回带有消息“无法呈现卡片”的卡片。我认为卡片中动作数量的问题与github.com/Microsoft/AdaptiveCards有关,但我不确定。
  • 总结一下:自适应卡片:最多 5 个动作可以,否则不渲染 // HeroCard:最多 5 个动作可以,否则只渲染前 5 个
  • 是的。你知道这是否是一种限制吗?

标签: c# botframework adaptive-cards spfx


【解决方案1】:

这是网络聊天中自适应卡片的限制。幸运的是有一个解决方法。

如果不进行任何更改,您将看到 5 个 Actions 的限制,您可以在转到 visualizer 并添加超过 5 个操作时看到这一点,例如

{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
},
{
    "type": "Action.OpenUrl",
    "title": "View",
    "url": "http://adaptivecards.io"
}

你会得到这个错误:

但是,您可以更改此设置。可以通过编辑visualizer 特别是maxactions 字段中的主机配置文件来实现。 webchat 也包含一个主机配置文件,因此理论上您可以编辑该文件,它应该可以工作。

"actions": {
    "maxActions": 20,
    "spacing": "default",
    "buttonSpacing": 8,
    "showCard": {
      "actionMode": "inline",
      "inlineTopMargin": 8
    },
    "actionsOrientation": "vertical",
    "actionAlignment": "stretch"
  },

【讨论】:

  • 谢谢你,杰森,我马上试试 :)
  • 查看网络聊天的来源,有一个文件具有自适应卡的配置adaptivecards-hostconfig.json link,但它看到没有任何影响。
  • 嗯,这让我的想法大跌眼镜。你是如何使用网络聊天的?通过我假设的自定义构建?不是通过 iframe?
  • 是的,我通过自定义构建使用网络聊天。
  • 谢谢@JasonSowers :)。查看代码我看到网络聊天正在使用 this.props.hostconfig 而不是 defaultsHostConfig 其 maxAction 100 mountAdaptiveCards() { // Render for non adaptative cards debugger; const adaptiveCard = this.props.nativeCard || new AdaptiveCard(); console.error("Check HERE the props of the default hosting config"); debugger; adaptiveCard.hostConfig = this.props.hostConfig || defaultHostConfig;
猜你喜欢
  • 2019-04-22
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 2020-08-16
  • 2021-04-12
  • 2017-12-29
相关资源
最近更新 更多