无法在 Facebook Messenger 平台上发送按钮模板作为响应 (Node.js)
Unable to send button template as response on Facebook Messenger Platform (Node.js)
我正在使用 Node.js 在 Facebook Messenger 平台上开发聊天机器人。这是我用于设置文本响应的功能代码:
const fbReq = request.defaults({
uri: 'https://graph.facebook.com/me/messages',
method: 'POST',
json: true,
qs: {
access_token: Config.FB_PAGE_TOKEN
},
headers: {
'Content-Type': 'application/json'
},
});
const fbMessage = (recipientId, msg, cb) => {
const opts = {
form: {
recipient: {
id: recipientId,
},
message: {
text: msg,
},
},
};
fbReq(opts, (err, resp, data) => {
if (cb) {
cb(err || data.error && data.error.message, data);
}
});
};
我也可以通过这种方式设置图像响应。但是,当我尝试将响应设为按钮模板 (https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template) 时,没有收到任何响应。也不会抛出任何错误。
const fbInfo = (recipientId, cb) => {
const opts = {
form: {
recipient: {
id: recipientId,
},
message: {
attachment:{
type:"template",
text:"Check out our website",
payload:{
template_type:"button",
buttons:[
{
type:"web_url",
url:"https://some.website.com",
title:"Website"
}
]
}
}
}
}
};
fbReq(opts, (err, resp, data) => {
if (cb) {
cb(err || data.error && data.error.message, data);
}
});
};
而不是 form
你应该使用 json
.
看看我在glitch
上写的代码
应该是这样的:
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: <TOKEN> },
method: 'POST',
json: messageData}, ...)
我正在使用 Node.js 在 Facebook Messenger 平台上开发聊天机器人。这是我用于设置文本响应的功能代码:
const fbReq = request.defaults({
uri: 'https://graph.facebook.com/me/messages',
method: 'POST',
json: true,
qs: {
access_token: Config.FB_PAGE_TOKEN
},
headers: {
'Content-Type': 'application/json'
},
});
const fbMessage = (recipientId, msg, cb) => {
const opts = {
form: {
recipient: {
id: recipientId,
},
message: {
text: msg,
},
},
};
fbReq(opts, (err, resp, data) => {
if (cb) {
cb(err || data.error && data.error.message, data);
}
});
};
我也可以通过这种方式设置图像响应。但是,当我尝试将响应设为按钮模板 (https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template) 时,没有收到任何响应。也不会抛出任何错误。
const fbInfo = (recipientId, cb) => {
const opts = {
form: {
recipient: {
id: recipientId,
},
message: {
attachment:{
type:"template",
text:"Check out our website",
payload:{
template_type:"button",
buttons:[
{
type:"web_url",
url:"https://some.website.com",
title:"Website"
}
]
}
}
}
}
};
fbReq(opts, (err, resp, data) => {
if (cb) {
cb(err || data.error && data.error.message, data);
}
});
};
而不是 form
你应该使用 json
.
看看我在glitch
上写的代码应该是这样的:
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: <TOKEN> },
method: 'POST',
json: messageData}, ...)