2017-05-25 87 views
3

我想添加一個嵌套持久菜單到我的聊天機器人。 Facebook有3個按鈕的限制,但你可以有一個最多5個按鈕的嵌套按鈕。Facebook Messenger嵌套持久菜單錯誤

這是當我跑我的代碼

response body error

type: 'OAuthException',

Error: { message: '(#100) Invalid keys "call_to_actions" were found in param "call_to_actions[0]".', code: 100}

在這裏,我得到的錯誤是我的代碼:

function addPersistentMenu(){ 
    request({ 
    url: "https://graph.facebook.com/v2.6/me/thread_settings", 
    qs: {access_token: token}, 
    method: "POST", 
    json:{ 
     setting_type : "call_to_actions", 
     thread_state : "existing_thread", 
     call_to_actions : [ 
     { 
      type: "nested", 
      title: "Menu Item One", 
      call_to_actions: [ 
      { 
       type: "postback", 
       title: "Nested Item One", 
       payload: "NESTED_ONE" 
      }, 
      { 
       type: "postback", 
       title: "Nested Item Two", 
       payload: "NESTED_TWO" 
      } 
      ] 
     }, 
     { 
      type: "postback", 
      title: "Menu Item Two", 
      payload: "TWO" 
     }, 
     { 
      type: "postback", 
      title: "Menu Item Three", 
      payload: "THREE" 
     } 
     ] 
    } 
    }, function(error, response, body) { 
     if(error){ 
     console.log('sending error') 
     console.log('Error sending messages: ', error) 
     }else if(response.body.error){ 
     console.log('response body error') 
     console.log('Error: ', response.body.error) 
     } 
    }); 
} 

當我刪除嵌套的按鈕,連續的菜單出現,所以我不確定錯誤是什麼。我的代碼與persistent menu doc中facebook發佈的示例非常相似。我正在使用node.js進行編程,託管在heroku上,我在the code found here之後對我的菜單功能進行了建模。

問題:是否有人使用npm請求包使用nodejs webhook完成此操作以向messenger發送請求?我如何添加我的嵌套持久菜單,這個錯誤是什麼意思?

編輯: 當我通過終端使用持久菜單文檔中的確切命令使用直接CURL POST時,會添加嵌套的持久菜單。我不確定要添加到我的nodejs webhook版本的此請求以使其工作。

這是curl命令:

curl -X POST -H "Content-Type: application/json" -d '{ 
    "persistent_menu":[ 
    { 
     "locale":"default", 
     "composer_input_disabled":true, 
     "call_to_actions":[ 
     { 
      "title":"My Account", 
      "type":"nested", 
      "call_to_actions":[ 
      { 
       "title":"Pay Bill", 
       "type":"postback", 
       "payload":"PAYBILL_PAYLOAD" 
      }, 
      { 
       "title":"History", 
       "type":"postback", 
       "payload":"HISTORY_PAYLOAD" 
      }, 
      { 
       "title":"Contact Info", 
       "type":"postback", 
       "payload":"CONTACT_INFO_PAYLOAD" 
      } 
      ] 
     }, 
     { 
      "type":"web_url", 
      "title":"Latest News", 
      "url":"http://petershats.parseapp.com/hat-news", 
      "webview_height_ratio":"full" 
     } 
     ] 
    }, 
    { 
     "locale":"zh_CN", 
     "composer_input_disabled":false 
    } 
    ] 
}' "https://graph.facebook.com/v2.6/me/messenger_profile?access_token=YOUR_ACCESS_TOKEN_HERE" 

回答

4

Facebook的Messenger的API已更新爲嵌套持久菜單。 'call_to_actions'風格似乎仍然適用於非嵌套菜單。

但是,嵌套菜單需要不同的API調用。差異似乎是URL必須是'messenger_profile'而不是'thread_settings'。出於某種原因,還需要'get_started'處理程序。最後,json數組被命名爲'persistent_menu'。

我更新了gitub上的示例bot。鍵入'添加菜單'和'刪除菜單'以查看持久菜單出現/消失。某些瀏覽器可能需要重新加載頁面或兩個頁面。

下面是一些草率的nodejs代碼,應該做的伎倆。

function addPersistentMenu(){ 
request({ 
    url: 'https://graph.facebook.com/v2.6/me/messenger_profile', 
    qs: { access_token: PAGE_ACCESS_TOKEN }, 
    method: 'POST', 
    json:{ 
    "get_started":{ 
    "payload":"GET_STARTED_PAYLOAD" 
    } 
} 
}, function(error, response, body) { 
    console.log(response) 
    if (error) { 
     console.log('Error sending messages: ', error) 
    } else if (response.body.error) { 
     console.log('Error: ', response.body.error) 
    } 
}) 
request({ 
    url: 'https://graph.facebook.com/v2.6/me/messenger_profile', 
    qs: { access_token: PAGE_ACCESS_TOKEN }, 
    method: 'POST', 
    json:{ 
"persistent_menu":[ 
    { 
     "locale":"default", 
     "composer_input_disabled":true, 
     "call_to_actions":[ 
     { 
      "title":"My Account", 
      "type":"nested", 
      "call_to_actions":[ 
      { 
       "title":"Pay Bill", 
       "type":"postback", 
       "payload":"PAYBILL_PAYLOAD" 
      }, 
      { 
       "title":"History", 
       "type":"postback", 
       "payload":"HISTORY_PAYLOAD" 
      }, 
      { 
       "title":"Contact Info", 
       "type":"postback", 
       "payload":"CONTACT_INFO_PAYLOAD" 
      } 
      ] 
     }, 
     { 
      "type":"web_url", 
      "title":"Latest News", 
      "url":"http://foxnews.com", 
      "webview_height_ratio":"full" 
     } 
     ] 
    }, 
    { 
     "locale":"zh_CN", 
     "composer_input_disabled":false 
    } 
    ] 
    } 

}, function(error, response, body) { 
    console.log(response) 
    if (error) { 
     console.log('Error sending messages: ', error) 
    } else if (response.body.error) { 
     console.log('Error: ', response.body.error) 
    } 
}) 

} 
+0

謝謝!我嘗試了節點js代碼,我沒有看到我的開始按鈕,或菜單。我沒有再想到那個錯誤消息。併發送的JSON響應顯示{「object」:「Object」}而不是{「result」:「成功添加new_thread的CTA」} –

+0

再次嘗試。沒有改變。剛剛刪除了開始使用按鈕和菜單ID試圖以前添加。有效。 –

+0

我注意到持久菜單緩存它的狀態。重新加載頁面或重新啓動應用程序重複您有時需要看到更新 –