Skip to content

Commit

Permalink
added query_media_url and download_media method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalebu committed Aug 14, 2022
1 parent 5c76172 commit 6ca2d56
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions heyoo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,54 @@ def send_reply_button(self, button, recipient_id):
r = requests.post(self.url, headers=self.headers, json=data)
return r.json()

def query_media_url(self, media_id):
"""
Query media url from media id obtained either by manually uploading media or received media
Args:
media_id[str]: Media id of the media
Returns:
str: Media url
Example:
>>> from whatsapp import WhatsApp
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> whatsapp.query_media_url("media_id")
"""
r = requests.get(f"{self.BASE_URL}{media_id}", headers=self.headers)
if r.status_code == 200:
return r.json()["url"]
return None

def download_media(self, media_url, mime_type):
"""
Download media from media url obtained either by manually uploading media or received media
Args:
media_url[str]: Media url of the media
mime_type[str]: Mime type of the media
Returns:
str: Media url
Example:
>>> from whatsapp import WhatsApp
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> whatsapp.download_media("media_url", "image/jpeg")
"""
r = requests.get(media_url, headers=self.headers)
content = r.content
extension = mime_type.split("/")[1]
# create a temporary file
try:
with open(f"temp.{extension}", "wb") as f:
f.write(content)
return f.name
except Exception as e:
print(e)
return None

def preprocess(self, data):
"""
Preprocesses the data received from the webhook.
Expand Down

0 comments on commit 6ca2d56

Please sign in to comment.