我想在Messenger上针对单个用户触发的回发发送多个回复。我一直在关注Messenger的developer documentation并且无法真正找到如何做到这一点。
我的代码结构与他们在网站上给出的教程非常相似,我有一个'handlePostback'函数,用于标识收到的回发并将其与一组预定义的有效负载进行比较,以找到'响应'JSON对象。这个响应被赋予'callSendAPI',它将这个JSON对象放入将消息发送回Messenger API的基本格式。
function handlePostback(sender_psid,receivedPostback)
{ if(payload== 'defined_payload') {
response = {
text: 'Some text'
};
callSendAPI(sender_psid,response);
}
function callSendAPI(sender_psid,response) {
let body = {
recipient: {
id= sender_psid
},
message: response
};
// Followed by code for POST request to the webhook
}
这是基本结构,现在我想发送多条消息作为回复一个回发。我做了一些挖掘,我发现解决方案可能是创建一个message []数组。但是我该怎么做?因为我的'响应'是通过该函数生成的,并且消息结构应该看起来像这样(我认为):
let body = {
recipient: {
id=sender_psid
},
messages: [ {
response1
},
{
response2
}
]
};
我希望我能解释一下我的问题,如果我能提供更多细节,请告诉我!
好问题。如果您不熟悉Node.js,那么这样做的方式并不太明显,而且在Facebook的Send API文档中没有很好地记录。
首先,您使用数组发送多条消息的方法可能无法正常工作。 Facebook有一个解决方案,可以通过一个请求发送多达100个API调用,但在我看来,在您的情况下不需要这样做。如果你想了解更多关于它的信息,请查看Batched Request Documentation,你会发现实现与你的不同。
一个可行的解决方案是多次调用callSendAPI
函数。但是这个解决方案有一个主要缺点:您将无法控制发送的消息的实际顺序。例如,如果要发送两条单独的消息,则无法保证将首先发送给用户。
要解决此问题,您需要以保证下一个callSendAPI
调用仅在第一个消息已发送后才会发生的方式链接您的callSendAPI
函数。您可以使用回调或承诺在NodeJS中执行此操作。如果你不熟悉它们中的任何一个,你可以阅读this的回调和this的承诺。
您需要修改callSendAPI
函数,尤其是将POST请求发送到Facebook的部分。我将使用promises和模块node-fetch为您的问题提供解决方案。
const fetch = require('node-fetch');
function handlePostback(sender_psid,receivedPostback){
if (payload == 'defined_payload') {
response = {
text: 'Some text'
};
response2 = //... Another response
response3 = //... Another response
callSendAPI(sender_psid,response).then(() => {
return callSendAPI(sender_psid, response2).then(() => {
return callSendAPI(sender_psid, response3); // You can add as many calls as you want
});
});
}
}
function callSendAPI(sender_psid,response) {
let body = {
recipient: {
id= sender_psid
},
message: response
};
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN); // Here you'll need to add your PAGE TOKEN from Facebook
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(body),
});
}
我发现下面的链接对于解决在单个帖子后面实现多个响应的方法非常有用。
https://codingislove.com/build-facebook-chat-bot-javascript/
就像你说的,数组应该工作。创建具有多个响应消息的数组变量
var multipleResponse = {
messages: [{
response1
},
{
response2
}]
};
并将数组变量推送到您的函数
function callSendAPI(sender_psid,response) {
let body = {
recipient: {
id= sender_psid
},
message: []
};
// Followed by code for POST request to the webhook
}
最后将数组推送到函数数组
body.message.push(multipleResponse.messages);
@Christos Panagiotakopoulos。我没有得到我使用链接的mainMenuResponse。相反,我得到的回应是三次。 handlePostback function =>
// Handles messaging_postbacks events
function handlePostback(sender_psid, received_postback) {
let response;
// Get the payload for the postback
let payload = received_postback.payload;
// Set the response based on the postback payload
if (payload === 'fashionTip') {
response = { "text": getFashionTip() } // calls a function which gives a fashion-tip
// Send the message to acknowledge the postback
callSendAPI(sender_psid, response).then(() => {
return callSendAPI(sender_psid, mainMenuResponse)
});
callSendAPI function =>
// Sends response messages via the Send API
function callSendAPI(sender_psid, response) {
// construct the message body
let request_body = {
"recipient": {
"id": sender_psid
},
"message": response
}
// Send the HTTP request to the messenger platform
request({
"uri": "https://graph.facebook.com/v2.6/me/messages",
"qs": {"access_token": PAGE_ACCESS_TOKEN},
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log("Message sent!");
} else {
console.error("Unable to send mesage:" + err);
}
});
}
不要修改callSendAPI
功能。在你的handlePostback
函数中多次调用callSendAPI
。
callsendAPI(sender_psid,response1);
callsendAPI(sender_psid,response2);