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

feat: handle sticker type for received messages #36

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/standalone_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion whatsapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions whatsapp/ext/_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
Loading