【问题标题】:res.send(200) issue on facebook messenger botfacebook messenger bot 上的 res.send(200) 问题
【发布时间】:2018-01-15 01:35:01
【问题描述】:

我正在尝试使用 nodejs 构建一个 facebook messenger 机器人。我开发了具有核心功能的机器人。在测试时 用户发送 GIF 或贴纸的负面情况,它必须回应“我无法得到你。对不起”。它确实发送 该消息,但它挂起并在此后每隔几分钟继续发送该消息。我注意到 ngrok 服务器抛出了 500 HTTP POST 请求的内部服务器错误。在进一步调试中,我发现 res.send(200) 没有正确执行。 我在 res.send(200) 之后拥有的 console.log stmt 永远不会被打印出来。不知道我可能会错过什么。任何帮助表示赞赏。尝试重新启动服务器并使用新的 ngork https 链接重新订阅该应用程序。继续打印相同的消息:(。

这是我的代码。

    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();
    });

【问题讨论】:

    标签: node.js facebook-messenger facebook-messenger-bot ngrok


    【解决方案1】:

    也许这个答案不能解决您的问题,但它可能会有所帮助。 如果您想发送 200 HTTP 代码,请改用:

    res.sendStatus(200); // equivalent to res.status(200).send('OK')

    另一方面,如果这不是中间件,您可以删除return next(); 这一行。

    【讨论】:

    • 是的。我也尝试过 res.sendStatus(200) 。没有运气。这也是一个中间件,这就是我有 next() 的原因。不过感谢您的观看。
    猜你喜欢
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多