1

I'm creating a chatbot with the Facebook Messenger developer platform.

My problem comes as I'm trying to set up the Webhook using a ngrok tunnel as callback_url and using the figaro gem to add my access_token and verify_token to my app.

Somehow, the test never passes and I don't know why.

My ngrok site works perfectly in the browser. Yet I think the problem stems from the fact that the Webhook isn't receiving the information it wants, aka the access_token & verify_token from how I understand it.

I have tried redirecting the ngrok tunnel to a page that returns the environment variables of both (ENV["ACCESS_TOKEN"] & ENV["VERIFY_TOKEN"]) yet this doesn't work. So I'm just not sure what the API is expecting and why it's not getting it.

Thanks for the help!!!!!

screenshot of what I see on the fb developer platform

  • Your bot needs to respond to the verification challenge as described here, https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup#verification – misorude Sep 07 '18 at 09:25

1 Answers1

0

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.

ChampR
  • 752
  • 7
  • 25