Simplify FB Messenger Bot Tutorial

Hey! I’m the author of messenger-bot, and saw it was being used in this post: https://hyperdev.com/blog/create-facebook-messenger-bot/

Figured I’d let you know you can simplify the bot.js code a bit :smile:. Instead of:

app.get('/bot', (req, res) => {
  return bot._verify(req, res);
});

app.post('/bot', (req, res) => {
  bot._handleMessage(req.body);
  res.end(JSON.stringify({status: 'ok'}));
});

You can just do:

app.use('/bot', bot.middleware());

It’ll register the same middleware with just one line, plus it’ll be a bit more secure since it’ll check incoming calls to the POST webhook for their integrity using your APP_SECRET.

Hope this helps! HyperDev is a pretty sick project.

1 Like

Thanks for the suggestion @bruggie! I tried it in the example, but unfortunately, it didn’t work - no errors, there was just no longer a response to incoming messages.

But thanks for making messenger-bot - very handy to get going with Facebook bots quickly!

Oh, really? Darn, haha. I’ll look into using the middleware with Express. I only have tests with the vanilla http module, so there’s probably some little difference in implementation I need to make. Ideally the _verify and _handleMessage should only be for advanced cases. Thanks for letting me know!

@bruggie I think the facebook developer site might have changed since you wrote the instructions for the chatbot…

these instructions:
"‘My Apps’ drop-down. Select ‘website’ and enter a name for your new app. For the site URL, you can enter the publish URL of your project - that’s the URL in the tab that opens when you click ‘Show’."

Do not match the FB page evelopers.facebook.com/apps/somenumbershere/settings at all…
:frowning:

Did you make a vid tutorial of the setup? If yes, could you share the URL?

thanks
Dennis

@pleabargain - yes, they keep changing that section. I’ve updated the instructions in the messenger-bot readme - they now no longer ask you to specify the type of app until a later stage.