Media#

class pywa.types.Image#

Represents a received image.

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

  • sha256 – The SHA256 hash of the image.

  • mime_type – The MIME type of the image.

class pywa.types.Video#

Represents a video.

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

  • sha256 – The SHA256 hash of the video.

  • mime_type – The MIME type of the video.

class pywa.types.Audio#

Represents an audio.

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

  • sha256 – The SHA256 hash of the audio.

  • mime_type – The MIME type of the audio.

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

class pywa.types.Document#

Represents a document.

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

  • sha256 – The SHA256 hash of the document.

  • mime_type – The MIME type of the document.

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

class pywa.types.Sticker#

Represents a sticker.

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

  • sha256 – The SHA256 hash of the sticker.

  • mime_type – The MIME type of the sticker.

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


Every media type has the following properties and methods:

class pywa.types.media.Media#

Base class for all media types.

get_media_url() str#

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

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.

delete(*, phone_id: str | int | None = <object object>) SuccessResult#

Deletes the media from WhatsApp servers.

Parameters:

phone_id – 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).

property BaseUserMedia.extension: str | None#

Gets the extension of the media (with dot.)

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

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.MediaUrlResponse#

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.

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.