问题
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 :(.
Here is my code.
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();
});
回答1:
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')
On the other hand, if this is not a middleware, you can remove the return next();
line.
来源:https://stackoverflow.com/questions/48255932/res-send200-issue-on-facebook-messenger-bot