简体   繁体   English

在Facebook Messenger中分享按钮

[英]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. 由于“共享”按钮特定于Facebook并且并非所有通道都通用,因此BotBuilder中没有用于执行此操作的代码。

However it can be achieved, by using ChannelData ( C# )/ sourceEvent ( Node.js ). 但是,可以通过使用ChannelDataC# )/ sourceEventNode.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. 此外, 此示例还演示了如何使用ChannelData功能。

Finally, here is the documentation around ChannelData . 最后,这是有关ChannelData的文档。

Piggybacking off of the information provided by Ezequiel, 捎带Ezequiel提供的信息,

I have created a working C# bot that utilizes the ChannelData property to send a Share Button over Facebook Messenger. 我创建了一个工作的C#bot,它利用ChannelData属性通过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. Models目录包含所有类定义,它们将充当Facebook Messenger Share Button的正确JSON格式,如此处所述

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: 然后,您只需使用所有组合的Model类创建一个新对象,并将其分配给对话框中新回复的ChannelData属性,如下所示:

From ShareButtonDialog.cs : 来自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. 请注意,如果您打算使用该项目,则需要在Web.config文件中填写您自己的Bot App ID和Secret。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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