Microsoft Bot Framework messages with buttons in Facebook Messenger
Asked Answered
L

2

5

I'm working on a bot using the C# Microsoft Bot Framework and I'd like to send messages with action buttons to Facebook Messenger. I've successfully created the bot, deployed it and can communicate with it through Messenger and am now trying to refine the appearance of the bot's responses. I have been able to create single cards and carousels by putting the card info into Message.Attachements but I'd like to also include action buttons. The Messenger Platform docs describe button and "generic" templates in their Send API Reference but for the life of me I can't figure out how to coerce the Bot Connector to send buttons to Messenger. It'd be great if I could just put the Send API json into the Message.ChannelData property but no luck. Has anyone managed to get Messenger to show buttons from the Bot Framework?

Lockman answered 3/5, 2016 at 18:43 Comment(0)
R
7

To add buttons to your message, you can add multiple actions to the attachment. Each action will be mapped to a button by connector. Multiple attachments will be mapped into a carousel in Facebook messenger. Below is an example of adding 3 buttons to the message.

            var reply = context.MakeMessage();
            reply.Attachments = new List<Attachment>();

            var actions = new List<Microsoft.Bot.Connector.Action>();
            for (int i = 0; i < 3; i++)
            {
                actions.Add(new Microsoft.Bot.Connector.Action
                {
                    Title = $"Button:{i}",
                    Message = $"Action:{i}"
                });
            }

            reply.Attachments.Add(new Attachment
            {
                Title = "Choose one:",
                Actions = actions
            });

            await context.PostAsync(reply);
Rufford answered 3/5, 2016 at 23:16 Comment(4)
Awesome! That worked perfectly. My problem was that I had an old version of the Microsoft.Bot.Connector Nuget library which didn't have the Actions. Needed to upgrade to version 1.1.0. Thanks!Lockman
What type is context? Could you please post the entire method? I am currently trying to downgrade from v3 to v1 in order to see if buttons are working (in v3 they are not showing/working properly neither on skype nore on facebook messenger).Rozanne
I think Context would be any implementation of IDialogContextAnonymous
Please update your sample. There is not any Action in BotFramework anymore. It must be CardAction. Also, their fields must be updated.Commit
S
1

Updating solution for version 3.9.0 :

        var actions = new List<CardAction>();
        for (int i = 0; i < 3; i++)
        {
            actions.Add(new CardAction
            {
                Title = $"Button:{i}",
                Text = $"Action:{i}"
            });
        }

        reply.Attachments.Add(
            new HeroCard
            {
                Title = "Choose option",
                Buttons = actions
            }.ToAttachment()
        );

        await context.PostAsync(reply);
Subduct answered 26/8, 2017 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.