2017-07-27 101 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"