问题描述
我正在构建一个简单的Facebook Messenger聊天机器人,并且无法按顺序发送消息。
I'm playing around with building a simple Facebook Messenger chatbot and I'm having trouble sending messages in sequence.
在上面的示例中,它应该依次打印 Hello!, 1, 2, 3。我目前正在找到的Facebook文档关注实现此简单的短信功能。我在下面包括了Express Node.JS服务器代码:
In the example above, it should have printed "Hello!", "1", "2", "3" in order. I'm currently following the Facebook docs found here to implement this simple text message function. I've included my Express Node.JS server code below:
定义 sendTextMessage()
函数:
var request = require("request");
function sendTextMessage(user, text) {
messageData = {
text: text
};
request({
url: "https://graph.facebook.com/v2.6/me/messages",
qs: {access_token: PAGE_ACCESS_TOKEN},
method: "POST",
json: {
recipient: {id: user},
message: messageData
}
}, function(error, response, body) {
if (error) {
console.log("Error sending message: ", error);
} else if (response.body.error) {
console.log("Error: ", response.body.error);
} else {
console.log("Message successfully send.")
}
});
}
使用它发送响应:
sendTextMessage(user, "Hello!");
sendTextMessage(user, "1");
sendTextMessage(user, "2");
sendTextMessage(user, "3");
我什至尝试实现一个简单的队列,该队列将消息放入队列并仅发送一个每次请求
的成功回调后一次出现一条消息。这让我怀疑我没有与Messenger API正确交互。
I even tried implementing a simple queue that queues messages and only sends one message at a time after each request
's success callback. This is making me suspect that I'm not interacting with the Messenger API correctly.
有人遇到过此问题吗?如何获得按顺序发送的消息?谢谢!
Has anyone encountered this issue? How can I get messages to send in sequence? Thanks!
因为我实现了一个简单的队列,但仍然遇到这种情况问题,我在这里包括了我的简单队列系统的代码。
Because I implemented a simple queue but still experiencing this problem, I'm including the code for my simple queue system here.
var queue = [];
var queueProcessing = false;
function queueRequest(request) {
queue.push(request);
if (queueProcessing) {
return;
}
queueProcessing = true;
processQueue();
}
function processQueue() {
if (queue.length == 0) {
queueProcessing = false;
return;
}
var currentRequest = queue.shift();
request(currentRequest, function(error, response, body) {
if (error || response.body.error) {
console.log("Error sending messages!");
}
processQueue();
});
}
queueRequest(/* Message 1 */);
queueRequest(/* Message 2 */);
queueRequest(/* Message 3 */);
更新
此错误已报告给Facebook,但听起来好像他们不会修复。请阅读Facebook帖子上的票证线程,以了解他们所说的话的详细信息。 (感谢露易丝(Louise)在此方面引起了Facebook的关注)
UPDATE
This "bug" was reported to Facebook but it sounds like they aren't going to fix it. Please read the ticket thread on Facebook's post here for details on what they say is going on. (Thank you to Louise for getting Facebook's attention on this)
推荐答案
我为此向Facebook提交了错误报告同样的问题。他们承认这确实是一个错误,并正在对其进行修复:
I submitted a bug report to Facebook about this because I was having the same problem. They acknowledged that it is indeed a bug and are working to fix it: https://developers.facebook.com/bugs/565416400306038
这篇关于Facebook Messenger机器人未按顺序发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!