Message#

The Message type is used to represent incoming messages from WhatsApp user.

class pywa.types.Message#

A message received from a user.

Variables:
  • id (str) – The message ID (If you want to reply to the message, use message_id_to_reply instead).

  • metadata (Metadata) – The metadata of the message (to which phone number it was sent).

  • type (MessageType) – The message type (See MessageType).

  • from_user (User) – The user who sent the message.

  • timestamp (datetime.datetime) – The timestamp when the message was sent.

  • reply_to_message (ReplyToMessage | None) – The message to which this message is a reply to. (Optional)

  • forwarded (bool) – Whether the message was forwarded.

  • forwarded_many_times (bool) – Whether the message was forwarded many times. (when True, forwarded will be True as well)

  • text (str | None) – The text of the message (if the message type is MessageType.TEXT).

  • image (Image | None) – The image of the message (if the message type is MessageType.IMAGE).

  • video (Video | None) – The video of the message (if the message type is MessageType.VIDEO).

  • sticker (Sticker | None) – The sticker of the message (if the message type is MessageType.STICKER).

  • document (Document | None) – The document of the message (if the message type is MessageType.DOCUMENT).

  • audio (Audio | None) – The audio of the message (if the message type is MessageType.AUDIO).

  • caption (str | None) – The caption of the message (Optional, only available for MessageType.IMAGE, MessageType.VIDEO and MessageType.DOCUMENT).

  • reaction (Reaction | None) – The reaction of the message (if the message type is MessageType.REACTION).

  • location (Location | None) – The location of the message (if the message type is MessageType.LOCATION).

  • contacts (tuple[Contact, ...] | None) – The contacts of the message (if the message type is MessageType.CONTACTS).

  • order (Order | None) – The order of the message (if the message type is MessageType.ORDER).

  • system (System | None) – The system update (if the message type is MessageType.SYSTEM).

  • error (WhatsAppError | None) – The error of the message (if the message type is MessageType.UNSUPPORTED).

property has_media: bool#

Whether the message has any media. (image, video, sticker, document or audio) - If you want to get the media of the message, use media instead.

property is_reply: bool#

Whether the message is a reply to another message.

  • Reaction messages are also considered as replies (But .reply_to_message will be None).

property media: Image | Video | Sticker | Document | Audio | None#

The media of the message if any, otherwise None. (image, video, sticker, document or audio) - If you want to check whether the message has any media, use has_media instead.

download_media(filepath: str | None = None, filename: str | None = None, in_memory: bool = False, **kwargs) str | bytes#

Download a media file from WhatsApp servers (image, video, sticker, document or audio).

Parameters:
  • filepath – 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 requests.get.

Returns:

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

Raises:

ValueError – If the message does not contain any media.

copy(to: str, header: str | None = None, body: str | None = None, footer: str | None = None, buttons: Iterable[Button] | ButtonUrl | SectionList | None = None, preview_url: bool = False, reply_to_message_id: str = None, keyboard: None = None, tracker: str | None = None) str#

Send the message to another user.

  • The WhatsApp Cloud API does not offer a real forward option, so this method will send a new message with the same content as the original message.

  • Supported message types: TEXT, DOCUMENT, IMAGE, VIDEO, STICKER, LOCATION, AUDIO, CONTACTS, ORDER and SYSTEM.

  • If the message type is reaction, you must provide reply_to_message_id.

Parameters:
  • to – The phone ID of the WhatsApp user to copy the message to.

  • header – The header of the message (if keyboard is provided, optional, up to 60 characters, no markdown allowed).

  • body – The body of the message (if keyboard are provided, optional, up to 1024 characters, markdown allowed).

  • footer – The footer of the message (if keyboard is provided, optional, markdown has no effect).

  • buttons – The buttons to send with the message (only in case of message from type text, document, video and image. also, the SectionList is only available to text type)

  • reply_to_message_id – The message ID to reply to (optional).

  • preview_url – Whether to show a preview of the URL in the message (if any).

  • keyboard – Deprecated and will be removed in a future version, use buttons instead.

  • tracker – The track data of the message.

Returns:

The ID of the sent message.

Raises:

ValueError – If the message type is reaction and no reply_to_message_id is provided, or if the message type is unsupported.


class pywa.types.MessageType#

Message types.

Variables:
  • TEXT – Message.text -> str.

  • IMAGE – Message.image -> Image.

  • VIDEO – Message.video -> Video.

  • DOCUMENT – Message.document -> Document.

  • AUDIO – Message.audio -> Audio.

  • STICKER – Message.sticker -> Sticker.

  • REACTION – Message.reaction -> Reaction.

  • LOCATION – Message.location -> Location.

  • CONTACTS – Message.contacts -> tuple[Contact].

  • ORDER – Message.order -> Order.

  • SYSTEM – Message.system -> System.

  • UNKNOWN – An unknown message (Warning with the actual type will be logged).

  • UNSUPPORTED – An unsupported message (message type not supported by WhatsApp Cloud API).

  • INTERACTIVE – Only used in CallbackButton and CallbackSelection.

  • BUTTON – Only used in CallbackButton.

  • REQUEST_WELCOME – Only used in ChatOpened.