Share Button In Facebook Messenger

旧巷老猫 提交于 2019-12-10 18:15:33

问题


Is it possible to create a CardAction (button) in botframework which works as a share button in Facebook Messenger?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/42318597/share-button-in-facebook-messenger

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