在Facebook Messenger中分享按钮

时间:2017-02-18 17:48:39

标签: c# botframework facebook-messenger-bot

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

2 个答案:

答案 0 :(得分:2)

由于“共享”按钮特定于Facebook并且并非所有频道都通用,因此BotBuilder中没有代码可以执行此操作。

但是,可以使用ChannelDataC#)/ sourceEventNode.js)来实现。

请参阅this post,了解频道数据信息的外观。另外,this sample显示了如何使用ChannelData功能。

最后,这是围绕ChannelData的文档。

答案 1 :(得分:2)

捎带Ezequiel提供的信息,

我创建了一个有效的C#bot,利用ChannelData属性通过Facebook Messenger发送共享按钮。

随意check out the repo here.

Models目录包含将作为Facebook Messenger共享按钮的正确JSON格式的所有类定义as is documented here.

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

来自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);
        }
    }
}

这将产生所需的输出:

enter image description here

请注意,如果您打算使用该项目,则需要在Web.config文件中填写您自己的Bot App ID和Secret。