Media#
- class pywa.types.media.Media#
Base class for all media types.
- Variables:
id (str) β The ID 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).
- 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:
- 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).
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).
- property is_expired: bool#
Checks if the media URL is expired. If expired, you need to regenerate it.
- 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:
- 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.
If the media URL is expired, it will use the media ID to reupload (Will make an extra request to get a new URL).
- Parameters:
- Return type:
- 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).