Microsoft Bot Framework messages with buttons in Facebook Messenger

别来无恙 提交于 2019-11-29 00:37:56

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

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