Search code examples
facebookfacebook-messengerfacebook-chatbot

Facebook Chatbot Webhook - Why should I implement the get-request?


We currently develop our first chatbot on for the facebook messenger platform. I started with the quick-start sample here: https://developers.facebook.com/docs/messenger-platform/guides/quick-start

Everything works fine, the Bot calls my backend, I can respond. All good.

But, the following GET-Hook is never called:

app.get('/hook/', (req, res) => {
    console.log('GET REQUEST CALLED');
    if (req.query['hub.verify_token'] === FB_VERIFY_TOKEN) {
        res.send(req.query['hub.challenge']);
    } else {
        res.send('Error, wrong validation token');
    }
});

The quick-start and the full guide tell me, that I need that peace of code, but none of them tell me why...

My question is:

  • When should this webhook be called?
  • Why does my bot work without this hook?
  • Shouldn't it somehow verify my backend? And why is that needed?

I think I completely missed something here :/

Edit: Is this only needed when I subscribe to the page programmatically? Currently, I've added a subscription to the page using the dashboard.


Solution

  • When you setup your webhook URL from developers panel. Facebook automatically sends a GET request to that endpoint and expect that you are validating the verification token.

    This endpoint is kind a verification endpoint for Facebook. It acknowledges that Facebook does not send chat information to wrong endpoints which may cause privacy abuse. Also, it verifies that your endpoint is ready to process Facebook input data.