Neurotech-HQ/heyoo

webhook got data even if the user didnt send a msg

rinab301 opened this issue · 5 comments

hi,
I am writing in python a webhook to use in my meta whatsapp app.
The webhook is called even if the user didnt send any messages. (It prints "Received webhook data" from the code below and continue).
What do I need to change in this code to make sure the webhook really got a message and not some other request from whatsapp? which condition do I need to check in the request\data and do return if its not a real message? or change somthing in the changed_field condition?
Thank you

@app.route("/", methods=["GET", "POST"])
def hook():
code...
code...
# Handle Webhook Subscriptions
data = request.get_json()
logging.info("Received webhook data: %s", data)
changed_field = messenger.changed_field(data)
if changed_field == "messages":

where changed_field is:
return data["entry"][0]["changes"][0]["field"]
...
...

see message examples in: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples
I added new def in heyoo lib:

def is_message(self, data):
    data = self.preprocess(data)
    if "messages" in data:
        return True
    else:
        return False

and used it in the webhook:  
        
  logging.info("Received webhook data: %s", data)
  changed_field = messenger.changed_field(data)
  if changed_field == "messages":
      #new_message = messenger.get_mobile(data)       <---------------- here is the problem 
      new_message = messenger.is_message(data)          <----------------
      if new_message:
          message = "null"
          mobile = messenger.get_mobile(data)
          name = messenger.get_name(data)
          message_type = messenger.get_message_type(data)

Hello @rinab301

Can you please clarify more on how those two lines leads to a bug ?

The problem is not super clear to me. And can have two causes.

  1. The update you receive is of some other type (information about the change of a message status - e.g. send to received). It would be helpful to print the update to know more.
  2. The message you receive is older. Sometime e.g. if you bot was unavailable during the time the message was send you bot will get the update later. This might not be directly after you start your bot, but it can take some time. Please dubblechek the timestamp in the update you receive.

What exactly do your changed line of code do?
isn't checking for the message key in the object the same as checking for the "field" == "message". Shouldn't that yield the same value? Can you please share the update you have this problem with.

This might be related to #63

I found out that sometimes Facebook sends me a verification payload even after the first verification. This verification payload doesn't contain any message data, just the verification stuff. Idk if this is related.