我正在尝试将NESTED持久性菜单添加到我的聊天机器人中。 Facebook有3个按钮的限制,但你可以拥有一个最多有5个按钮的嵌套按钮。
这是我运行代码时遇到的错误
响应正文错误
输入:'OAuthException',
错误:{message:'(#100)在param“call_to_actions [0]”中找到了无效的密钥“call_to_actions”。',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发送请求?如何添加嵌套的持久性菜单以及此错误的含义是什么?
编辑: 当我使用持久性菜单文档中的exact命令通过终端使用直接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"
答案 0 :(得分: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)
}
})
}