diff --git a/tests/data/18.0/callback_button.json b/tests/data/18.0/callback_button.json new file mode 100644 index 00000000..cbc44eef --- /dev/null +++ b/tests/data/18.0/callback_button.json @@ -0,0 +1,94 @@ +{ + "button": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "5467539754836534", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "from": "1122334455667", + "id": "wamid.xyzxyz" + }, + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1698266730", + "type": "interactive", + "interactive": { + "type": "button_reply", + "button_reply": { + "id": "callback_data", + "title": "title" + } + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "quick_reply": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "5467539754836534", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "from": "972123456789", + "id": "wamid.xyzxyz==" + }, + "from": "972987654321", + "id": "wamid.xsidjw", + "timestamp": "1698269027", + "type": "button", + "button": { + "payload": "callback_data", + "text": "title" + } + } + ] + }, + "field": "messages" + } + ] + } + ] + } + +} diff --git a/tests/data/18.0/callback_selection.json b/tests/data/18.0/callback_selection.json new file mode 100644 index 00000000..e8df5dd3 --- /dev/null +++ b/tests/data/18.0/callback_selection.json @@ -0,0 +1,97 @@ +{ + "callback": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "from": "1122334455667", + "id": "wamid.xyzxyz" + }, + "from": "972987654321", + "id": "wamid.zyzxyz", + "timestamp": "1698266905", + "type": "interactive", + "interactive": { + "type": "list_reply", + "list_reply": { + "id": "callback_data", + "title": "title" + } + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "description": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "from": "1122334455667", + "id": "wamid.xyzxyz" + }, + "from": "972987654321", + "id": "wamid.zyzxyz", + "timestamp": "1698266905", + "type": "interactive", + "interactive": { + "type": "list_reply", + "list_reply": { + "id": "callback_data", + "title": "title", + "description": "description" + } + } + } + ] + }, + "field": "messages" + } + ] + } + ] + } +} diff --git a/tests/data/18.0/message.json b/tests/data/18.0/message.json new file mode 100644 index 00000000..9670226b --- /dev/null +++ b/tests/data/18.0/message.json @@ -0,0 +1,735 @@ +{ + "text": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697043223", + "text": { + "body": "Body Text" + }, + "type": "text" + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "image": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697043379", + "type": "image", + "image": { + "mime_type": "image/jpeg", + "sha256": "4654+8g=", + "id": "65463453" + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "video": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "forwarded": true + }, + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044362", + "type": "video", + "video": { + "caption": "caption", + "mime_type": "video/mp4", + "sha256": "Jh+2ij6PzUv/J2y+grrtegr/ZMzRxkEPwc24Oyib4s03w9Jc=", + "id": "765756756" + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "document": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "forwarded": true + }, + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697043666", + "type": "document", + "document": { + "caption": "caption", + "filename": "filename.pdf", + "mime_type": "application/pdf", + "sha256": "grwfwe/ZPx0wAbdfbdeFUNItPZ0RylL8gGUvdt3YFqc18SY=", + "id": "1234567890987654321" + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "audio": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "forwarded": true + }, + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697043509", + "type": "audio", + "audio": { + "mime_type": "audio/ogg; codecs=opus", + "sha256": "E3dxS/PdYZE7ppA3pQ4mpCFaXBJ8pX4SRN/dsfvds/iPGA=", + "id": "1234567890987654321", + "voice": false + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "voice": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697043473", + "type": "audio", + "audio": { + "mime_type": "audio/ogg; codecs=opus", + "sha256": "m04pFf2ERdaopTyFkGPA0+bX3i+YY2dYgVFwOgDLSDI=", + "id": "1234567890987654321", + "voice": true + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "static_sticker": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697043760", + "type": "sticker", + "sticker": { + "mime_type": "image/webp", + "sha256": "OmT4nhjUhCMIPNmj7eeOZeCYjTXkLxoOv85OMCFwSuc=", + "id": "1234567890987654321", + "animated": false + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "animated_sticker": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697043785", + "type": "sticker", + "sticker": { + "mime_type": "image/webp", + "sha256": "HcwyCKrTKXHk0a00ms4HWIA76GY0Du6q4Z/iuwevw=", + "id": "1234567890987654321", + "animated": true + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "reaction": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044456", + "type": "reaction", + "reaction": { + "message_id": "wamid.yzxyzx=", + "emoji": "\ud83d\ude2e" + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "location": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "forwarded": true + }, + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044557", + "location": { + "latitude": 12.25089, + "longitude": 43.90539 + }, + "type": "location" + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "contacts": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044621", + "type": "contacts", + "contacts": [ + { + "name": { + "first_name": "Chandler", + "last_name": "Bing", + "formatted_name": "Chandler Bing" + }, + "phones": [ + { + "phone": "+1 555-555-5555", + "wa_id": "972987654321", + "type": "CELL" + } + ] + }, + { + "name": { + "first_name": "Monica", + "formatted_name": "Monica" + }, + "phones": [ + { + "phone": "+1 666-666-6666", + "wa_id": "972987654321", + "type": "HOME" + } + ] + }, + { + "name": { + "first_name": "Rachel", + "last_name": "Green", + "formatted_name": "Rachel Green" + }, + "phones": [ + { + "phone": "+1 777-777-7777", + "wa_id": "972987654321", + "type": "WORK" + } + ] + } + ] + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "order": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044668", + "type": "order", + "order": { + "catalog_id": "354160437058781", + "text": "", + "product_items": [ + { + "product_retailer_id": "HAMBURGER", + "quantity": 3, + "item_price": 30, + "currency": "ILS" + }, + { + "product_retailer_id": "FRENCH_FRIES", + "quantity": 2, + "item_price": 35, + "currency": "ILS" + }, + { + "product_retailer_id": "SODA", + "quantity": 2, + "item_price": 10, + "currency": "ILS" + } + ] + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "unsupported": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044706", + "errors": [ + { + "code": 131051, + "title": "Message type unknown", + "message": "Message type unknown", + "error_data": { + "details": "Message type is currently not supported." + } + } + ], + "type": "unsupported" + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "reply": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044746", + "text": { + "body": "replied text" + }, + "type": "text" + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "forwarded": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "forwarded": true + }, + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044815", + "text": { + "body": "forwarded text" + }, + "type": "text" + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "forwarded_many_times": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "1234567890987654321", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "contacts": [ + { + "profile": { + "name": "Test Name" + }, + "wa_id": "972987654321" + } + ], + "messages": [ + { + "context": { + "frequently_forwarded": true + }, + "from": "972987654321", + "id": "wamid.xyzxyz", + "timestamp": "1697044968", + "text": { + "body": "text forwarded many times" + }, + "type": "text" + } + ] + }, + "field": "messages" + } + ] + } + ] + } +} diff --git a/tests/data/18.0/message_status.json b/tests/data/18.0/message_status.json new file mode 100644 index 00000000..636fd314 --- /dev/null +++ b/tests/data/18.0/message_status.json @@ -0,0 +1,81 @@ +{ + "sent": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "5467539754836534", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "statuses": [ + { + "id": "wamid.xyzxyz", + "status": "sent", + "timestamp": "1698266945", + "recipient_id": "972987654321", + "conversation": { + "id": "7270432eae37498234f4444b35ffc5", + "expiration_timestamp": "1698339900", + "origin": { + "type": "service" + } + }, + "pricing": { + "billable": true, + "pricing_model": "CBP", + "category": "service" + } + } + ] + }, + "field": "messages" + } + ] + } + ] + }, + "failed": { + "object": "whatsapp_business_account", + "entry": [ + { + "id": "5467539754836534", + "changes": [ + { + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "972123456789", + "phone_number_id": "1122334455667" + }, + "statuses": [ + { + "id": "wamid.xyzxyz", + "status": "failed", + "timestamp": "1689380458", + "recipient_id": "972987654321", + "errors": [ + { + "code": 130472, + "title": "User's number is part of an experiment", + "message": "User's number is part of an experiment", + "error_data": { + "details": "Failed to send message because this user's phone number is part of an experiment" + }, + "href": "https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/" + } + ] + } + ] + }, + "field": "messages" + } + ] + } + ] + } +} diff --git a/tests/data/18.0/template_status.json b/tests/data/18.0/template_status.json new file mode 100644 index 00000000..e7ad7e4a --- /dev/null +++ b/tests/data/18.0/template_status.json @@ -0,0 +1,23 @@ +{ + "approved": { + "entry": [ + { + "id": "0", + "time": 1698268000, + "changes": [ + { + "field": "message_template_status_update", + "value": { + "event": "APPROVED", + "message_template_id": 12345678, + "message_template_name": "my_message_template", + "message_template_language": "pt-BR", + "reason": null + } + } + ] + } + ], + "object": "whatsapp_business_account" + } +} diff --git a/tests/test_updates.py b/tests/test_updates.py new file mode 100644 index 00000000..146befc4 --- /dev/null +++ b/tests/test_updates.py @@ -0,0 +1,66 @@ +import json +from typing import Any + +from pywa import WhatsApp +from pywa.types import ( + Message, + CallbackButton, + CallbackSelection, + MessageStatus, + TemplateStatus, +) +from pywa.types.base_update import BaseUpdate + +API_VERSIONS: list[float] = [18.0] + +UPDATES: dict[tuple[str, type[BaseUpdate]], list[str]] = { + ("message", Message): [ + "text", + "image", + "video", + "document", + "audio", + "voice", + "static_sticker", + "animated_sticker", + "location", + "contacts", + "order", + "unsupported", + "reply", + "forwarded", + "forwarded_many_times", + ], + ("callback_button", CallbackButton): [ + "button", + "quick_reply", + ], + ("callback_selection", CallbackSelection): [ + "callback", + "description", + ], + ("message_status", MessageStatus): [ + "sent", + "failed", + ], + ("template_status", TemplateStatus): [ + "approved", + ], +} + + +def test_types(): + client = WhatsApp(phone_id="1234567890", token="xyzxyzxyz") + for version in API_VERSIONS: + for (update_id, update_type), updates in UPDATES.items(): + with open(f"tests/data/{version}/{update_id}.json", "r") as update_file: + update_data: dict[str, Any] = json.load(update_file) + for update in updates: + try: + update = update_type.from_update( + client=client, update=update_data[update] + ) # noqa + except Exception as e: + raise AssertionError( + f"Failed to parse update '{update_id}': test='{update}', v={version}, error={e}" + )