Secreto31126 / whatsapp-api-js

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

Handlers.post() fails if requests doesn't include data.entry[0].changes[0].value.contacts or data.entry[0].changes[0].value.messages #4

Closed Secreto31126 closed 2 years ago

Secreto31126 commented 2 years ago

Don't look at me 🤷‍♂️. One would expect that, if you only subscribe to messages updates, you would only get messages objects. Seems like I was wrong...

I got this request after sending a message:

{
    "object": "whatsapp_business_account",
    "entry": [
        {
            "id": "",
            "changes": [
                {
                    "value": {
                        "messaging_product": "whatsapp",
                        "metadata": {
                            "display_phone_number": "",
                            "phone_number_id": ""
                        },
                        "statuses": [
                            {
                                "id": "",
                                "status": "sent",
                                "timestamp": "1651616208",
                                "recipient_id": "",
                                "conversation": {
                                    "id": "",
                                    "expiration_timestamp": "1651697640",
                                    "origin": {
                                        "type": "user_initiated"
                                    }
                                },
                                "pricing": {
                                    "billable": true,
                                    "pricing_model": "CBP",
                                    "category": "user_initiated"
                                }
                            }
                        ]
                    },
                    "field": "messages"
                }
            ]
        }
    ]
}

Clearly this is a "sent message" status update. They should be separated in the API subscriptions, but seems like not. A statuses check would be better than throwing an error in the Handler. Maybe the handler could also take a second callback for statuses.

Secreto31126 commented 2 years ago

Also, since a request might not include the contacts property, the user phone number should be the one in the metadata which is always present.