Microsoft Bot Framework messages with buttons in Facebook Messenger

主宰稳场 提交于 2019-11-27 15:36:31

问题


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?


回答1:


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);



回答2:


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);


来源:https://stackoverflow.com/questions/37011994/microsoft-bot-framework-messages-with-buttons-in-facebook-messenger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!