尝试向此 url 发送请求
https://graph.facebook.com/v2.6/PAGE_ID/thread_settings?access_token=PAGE_ACCESS_TOKEN
但是没有用。
对 API JSON 正文进行 POST 调用,如下所示。
curl -X POST -H "Content-Type: application/json" -d '{
"setting_type":"call_to_actions",
"thread_state":"new_thread",
"call_to_actions":[
{
"payload":"USER_DEFINED_PAYLOAD"
}
]
}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=PAGE_ACCESS_TOKEN"
当前格式为 https://graph.facebook.com/v2.6/me/messenger_profile?access_token=PAGE_ACCESS_TOKEN
{
"get_started":{
"payload":"GET_STARTED_PAYLOAD"
}
}
您可能已成功设置它,但没有看到它,因为您已经与 facebook 页面进行了现有对话。
成功设置“开始”话题后,只有在删除现有对话话题并开始新话题时才能看到它。
开始按钮仅在您第一次与 facebook 页面交互时显示,因此如果您之前已向该页面发送消息,除非您从 Facebook Messenger 客户端删除线程,否则您将无法看到“开始”(移动或桌面)。
看到欢迎屏幕和开始按钮有一定的条件:
- 它们仅在用户第一次与 Messenger 上的页面交互时呈现
- 当应用程序处于开发模式时,只有应用程序的管理员/开发人员/测试人员可以看到它
- 您的应用必须订阅 webhook 上的回发
感谢您的宝贵意见,经过一些变通方法发现此解决方案有效,根据 facebook 指南
只需向该 URL 发送一次独立的 POST 请求
https://graph.facebook.com/v2.6/PAGE_ID/thread_settings?access_token=PAGE_ACCESS_TOKEN
使用邮递员在这里发送这样的请求
如果 Get Started 按钮设置成功,您将收到以下响应
{
"result": "Successfully added new_thread's CTAs"
}
您必须运行适当的 curl 命令来设置它。检查此链接并查看他们的示例。https://developers.facebook.com/docs/messenger-platform/implementation#send_api
npm 中有一个库,其中包含 POST/DELETE 操作的功能:https ://www.npmjs.com/package/fb-get-started-button
$ npm install -g fb-get-started-button
$ fb-get-started-button add <YOUR PAGE ACCESS TOKEN>
Adding "Get Started" button with the payload "GET_STARTED"
Successfully added new_thread's CTAs
$ fb-get-started-button remove <YOUR PAGE ACCESS TOKEN>
Removing "Get Started" button
Successfully deleted all new_thread's CTAs
使用您的页面访问令牌发送发布请求
https://graph.facebook.com/v2.6/me/messenger_profile?access_token=YOUR-TOKEN
有以下数据
{
"get_started":{
"payload":"<GET_STARTED_PAYLOAD>"
}
}
Facebook 文档: 开始按钮
希望这种新方法能解决您的问题。不要忘记首先使用 Facebook Web 删除已发送的消息,以查看按钮的作用。
我认为更好的解决方案是使用 Microsoft Bot Framework 并使用它的 /firstRun 发送信使开始按钮
function firstRun(session) {
console.log('This user is running our bot the first time')
createUser(session)
platforms.firstRun(session.message.user.id, session.message.address.channelId)
.then((values) => {
for (let value of values) {
if (value.data.firstName && value.data.lastName) {
session.userData.user.profile = value.data
}
}
})
.catch((errors => {
console.log(errors);
}))
reply(session)
session.endDialog()
}
platform.firstRun 如下所示
platforms.firstRun = function (userId, channel) {
switch (channel) {
case platforms.channels.emulator:
return Promise.reject('none')
case platforms.channels.facebook:
return platforms.facebook.firstRun(userId)
case platforms.channels.skype:
return Promise.reject('none')
default:
return Promise.reject('none')
}
}
这反过来又调用了platforms.facebook.firstRun
platforms.facebook.firstRun = function (userId) {
return Promise.all([
platforms.facebook.sendThread(facebookTemplates.greet(), 'Greeting'),
platforms.facebook.sendThread(facebookTemplates.getStarted(), 'Get Started'),
platforms.facebook.sendThread(facebookTemplates.getPersistentMenu(), 'Persistent Menu'),
platforms.facebook.sendThread(facebookTemplates.getDomainWhitelisting(), 'Domain Whitelisting'),
platforms.facebook.getProfile(userId)
])
}
platform.facebook.sendThread 如下所示 // 调用 Facebook 图形 api 来更改机器人设置
platforms.facebook.sendThread = function (template, cmd) {
return new Promise((resolve, reject) => {
// Start the request
request({
url: platforms.facebook.GRAPH_BASE_URI + '/me/thread_settings?access_token=' + endpoints.FACEBOOK_PAGE_ACCESS_TOKEN,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
form: template
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
// Print out the response body
resolve({ status: response.statusCode, data: body })
} else {
// TODO: Handle errors
reject({ status: response.statusCode, data: error })
}
});
})
}
请注意 facebookTemplates.getStarted(),它实际上具有用于入门的 json,如下所示
templates.getStarted = function () {
return {
setting_type: "call_to_actions",
thread_state: "new_thread",
call_to_actions: [
{
payload: payloads.FACEBOOK_GET_STARTED
}
]
}
}
完全可插拔的代码架构,用于在所有聊天机器人平台上执行首次运行操作。在我的机器人上完美运行HERE
非常简单的解决方案,只需在终端中打开并转到您的主机文件夹位置(在我/var/www/html/booking/public/facebookbot
的)并粘贴以下代码:
curl -X POST -H "Content-type: application/json" -d '{
"setting-type":"call_to_actions",
"thread_state":"new_thread",
"get_started":{
"payload":"GET_STARTED_PAYLOAD"
}
}' "https://graph.facebook.com/v2.6/me/messenger_profile?access_token=YOUR_ACCESS_TOKEN"
并按回车键,确保输入正确的访问令牌,您还可以识别何时从上述代码示例中的有效负载(即 GET_STARTED_PAYLOAD)按下开始按钮。
在我们的例子中,以下工作:
点击thread_settings
API
https://graph.facebook.com/v2.6/me/thread_settings?access_token=<YOU FACEBOOK PAGE'S PAGE ACCESS TOKEN>
传递了以下示例 JSON
{ "setting_type": "call_to_actions", "thread_state": "new_thread", “call_to_actions”:[ { “有效载荷”:“开始” } ] }
{ "result": "成功添加 new_thread 的 CTA" }