【问题标题】:Converting JSON code of Facebook's Generic Template to C# in Bot framework在 Bot 框架中将 Facebook 通用模板的 JSON 代码转换为 C#
【发布时间】:2019-03-13 01:00:25
【问题描述】:

我很难将这个通用的 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);

【问题讨论】:

    标签: c# json botframework messenger


    【解决方案1】:

    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 未列入白名单且图片链接已损坏,则模板不会呈现。

    希望这会有所帮助!

    【讨论】:

    猜你喜欢
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多