Microsoft Bot Framework messages with buttons in Facebook Messenger

前端 未结 2 1839
南方客
南方客 2020-12-15 13:45

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, depl

相关标签:
2条回答
  • 2020-12-15 14:05

    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);
    
    0 讨论(0)
  • 2020-12-15 14:11

    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);
    
    0 讨论(0)
提交回复
热议问题