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_replyinstead).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,forwardedwill beTrueas 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
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, **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_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, 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,ORDERandSYSTEM.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/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,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).
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
reactionand noreply_to_message_idis 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.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,CallbackSelectionandCallPermissionUpdate.BUTTON β Only used in
CallbackButton.REQUEST_WELCOME β Only used in
ChatOpened.SYSTEM β Only used in
PhoneNumberChangeandIdentityChange