简体   繁体   中英

Share Button In Facebook Messenger

是否可以在botframework中创建CardAction(按钮),作为Facebook Messenger中的共享按钮?

Since the Share button is specific to Facebook and not common to all the channels, there isn't code in the BotBuilder for doing that.

However it can be achieved, by using ChannelData ( C# )/ sourceEvent ( Node.js ).

See this post as reference on how the channel data info should looks like. Also, this sample shows how to use the ChannelData feature.

Finally, here is the documentation around ChannelData .

Piggybacking off of the information provided by Ezequiel,

I have created a working C# bot that utilizes the ChannelData property to send a Share Button over Facebook Messenger.

Feel free to check out the repo here.

The Models directory contains all the class definitions that will act as the proper JSON format for a Facebook Messenger Share Button as is documented here.

Then you just create a new object using all of your combined Model classes and assign it to the ChannelData property of a new reply in your dialog like so:

From ShareButtonDialog.cs :

namespace Azure_Bot_Generic_CSharp
{
    using System;
    using System.Diagnostics;
    using System.Threading.Tasks;
    using Microsoft.Bot.Connector;
    using Microsoft.Bot.Builder.Dialogs;
    using Models;

    [Serializable]
    public class ShareButtonDialog : IDialog<object>
    {
        public async Task StartAsync(IDialogContext context)
        {
            context.Wait(this.MessageReceivedAsync);
        }
        public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
        {
            var message = await argument;

            //create a reply message
            var reply = context.MakeMessage();
            //create a channel data object to act as a facebook share button
            reply.ChannelData = new FacebookChannelData()
            {
                Attachment = new FacebookAttachment()
                {
                    Payload = new FacebookGenericTemplate()
                    {
                        Elements = new object[]
                        {
                            new FacebookGenericTemplateContent()
                            {
                                Buttons = new[]
                                {
                                    new FacebookShareButton()
                                }
                            }
                        }
                    }
                }
            };

            //send message
            await context.PostAsync(reply);

            var reply2 = context.MakeMessage();
            reply2.Text = "This is a message after the Share Button template.";
            await context.PostAsync(reply2);
            //wait for more messages to be sent here
            context.Wait(MessageReceivedAsync);
        }
    }
}

That will produce the desired output:

在此输入图像描述

Please note that you will need to fill in your own Bot App ID and Secret in the Web.config file if you plan to use the project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM