Search code examples
c#botframeworkadaptive-cardsspfx

Adaptive Cards don't render on Spfx when the card has more than 5 actions


Issue: Doesn't render more than five actions

Background:

I was doing an integration of the BotFramework-WebChat with SharePoint Framework and all is working fine. But, when I try to display an adaptive card with more than 5 actions inside the WebChat the chat show's me a card with the text "Can't render card" but if tested in BotFramework-Emulator works or Console Directline works fine.

For testing proposes I wrote a C# method in which you pass the number of actions, that you want to show in the chat.

Test with Adaptative Card

Issue: Render a card with the message "Can't render card" if there are more than five actions

Excepected : Render an adaptative card with more than five actions private async Task MessageReceivedAsync(IDialogContext context, IAwaitable argument) { var message = await argument; var reply = 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
    };

}

Screenshots:

Test with HeroCard:

I just try using HeroCard instead of AdaptiveCard. And in this case when are more than 5 buttons only shows the first 5 buttons. Maybe this is a limitation?

Issue: The card only render the first five actions

Excepected : Render a hero card with more than five actions

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://learn.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();
}

Solution

  • This is a limitation of adaptive cards in webchat. Luckily there is a workaround.

    WIthout making any changes you will see a limit of 5 Actions you can see this when you go to the visualizer and add over 5 actions such as

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

    you will get this error:

    enter image description here

    However, you can change this. It can be achieved by editing the host config file in the visualizer Specifically the maxactions field. webchat includes a host config file as well so in theory you could just edit that file and it should work.

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