Search code examples
c#carouselbotframeworkfacebook-messenger-bot

Carousel Card is not working well in BotFramework for Facebook Messenger


I have implemented a bot which sends a hero card to the user as a response. As I have expected the following code send a carousel to the messenger.

ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

#region HeroCard
Activity replyToConversation = activity.CreateReply("Should go to conversation, with a hero card");
replyToConversation.Recipient = activity.From;
replyToConversation.Type = "message";
replyToConversation.Attachments = new List<Attachment>();
List<CardImage> cardImages = new List<CardImage>();
cardImages.Add(new CardImage(url: "https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png"));
cardImages.Add(new CardImage(url: "https://upload.wikimedia.org/wikipedia/en/archive/a/a9/20151112035044!Banyan_Tree_(_Shiv_Bajrang_Dham_Kishunpur).jpeg"));
List<CardAction> cardButtons = new List<CardAction>();
CardAction plButton = new CardAction()
{
    Value = "https://en.wikipedia.org/wiki/Pig_Latin",
    Type = "openUrl",
    Title = "WikiPedia Page"
};
CardAction plButton2 = new CardAction()
{
    Value = "https://en.wikipedia.org/wiki/Pig_Latin",
    Type = "openUrl",
    Title = "WikiPedia Page"
};
cardButtons.Add(plButton);
cardButtons.Add(plButton2);
HeroCard plCard = new HeroCard()
{
    Title = "I'm a hero card",
    Subtitle = "Pig Latin Wikipedia Page",
    Images = cardImages,
    Buttons = cardButtons
};
Attachment plAttachment = plCard.ToAttachment();
replyToConversation.Attachments.Add(plAttachment);
var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);
#endregion

However, I got the following message which was not a carousel. enter image description here

The question is how can I send a carousel using the native variables of botframework (not using manual generated json) to Facebook Messenger?


Solution

  • The problem was solved by these changes:

    • First, create two cards and attachments
    • Second, set AttachmentLayout to "carousel".

    You can find the modified code in the following:

    ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
    
    #region HeroCard
    
    Activity replyToConversation = activity.CreateReply("Should go to conversation, with a hero card");
    replyToConversation.Recipient = activity.From;
    replyToConversation.Type = "message";
    replyToConversation.Attachments = new List<Attachment>();
    // First Change
    // Card #One
    List<CardImage> cardImages1 = new List<CardImage>();
    cardImages1.Add(new CardImage(url: "https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png"));
    
    List<CardAction> cardButtons1 = new List<CardAction>();
    CardAction plButton1 = new CardAction()
    {
        Value = "https://en.wikipedia.org/wiki/Pig_Latin",
        Type = "openUrl",
        Title = "WikiPedia Page"
    };
    cardButtons1.Add(plButton1);
    HeroCard plCard1 = new HeroCard()
    {
        Title = "I'm a hero card",
        Subtitle = "Pig Latin Wikipedia Page",
        Images = cardImages1,
        Buttons = cardButtons1
    };
    Attachment plAttachment1 = plCard1.ToAttachment();
    replyToConversation.Attachments.Add(plAttachment1);
    
    // Card #Two
    List<CardImage> cardImages2 = new List<CardImage>();
    cardImages2.Add(new CardImage(url: "https://upload.wikimedia.org/wikipedia/en/archive/a/a9/20151112035044!Banyan_Tree_(_Shiv_Bajrang_Dham_Kishunpur).jpeg"));
    
    List<CardAction> cardButtons2 = new List<CardAction>();
    CardAction plButton2 = new CardAction()
    {
        Value = "https://en.wikipedia.org/wiki/Pig_Latin",
        Type = "openUrl",
        Title = "WikiPedia Page"
    };
    cardButtons2.Add(plButton2);
    HeroCard plCard2 = new HeroCard()
    {
        Title = "I'm a hero card",
        Subtitle = "Pig Latin Wikipedia Page",
        Images = cardImages2,
        Buttons = cardButtons2
    };
    
    Attachment plAttachment2 = plCard2.ToAttachment();
    replyToConversation.Attachments.Add(plAttachment2);
    
    // Second Change
    replyToConversation.AttachmentLayout = "carousel";
    
    var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);
    #endregion
    

    you can find the image of the carousel in Facebook Messenger in the below: enter image description here