Facebook Messenger Bot导出参数解析及Bot创建实操问询
I’m currently learning how to build a Facebook Messenger Bot and came across a detailed, high-quality article that breaks down the entire creation process step by step. One critical early step outlined in the article is configuring the webhook verification file.
Here’s what you need to follow:
- Create a file named
verification.jsin thecontrollers/directory of your project. - Paste the following code into this file (I’ve completed the truncated snippet to ensure the verification logic works as intended):
module.exports = (req, res) => { const hubChallenge = req.query['hub.challenge']; const hubMode = req.query['hub.mode']; const verifyTokenMatches = (req.query['hub.verify_token'] === 'crowdbotics'); if (hubMode && verifyTokenMatches) { res.status(200).send(hubChallenge); } else { res.status(403).send('Verification failed: Invalid token or mode'); } };
This code handles Facebook’s webhook verification flow: it checks if the incoming request has a valid hub.mode and if the verification token matches the one you’ve set (here, crowdbotics). If both conditions are met, it sends back the hub.challenge value to confirm your server is authorized to receive webhook events for your bot. If not, it returns a 403 error to reject the verification request.
内容的提问来源于stack exchange,提问作者user9504869

