Secreto31126 / whatsapp-api-js

A TypeScript server agnostic Whatsapp's Official API framework
MIT License
128 stars 31 forks source link

System notifications bug #186

Closed Secreto31126 closed 10 months ago

Secreto31126 commented 11 months ago

Describe the bug System notifications break the phone variable in post() handler.

To Reproduce Steps to reproduce the behavior:

  1. Get a phone change system notification
  2. See error:
Error Unknown: TypeError: Cannot read properties of undefined (reading '0')
    at Object.post (requests:74:37)
    at doPost (https:28:55)

Expected behavior Don't fail

Desktop (please complete the following information):

Additional context The breaking request was the following:

{
   "object": "whatsapp_business_account",
   "entry": [
      {
         "id": "",
         "changes": [
            {
               "value": {
                  "messaging_product": "whatsapp",
                  "metadata": {
                     "display_phone_number": "bot",
                     "phone_number_id": "bot"
                  },
                  "messages": [
                     {
                        "from": "user-87",
                        "id": "wamid",
                        "timestamp": "1689861876",
                        "system": {
                           "body": "User A changed from user-87 to user-67",
                           "wa_id": "user-67",
                           "type": "user_changed_number"
                        },
                        "type": "system"
                     }
                  ]
               },
               "field": "messages"
            }
         ]
      }
   ]
}
Secreto31126 commented 11 months ago

The following line fails because contacts is not defined:

const contact = value.contacts[0];

Weird. I will consider changing it to value.messages[0].from, that is, if it's always available. I will research more on it later.