It's actually simple so let me explain.
@app.route('/facebook/webhook', methods=['GET'])
def verify_webhook():
mode = request.args.get('hub.mode')
token = request.args.get('hub.verify_token')
challenge = request.args.get('hub.challenge')
if mode and token and mode == 'subscribe' and token == fb_app_token:
return challenge
else:
return 'Error, wrong token', 403
@app.route('/facebook/webhook', methods=['POST'])
def handle_webhook():
data = request.json
print('were here')
print('we got here', data)
if data['object'] == 'page':
for entry in data['entry']:
messaging = entry['messaging']
for message in messaging:
if 'message' in message:
sender_id = message['sender']['id']
chatbot_response = chatbot_service.call_chatbot([message['message'].get('text', '')])
# Reply with the same text received
send_message(sender_id, chatbot_response)
Say you expose these two functions as endpoints.
verify_webhook() has a string called fb_app_token, this is just a string of any value, I used a GUID value here.
also, the handle_webhook() is the actual callbackurl function you will also expose.
Notice the HTTP methods despite the same endpoint naming convention.
Facebook will send a GET request to the same CallbackUrl you provided.
So when you go to your developer account you will have these two options
Callback URL: https://www.myweb_or_ngrok.com/facebook/webhook
Verify Token: The GUID you generated for your fb_app_token
Facebook will send the VerifyToken value before any request in a GET request to your callbackurl. If they match, facebook affirms it has hit the right endpoint and it can now send the actual POST request to your callbackurl.
Too much mention of the word callbackurl :).
That should make you verify/register your callbackurl with FB.