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
andMessageType.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 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_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
andSYSTEM
.If the message type is
reaction
, you must providereply_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
andimage
. also, theSectionList
is only available totext
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 noreply_to_message_id
is provided, or if the message type isunsupported
.
- 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
andCallbackSelection
.BUTTON β Only used in
CallbackButton
.REQUEST_WELCOME β Only used in
ChatOpened
.