在 Bot 框架中将 Facebook 通用模板的 JSON 代码转换为 C#

Converting JSON code of Facebook's Generic Template to C# in Bot framework

我很难将这个通用的 facebook 模板转换为 C#。我不确定我是否正确转换了它。以下是我尝试但未在 Messenger 上呈现的代码。谢谢。

    curl -X POST -H "Content-Type: application/json" -d '{
  "recipient":{
    "id":"<PSID>"
  },
  "message":{
    "attachment":{
      "type":"template",
      "payload":{
        "template_type":"generic",
        "elements":[
           {
            "title":"Welcome!",
            "image_url":"https://petersfancybrownhats.com/company_image.png",
            "subtitle":"We have the right hat for everyone.",
            "default_action": {
              "type": "web_url",
              "url": "https://petersfancybrownhats.com/view?item=103",
              "webview_height_ratio": "tall",
            },
            "buttons":[
              {
                "type":"web_url",
                "url":"https://petersfancybrownhats.com",
                "title":"View Website"
              },{
                "type":"postback",
                "title":"Start Chatting",
                "payload":"DEVELOPER_DEFINED_PAYLOAD"
              }              
            ]      
          }
        ]
      }
    }
  }
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"

这是我在 C# 中尝试的方法,但它不起作用。我不确定我是否以正确的方式转换它。如有任何帮助,我们将不胜感激。

   Activity previewReply = stepContext.Context.Activity.CreateReply();

            previewReply.ChannelData = JObject.FromObject(
            new
            {
                attachment = new
                {
                    type = "template",
                    payload = new
                    {
                        template_type = "generic",
                        elements = new
                        {
                            title = "title",
                            subtitle = "subtitle",
                            image_url = "https://thechangreport.com/img/lightning.png",
                            buttons = new object[]
                            {
                                new
                                {
                                    type = "element_share,",
                                    share_contents = new
                                    {
                                        attachment = new
                                        {
                                            type = "template",
                                            payload = new
                                            {
                                                template_type = "generic",
                                                elements = new
                                                {
                                                        title = "x",
                                                        subtitle = "xx",
                                                        image_url = "https://thechangreport.com/img/lightning.png",
                                                        default_action = new
                                                        {
                                                             type = "web_url",
                                                             url = "http://m.me/petershats?ref=invited_by_24601",
                                                        },
                                                        buttons = new
                                                        {
                                                              type = "web_url",
                                                              url = "http://m.me/petershats?ref=invited_by_24601",
                                                              title = "Take Quiz",
                                                        },
                                                },
                                            },
                                         },
                                     },
                                },
                            },
                        },
                    },
                },
            });

            await stepContext.Context.SendActivityAsync(previewReply);

elementsbuttons 属性需要是列表。看看下面的示例模板。

var attachment = new
{
    type = "template",
    payload = new
    {
        template_type = "generic",
        elements = new []
        {
            new {
                title = "title",
                image_url = "https://thechangreport.com/img/lightning.png",
                subtitle = "subtitle",
                buttons = new object[]
                {
                    new {
                        type = "element_share",
                        share_contents = new {
                            attachment = new {
                                type = "template",
                                payload = new
                                {
                                    template_type = "generic",
                                    elements = new []
                                    {
                                        new {
                                            title = "title 2",
                                            image_url = "https://thechangreport.com/img/lightning.png",
                                            subtitle = "subtitle 2",
                                            buttons = new object[]
                                            {
                                                new {
                                                    type = "web_url",
                                                    url = "http://m.me/petershats?ref=invited_by_24601",
                                                    title = "Take Quiz"
                                                },
                                            },
                                        },
                                    },
                                },
                            }
                        },
                    },
                },
            },
        },
    },
};

reply.ChannelData = JObject.FromObject(new { attachment });

请注意,如果您的主模板与您尝试共享的模板不同,您只需向模板添加 share_contents 元素。否则,您的按钮可以只是 new { type = "element_share" },这样可以大大降低模板的复杂性。

此外,请务必 Whitelist 您的所有网址并确保所有图片网址都正常工作 - 其中有几个无法正常工作。如果 URL 未列入白名单且图像链接已损坏,则模板将不会呈现。

希望对您有所帮助!