为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
紫衣仙女 2022-10-23 14:02:48
我很难将这个通用的 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[]                              
查看完整描述

1 回答

?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

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


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" },这使得模板变得不那么复杂。


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


查看完整回答
反对 回复 2022-10-23
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信