Media#

class pywa.types.media.Image#

Represents an received image.

Variables:
  • id (str) – The ID of the file (can be used to download or re-send the image).

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

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

class pywa.types.media.Video#

Represents a video.

Variables:
  • id (str) – The ID of the file (can be used to download or re-send the video).

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

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

class pywa.types.media.Audio#

Represents an audio.

Variables:
  • id (str) – The ID of the file (can be used to download or re-send the audio).

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

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

  • voice (bool) – Whether the audio is a voice message or just an audio file.

class pywa.types.media.Document#

Represents a document.

Variables:
  • id (str) – The ID of the file (can be used to download or re-send the document).

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

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

  • filename (str | None) – The filename of the document (optional).

class pywa.types.media.Sticker#

Represents a sticker.

Variables:
  • id (str) – The ID of the file (can be used to download or re-send the sticker).

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

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

  • animated (bool) – Whether the sticker is animated.


Every media type has the following properties and methods:

property BaseMedia.extension: str | None#

Gets the extension of the media (with dot.)

BaseMedia.get_media_url() str#

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

BaseMedia.download(path: str | None = None, filename: str | None = None, in_memory: bool = False, **kwargs) bytes | str#
Download a media file from WhatsApp servers.
>>> message.image.download()
Parameters:
  • path – The path where to save the file (if not provided, the current working directory will be used).

  • filename – The name of the file (if not provided, it will be guessed from the URL + extension).

  • in_memory – 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.

classmethod BaseMedia.from_flow_completion(client: WhatsApp, media: dict[str, str]) BaseMedia#

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 – The WhatsApp client.

  • media – The media dict returned by the flow completion.

Returns:

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


class pywa.types.media.MediaUrlResponse#

Represents a media response.

Variables:
  • id (str) – 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.