编程语言
首页 > 编程语言> > javascript-Facebook Messenger嵌套持久菜单错误

javascript-Facebook Messenger嵌套持久菜单错误

作者:互联网

我正在尝试向我的聊天机器人添加一个NESTED持久菜单. Facebook有3个按钮的限制,但您可以有一个嵌套按钮,最多5个按钮.

这是我运行代码时遇到的错误

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)
      }
   });
}

当我删除嵌套按钮时,将显示持久菜单,因此我不确定错误是什么.我的代码与facebook在其persistent menu doc中发布的示例非常相似.我使用的是托管在heroku上的node.js进行编程,并在the code found here之后对菜单函数进行了建模.

问题:是否有人使用nodejs webhook使用npm请求包将请求发送到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"

解决方法:

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)
    }
})

}

标签:node-js,heroku,bots,javascript,facebook-graph-api
来源: https://codeday.me/bug/20191026/1932812.html