facebook Messenger bot上的res.send(200)问题 [英] res.send(200) issue on facebook messenger bot
问题描述
我正在尝试使用nodejs构建一个Facebook Messenger机器人.我开发了具有核心功能的机器人.在测试时 如果用户发送了GIF或不干胶标签,那么它必须回答我无法理解你.抱歉".它确实发送 该消息,但它挂起并此后每隔几分钟继续发送该消息.我注意到ngrok服务器抛出了500 HTTP POST请求的内部服务器错误.在进一步调试中,我发现res.send(200)没有正确执行. 我在res.send(200)之后拥有的console.log stmt从未被打印过.不知道我可能会缺少什么.任何帮助表示赞赏.尝试重新启动服务器,并使用新的ngork https链接重新订阅了该应用程序.相同的消息继续被打印出:(.
I am trying to build a facebook messenger bot using nodejs. I got the bot developed with core features. While testing for a negative scenario where the user sends in a GIF or sticker, it has to respond "I couldn't get you. Sorry". It does send that message but it hangs and keeps sending that message thereafter every few minutes. I noticed that the ngrok server threw an 500 HTTP Internal Server Error for the POST request. On further debugging, I was able to find out that res.send(200) is not getting executed properly. The console.log stmt that I have after res.send(200) never gets printed. Not sure what I may be missing. Any help is appreciated. Tried restarting the server and resubscribed to the app with a new ngork https link. The same message continues to get printed out :(.
这是我的代码.
server.post('/', (req, res, next) => {
// Extract the body of the POST request
let data = req.body;
let incomingMessage = '';
if(data.object === 'page') {
data.entry.forEach(pageObj => {
// Iterate through the messaging Array
pageObj.messaging.forEach(msgEvent => {
incomingMessage = {
sender: msgEvent.sender.id,
timeOfMessage: msgEvent.timestamp,
message: msgEvent.message
}
});
});
}
const {
message,
sender
} = incomingMessage
if(message.text) {
f.txt(sender, 'Hi there!!!');
} else {
f.txt(sender, `I couldn't get you. Sorry :(`);
//res.send(200);
}
res.send(200);
console.log('We are at the end of post');
return next();
});
推荐答案
也许这个答案不能解决您的问题,但可能会有所帮助. 如果要发送200个HTTP代码,请改用此代码:
Maybe this answer doesn't resolve your problem but it can be helpful. If you want to send a 200 HTTP code use this instead:
res.sendStatus(200); // equivalent to res.status(200).send('OK')
另一方面,如果这不是中间件,则可以删除return next();
行.
On the other hand, if this is not a middleware, you can remove the return next();
line.
这篇关于facebook Messenger bot上的res.send(200)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!