botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Question] How to intercept HeroCard button events?

Open RaoVenka opened this issue 9 years ago • 3 comments

could please help me how to call a method from hero card button click in c# using Microsoft bot Framework.

RaoVenka avatar Jun 16 '17 09:06 RaoVenka

You can use a button.action post back to send a particular string. After you send this string, listen out for it to call the method. I am not sure if anyone has a more elegant solution

Regards Michael Hutchful

Sent from Mail for Windows 10

From: RaoVenka Sent: 16 June 2017 09:40 To: Microsoft/BotBuilder Cc: Subscribed Subject: [Microsoft/BotBuilder] HeroCard Button (#2963)

could please help me how to call a method from hero card button click in c# using Microsoft bot Framework. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

michaelhutchful avatar Jun 16 '17 09:06 michaelhutchful

Could pls provide some sample code in C# regarding above?

RaoVenka avatar Jun 16 '17 14:06 RaoVenka

@RaoVenka For more information, see: Process events within rich cards

To process events within rich cards, define CardAction objects to specify what should happen when the user clicks a button or taps a section of the card.

This code example shows how to create a reply message that contains three Hero cards rendered in carousel format:

Activity replyToConversation = message.CreateReply("Should go to conversation, in carousel format");
replyToConversation.AttachmentLayout = AttachmentLayouts.Carousel;
replyToConversation.Attachments = new List<Attachment>();

Dictionary<string, string> cardContentList = new Dictionary<string, string>();
cardContentList.Add("PigLatin", "https://<ImageUrl1>");
cardContentList.Add("Pork Shoulder", "https://<ImageUrl2>");
cardContentList.Add("Bacon", "https://<ImageUrl3>");

foreach(KeyValuePair<string, string> cardContent in cardContentList)
{
    List<CardImage> cardImages = new List<CardImage>();
    cardImages.Add(new CardImage(url:cardContent.Value ));

    List<CardAction> cardButtons = new List<CardAction>();

    CardAction plButton = new CardAction()
    {
        Value = $"https://en.wikipedia.org/wiki/{cardContent.Key}",
        Type = "openUrl",
        Title = "WikiPedia Page"
    };

    cardButtons.Add(plButton);

    HeroCard plCard = new HeroCard()
    {
        Title = $"I'm a hero card about {cardContent.Key}",
        Subtitle = $"{cardContent.Key} Wikipedia Page",
        Images = cardImages,
        Buttons = cardButtons
    };

    Attachment plAttachment = plCard.ToAttachment();
    replyToConversation.Attachments.Add(plAttachment);
}

var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);

nwhitmont avatar Jun 16 '17 15:06 nwhitmont