Media#
Every media type has the following properties and methods:
- class pywa.types.media.Media#
Base class for all media types.
- download(*, path=None, filename=None, in_memory=False, **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(...)
- 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_memoryis False, the file as bytes otherwise.- 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.BaseUserMedia#
Bases:
Media,FromDictBase class for all user media types (Image, Video, Sticker, Document, Audio).
- 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: