diff --git a/examples/standalone_hook.py b/examples/standalone_hook.py index 4a9f470..ddedb53 100644 --- a/examples/standalone_hook.py +++ b/examples/standalone_hook.py @@ -85,6 +85,17 @@ def hook(): image_filename = messenger.download_media(image_url, mime_type) logging.info(f"{mobile} sent image {image_filename}") + elif message_type == "sticker": + sticker = msg.sticker + if sticker is None: + return Response(status=400) + sticker_id, mime_type = sticker["id"], sticker["mime_type"] + sticker_url = messenger.query_media_url(sticker_id) + if sticker_url is None: + return Response(status=400) + sticker_filename = messenger.download_media(sticker_url, mime_type) + logging.info(f"{mobile} sent sticker {sticker_filename}") + elif message_type == "video": video = msg.video if video is None: diff --git a/whatsapp/__init__.py b/whatsapp/__init__.py index 3496599..d6cb4ca 100644 --- a/whatsapp/__init__.py +++ b/whatsapp/__init__.py @@ -14,7 +14,7 @@ from .ext._send_media import send_image, send_video, send_audio, send_location, send_sticker, send_document from .ext._media import upload_media, query_media_url, download_media, delete_media from .ext._buttons import send_button, create_button, send_reply_button -from .ext._static import is_message, get_mobile, get_author, get_name, get_message, get_message_id, get_message_type, get_message_timestamp, get_audio, get_delivery, get_document, get_image, get_interactive_response, get_location, get_video, changed_field +from .ext._static import is_message, get_mobile, get_author, get_name, get_message, get_message_id, get_message_type, get_message_timestamp, get_audio, get_delivery, get_document, get_image, get_sticker, get_interactive_response, get_location, get_video, changed_field import json @@ -136,6 +136,7 @@ async def hook(r: Request): get_delivery = staticmethod(get_delivery) get_document = staticmethod(get_document) get_image = staticmethod(get_image) + get_sticker = staticmethod(get_sticker) get_interactive_response = staticmethod(get_interactive_response) get_location = staticmethod(get_location) get_video = staticmethod(get_video) @@ -240,6 +241,11 @@ def __init__(self, id: int = None, data: dict = {}, instance: WhatsApp = None, c self.image = self.instance.get_image(data) except: self.image = None + if self.type == "sticker": + try: + self.sticker = self.instance.get_sticker(data) + except: + self.sticker = None elif self.type == "video": try: self.video = self.instance.get_video(data) diff --git a/whatsapp/ext/_static.py b/whatsapp/ext/_static.py index 482a4df..123a439 100644 --- a/whatsapp/ext/_static.py +++ b/whatsapp/ext/_static.py @@ -180,6 +180,25 @@ def get_image(data: Dict[Any, Any]) -> Union[Dict, None]: if "image" in data["messages"][0]: return data["messages"][0]["image"] +@staticmethod +def get_sticker(data: Dict[Any, Any]) -> Union[Dict, None]: + """ " + Extracts the sticker of the sender from the data received from the webhook. + + Args: + data[dict]: The data received from the webhook + Returns: + dict: The sticker_id of an sticker sent by the sender + + Example: + >>> from whatsapp import WhatsApp + >>> whatsapp = WhatsApp(token, phone_number_id) + >>> sticker_id = whatsapp.get_sticker(data) + """ + data = data["entry"][0]["changes"][0]["value"] + if "messages" in data: + if "sticker" in data["messages"][0]: + return data["messages"][0]["sticker"] @staticmethod def get_document(data: Dict[Any, Any]) -> Union[Dict, None]: