Facebook Messenger - 无效的密钥类型,有效负载

时间:2017-07-27 13:18:58

标签: php facebook facebook-messenger

我在为Facebook Messenger构建应用时遇到了一些问题。当我尝试发送"通用模板"按钮返回Facebook Messenger的响应我收到以下错误。

{"error":{"message":"(#100) Invalid keys \"type, payload\" were found in param \"name_placeholder[elements][buttons]\".","type":"OAuthException","code":100,"fbtrace_id":""}}

最合乎逻辑的错误是我向Facebook提供了错误的密钥。所以我将我的输入与Facebook docs进行了比较。不幸的是,我无法找到原因。

可用日志/数据

我发送给Facebook的数据:

{"recipient":{"id":"REMOVED_ID"},"message":{"attachment":{"type":"template","payload":{"template_type":"generic","elements":{"0":{"title":"title","image_url":"https://i.ytimg.com/vi/JIciUWPzTxM/hqdefault.jpg","subtitle":"body text","default_action":{"type":"web_url","url":"https://www.google.nl/"}},"buttons":"{\"type\":\"postback\",\"title\":\"Bookmark Item\",\"payload\":\"DEVELOPER_DEFINED_PAYLOAD\"}"}}}}}

More information

我从Facebook收到的数据:

{"error":{"message":"(#100) Invalid keys \"type, payload\" were found in param \"name_placeholder[elements][buttons]\".","type":"OAuthException","code":100,"fbtrace_id":""}}

建议吗?

1 个答案:

答案 0 :(得分:0)

在有效负载中,elementsbuttons 必须数组类型,但您要将它们作为对象类型发送:

这是你的

{
  "recipient": {
    "id": "REMOVED_ID"
  },
  "message": {
    "attachment": {
      "type": "template",
      "payload": {
        "template_type": "generic",
        "elements": {
          "0": {
            "title": "title",
            "image_url": "https:\/\/i.ytimg.com\/vi\/JIciUWPzTxM\/hqdefault.jpg",
            "subtitle": "body text",
            "default_action": {
              "type": "web_url",
              "url": "https:\/\/www.google.nl\/"
            }
          },
          "buttons": "{\"type\":\"postback\",\"title\":\"Bookmark Item\",\"payload\":\"DEVELOPER_DEFINED_PAYLOAD\"}"
        }
      }
    }
  }
}

正确的应该是:

{
  "recipient": {
    "id": "REMOVED_ID"
  },
  "message": {
    "attachment": {
      "type": "template",
      "payload": {
        "template_type": "generic",
        "elements": [
          {
            "title": "title",
            "image_url": "https:\/\/i.ytimg.com\/vi\/JIciUWPzTxM\/hqdefault.jpg",
            "subtitle": "body text",
            "default_action": {
              "type": "web_url",
              "url": "https:\/\/www.google.nl\/"
            }
          },
          "buttons": [{"type":"postback","title":"Bookmark Item","payload":"DEVELOPER_DEFINED_PAYLOAD"}]
        ]
      }
    }
  }
}

工作样本:

curl -X POST -H "Content-Type: application/json" -d '{
  "recipient":{
    "id":"USER_ID"
  },
  "message":{
    "attachment":{
      "type":"template",
      "payload":{
        "template_type":"generic",
        "elements":[
           {
            "title":"Welcome to Peter\'s Hats",
            "image_url":"https://petersfancybrownhats.com/company_image.png",
            "subtitle":"We\'ve got the right hat for everyone.",
            "default_action": {
              "type": "web_url",
              "url": "https://peterssendreceiveapp.ngrok.io/view?item=103",
              "messenger_extensions": true,
              "webview_height_ratio": "tall",
              "fallback_url": "https://peterssendreceiveapp.ngrok.io/"
            },
            "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"