2017-07-27 102 views
0

我在为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":""}} 

建议好吗?

+0

我打消了我的收件人ID&fbtrace_id了这个问题。 –

+0

你为什么将'buttons'的值作为字符串发送? – CBroe

+0

@CBroe我试图发送我的按钮作为字符串,数组,对所有类型的对象相同的错误。一个按钮必须是一个对象吗? –

回答

0

在有效载荷,elementsbuttonsMUST阵列类型,但要发送作为对象类型两者:

这里是

{ 
    "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"