res.send(200) issue on facebook messenger bot

旧街凉风 提交于 2019-12-24 20:42:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!