簡體   English   中英

在Facebook Messenger中分享按鈕

[英]Share Button In Facebook Messenger

是否可以在botframework中創建CardAction(按鈕),作為Facebook Messenger中的共享按鈕?

由於“共享”按鈕特定於Facebook並且並非所有通道都通用,因此BotBuilder中沒有用於執行此操作的代碼。

但是,可以通過使用ChannelDataC# )/ sourceEventNode.js )來實現。

有關頻道數據信息的外觀,請參閱此文章作為參考。 此外, 此示例還演示了如何使用ChannelData功能。

最后,這是有關ChannelData的文檔。

捎帶Ezequiel提供的信息,

我創建了一個工作的C#bot,它利用ChannelData屬性通過Facebook Messenger發送共享按鈕。

請隨時查看這里的回購。

Models目錄包含所有類定義,它們將充當Facebook Messenger Share Button的正確JSON格式,如此處所述

然后,您只需使用所有組合的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);
        }
    }
}

這將產生所需的輸出:

在此輸入圖像描述

請注意,如果您打算使用該項目,則需要在Web.config文件中填寫您自己的Bot App ID和Secret。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM