【问题标题】:Facebook Messenger Nested Persistent Menu ErrorFacebook Messenger 嵌套持久菜单错误
【发布时间】:2017-10-26 06:25:26
【问题描述】:

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

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

响应正文错误

类型:'OAuthException',

错误:{ message: '(#100) 在参数“call_to_actions[0]”中发现无效键“call_to_actions”。',代码: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 request 包向 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"

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 谢谢!我尝试了节点 js 代码,但我不再看到我的开始按钮或菜单。我不再想那个错误信息了。并且发送的 JSON 响应只显示 {"object": "Object"} 而不是 {"result": "Successfully added new_thread's CTA"}
    • 再试一次。没有做任何改变。刚刚删除了之前尝试添加的入门按钮和菜单 ID。它奏效了。
    • 我注意到持久菜单缓存了它的状态。有时需要重新加载页面或重新启动应用程序才能看到更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2019-11-05
    相关资源
    最近更新 更多