Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webhook got data even if the user didnt send a msg #48

Open
rinab301 opened this issue Dec 22, 2022 · 5 comments
Open

webhook got data even if the user didnt send a msg #48

rinab301 opened this issue Dec 22, 2022 · 5 comments
Labels
good first issue Good for newcomers

Comments

@rinab301
Copy link

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
Copy link
Author

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)

@Kalebu
Copy link
Contributor

Kalebu commented Dec 29, 2022

Hello @rinab301

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

@Kalebu Kalebu added the good first issue Good for newcomers label Dec 29, 2022
@soerenetler
Copy link
Collaborator

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
Copy link
Collaborator

This might be related to #63

@filipporomani
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants