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_replyinstead).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) – 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,
forwardedwill 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.VIDEOandMessageType.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
mediainstead.
- property is_reply: bool#
Whether the message is a reply to another message.
Reaction messages are also considered as replies (But
.reply_to_messagewill beNone).
- 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, usehas_mediainstead.
- download_media(filepath: str | None = None, filename: str | None = None, in_memory: bool = False) 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).
- Returns:
The path of the saved file if
in_memoryis 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, keyboard: Iterable[Button] | SectionList | None = None, preview_url: bool = False, reply_to_message_id: str = None) str#
- Copy incoming message to another chat
The WhatsApp Cloud API does not offer a real forward option so this is just copy the message content.
- 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).
keyboard – The buttons to send with the message (only in case of message from type
text,document,videoandimage. also, theSectionListis only available totexttype)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).
- Returns:
The ID of the sent message.
- Raises:
ValueError – If the message type is
reactionand noreply_to_message_idis provided, or if the message type isunsupported.
- class pywa.types.MessageType#
Message types.
- Variables:
AUDIO – An audio message.
DOCUMENT – A document message.
TEXT – A text message.
IMAGE – An image message.
STICKER – A sticker message.
VIDEO – A video message.
REACTION – A reaction to a message.
LOCATION – A location message.
CONTACTS – A contacts message.
INTERACTIVE – An interactive message (callback).
ORDER – An order message.
SYSTEM – A system message.
UNKNOWN – An unknown message.
UNSUPPORTED – An unsupported message.