Media#

Every media type has the following properties and methods:

class pywa.types.media.Media#

Base class for all media types.

get_media_url()#

Gets the URL of the media. (expires after 5 minutes)

Return type:

str

download(*, path=None, filename=None, in_memory=False, **kwargs)#

Download a media file from WhatsApp servers.

>>> from pywa import WhatsApp, types, filters
>>> wa = WhatsApp(...)
>>> @wa.on_message(filters.image)
... def on_message(_: WhatsApp, msg: types.Message):
...     msg.image.download(...)
Parameters:
  • path (str | 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 (if not provided, it will be guessed from the URL + extension).

  • in_memory (bool) – Whether to return the file as bytes instead of saving it to disk (default: False).

  • **kwargs – Additional arguments to pass to httpx.get(...).

Returns:

The path of the saved file if in_memory is False, the file as bytes otherwise.

Return type:

Path | bytes

delete(*, phone_id=<object object>)#

Deletes the media from WhatsApp servers.

Parameters:

phone_id (str | int | None) – The phone ID to delete the media from (optional, If included, the operation will only be processed if the ID matches the ID of the business phone number that the media was uploaded on. pass None to use the client’s phone ID).

Return type:

SuccessResult

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:
  • to_phone_id (str | int | None) – The phone ID to upload the media to (if not provided, the client’s phone ID will be used).

  • override_filename (str | None) – The filename to use for the re-uploaded media (if not provided, the original filename will be used if available).

Return type:

Media

class pywa.types.media.BaseUserMedia#

Bases: Media, FromDict

Base class for all user media types (Image, Video, Sticker, Document, Audio).

property extension: str | None#

Gets the extension of the media (with dot.)

classmethod from_flow_completion(client, media)#

Create a media object from the media dict returned by the flow completion.

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:
  • client (WhatsApp) – The WhatsApp client.

  • media (dict[str, str]) – The media dict returned by the flow completion.

Returns:

The media object (Image, Video, Sticker, Document, Audio).

Return type:

BaseUserMedia


class pywa.types.MediaUrlResponse#

Bases: Media, FromDict

Represents a media response.

Variables:
  • id – The ID of the media.

  • url (str) – The URL of the media (valid for 5 minutes).

  • mime_type (str) – The MIME type of the media.

  • sha256 (str) – The SHA256 hash of the media.

  • file_size (int) – The size of the media in bytes.