heyoo icon indicating copy to clipboard operation
heyoo copied to clipboard

webhook got data even if the user didnt send a msg

Open rinab301 opened this issue 3 years ago • 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"] ... ...

rinab301 avatar Dec 22 '22 06:12 rinab301

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)

rinab301 avatar Dec 22 '22 11:12 rinab301

Hello @rinab301

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

Kalebu avatar Dec 29 '22 09:12 Kalebu

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.

soerenetler avatar Feb 03 '23 12:02 soerenetler

This might be related to #63

soerenetler avatar Feb 13 '23 13:02 soerenetler

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.

filipporomani avatar Apr 03 '23 05:04 filipporomani