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

标签 javascript node.js facebook-graph-api heroku bots

我正在尝试向我的聊天机器人添加一个 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 之后为我的菜单功能建模。 .

问题:有人使用 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"

最佳答案

Facebook Messenger API 已针对嵌套持久菜单进行了更新。 “call_to_actions”样式似乎仍适用于非嵌套菜单。

然而,嵌套菜单需要不同的 API 调用。区别似乎是 URL 必须指向“messenger_profile”而不是“thread_settings”。出于某种原因,还需要“get_started”处理程序。最后,json 数组被命名为“persistent_menu”。

我更新了示例 bot在 gitub 上。键入“添加菜单”和“删除菜单”以查看永久菜单出现/消失。在某些浏览器上可能需要重新加载一两次页面。

这是一些草率的 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)
    }
})

}

关于javascript - Facebook Messenger 嵌套持久菜单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44183278/

相关文章:

javascript - 在 Node.js 中使用函数创建对象

javascript - 如何使用基于mysql的node js(ft.json解析)

javascript - new EventEmitter() 与 new EventEmitter<Something>()

JavaScript 代码补全完成了吗?

javascript - Webpack + React 和 Google Chrome 43.0.2357.65

javascript - 模态弹出打印问题

javascript - 如何在运行一组 Mocha 测试之前读取文件

Facebook 访问 token 因未知原因过期

php - 发布到之前页面后返回(graph api facebook php sdk)

java程序无需登录即可在Facebook墙页上发布