2017-05-25 62 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

我注意到持久菜单缓存它的状态。重新加载页面或重新启动应用程序重复您有时需要看到更新 –