0

I'd like to connect a chatbot made in python and deployed on aws lambda to a facebook page.

this is my code to verifly connection to facebook

def webhook(event, context):
        # #debug
        print("event:" )
        print(event)
        # print("context")
        # print(context)

        #handle webhook challenge
        if keys_exist(event, ["queryStringParameters"]):
            print("Veriflying stuff")
            v_token   = str(find_item(event, 'hub.verify_token'))
            print("token :")
            print (v_token)
            challenge = int(find_item(event, 'hub.challenge'))
            print ("challenge")
            print(challenge)
            if (os.environ['verify_token'] == v_token):
                print ("returning stuff")
                return (challenge)

But facebook says The URL couldn't be validated. Callback verification failed with the following errors: HTTP Status Code = 502; HTTP Message = Bad Gateway

I have created the urls with serverless. It works well when i do a get request from the browser.

I have given the same url in the facebook Webhook page. And made sure the validation and Verify Token are correct.

I have tried a few things I saw online. But i dont understand a few of them like this one Facebook Messenger API "URL COULD NOT BE VALIDATED" I dont understand if I nned a cert file for this?

AND "The URL could not be validated", facebook api error says to give path to a php. Which I dont even use?

Arsenal Fanatic
  • 3,663
  • 6
  • 38
  • 53

1 Answers1

0

I have found the problem. Facebook also now required the status code of the request.

if keys_exist(event, ["queryStringParameters","hub.verify_token","hub.challenge"]):
            print("subscribe to webhook invoked")
            v_token   = str(find_item(event, 'hub.verify_token'))
            challenge = find_item(event, 'hub.challenge')
            if ("strongtoken" == v_token):
                response = {
                    "statusCode": 200,
                    "body": str(challenge)
                }
                print(challenge)
                return response
Arsenal Fanatic
  • 3,663
  • 6
  • 38
  • 53