Media#
- class pywa.types.media.Media#
Base class for all media types.
- Variables:
id (str) β The ID of the media.
filename (str | None) β The filename of the media.
uploaded_by (UploadedBy) β Who uploaded the media (business or user).
uploaded_at (datetime.datetime) β The timestamp when the media was uploaded (in UTC).
uploaded_to (str) β The phone ID the media was uploaded to.
- property is_expired: bool#
Checks if the media is expired (30 days for business uploaded media, 7 days for user uploaded media).
- property expires_at: datetime#
Gets the expiration date of the media (30 days for business uploaded media, 7 days for user uploaded media).
- download(*, path=None, filename=None, in_memory=None, chunk_size=None, **httpx_kwargs)#
Download a media file from WhatsApp servers.
Same as
download_media()withmedia_url=media.get_media_url()
>>> from pywa import WhatsApp, types, filters >>> wa = WhatsApp(...)
>>> @wa.on_message(filters.image) ... def on_message(_: WhatsApp, msg: types.Message): ... msg.image.download(path=pathlib.Path('/path/to/save'), filename='my_image.jpg')
- Parameters:
path (str | Path | None) β The path where to save the file (if not provided, the current working directory will be used).
filename (str | None) β The name of the file to save (if not provided, it will be extracted from the
Content-Dispositionheader or a SHA256 hash of the URL will be used).chunk_size (int | None) β The size (in bytes) of each chunk to read when downloading the media (default:
64KB).in_memory (None) β Deprecated: Use
get_media_bytes()orstream_media()instead. If True, the file will be returned as bytes instead of being saved to disk.**httpx_kwargs β Additional arguments to pass to
httpx.get(...).
- Returns:
The path of the saved file.
- Return type:
- stream(chunk_size=None, **httpx_kwargs)#
Stream the media file as bytes.
Same as
stream_media()withmedia_url=media.get_media_url()
>>> from pywa import WhatsApp, types, filters
>>> wa = WhatsApp(...)
>>> @wa.on_message(filters.document) ... def on_message(_: WhatsApp, msg: types.Message): ... with httpx.Client() as client: ... client.post('http://example.com/upload', content=msg.document.stream())
- get_bytes(**httpx_kwargs)#
Get the media file as bytes.
Same as
get_media_bytes()withmedia_url=media.get_media_url()
>>> from pywa import WhatsApp, types, filters >>> wa = WhatsApp(...)
>>> @wa.on_message(filters.document) ... def on_message(_: WhatsApp, msg: types.Message): ... doc_bytes = msg.document.get_bytes()
- Parameters:
**httpx_kwargs β Additional arguments to pass to
httpx.get(...).- Returns:
The media file as bytes.
- Return type:
- delete(*, phone_id=<object object>)#
Deletes the media from WhatsApp servers.
- reupload(*, to_phone_id=None, override_filename=None)#
Reuploads the media to WhatsApp servers.
Useful for re-sending media from another business phone number or if you want to use the media more than 30 days after it was uploaded.
- Parameters:
- Return type:
- class pywa.types.media.ArrivedMedia#
Bases:
MediaBase class for all media types that can be received in a message.
- Variables:
id (str) β The ID of the file (can be used to download or re-send the media later, but only for 7 days after it was uploaded by the user).
filename (str | None) β The filename of the media (only for documents or when arrived via flow completion).
sha256 (str) β The SHA256 hash of the media.
mime_type (str) β The MIME type of the media.
url (str | None) β The URL of the media (may be None in webhook versions before 24.0, use
get_media_url()to get it).uploaded_by (UploadedBy) β Who uploaded the media (always USER for arrived media).
uploaded_at (datetime.datetime) β The timestamp when the message containing the media was received (in UTC).
uploaded_to (str | None) β The phone ID the media was received to (optional when constructing manually).
- classmethod from_flow_completion(client, media)#
Create a media object from the media dict returned by the flow completion.
You can use the shortcut
get_media()
Example
>>> from pywa import WhatsApp, types >>> wa = WhatsApp(...) >>> @wa.on_flow_completion ... def on_flow_completion(_: WhatsApp, flow: types.FlowCompletion): ... img = types.Image.from_flow_completion(client=wa, media=flow.response['media']) ... img.download()
- Parameters:
- Returns:
The media object (Image, Video, Sticker, Document, Audio).
- Return type:
- class pywa.types.media.MediaURL#
Represents a media response.
The URL is valid for 5 minutes.
- Variables:
- property expires_at: datetime#
Gets the expiration date of the media URL (5 minutes after creation).
- class pywa.types.media.UploadedBy#
Enum representing who uploaded the media.
- Variables:
BUSINESS β The media was uploaded by the business (available for 30 days).
USER β The media was uploaded by a user (available for 7 days).