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