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 – The message ID (If you want to reply to the message, use message_id_to_reply instead).

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

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

  • from_user – The user who sent the message.

  • timestamp – The timestamp when the message was arrived to WhatsApp servers (in UTC).

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

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

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

  • text (str | None) – The text of the message.

  • image (Image | None) – The image of the message.

  • video (Video | None) – The video of the message.

  • sticker (Sticker | None) – The sticker of the message.

  • document (Document | None) – The document of the message.

  • audio (Audio | None) – The audio of the message.

  • caption (str | None) – The caption of the message (Optional, only available for image video and document messages).

  • reaction (Reaction | None) – The reaction of the message.

  • location (Location | None) – The location of the message.

  • contacts (tuple[Contact, ...] | None) – The contacts of the message.

  • order (Order | None) – The order of the message.

  • referral (Referral | None) – The referral information of the message (When a customer clicks an ad that redirects to WhatsApp).

  • error (WhatsAppError | None) – The error of the message.

  • shared_data – Shared data between handlers.

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 httpx.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] | URLButton | VoiceCallButton | FlowButton | SectionList | None = None, preview_url: bool = False, reply_to_message_id: str = None, tracker: str | None = None, sender: str | int | None = None) SentMessage#

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/caption of the message (if buttons are provided, optional, up to 1024 characters, markdown allowed).

  • footer – The footer of the message (if buttons 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).

  • tracker – The track data of the message.

  • sender – The phone ID to send the message from (optional, overrides the client’s phone ID).

Returns:

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.

  • 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, CallbackSelection and CallPermissionUpdate.

  • BUTTON – Only used in CallbackButton.

  • REQUEST_WELCOME – Only used in ChatOpened.

  • SYSTEM – Only used in PhoneNumberChange and IdentityChange