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.
chat (Chat) β The chat where the message was sent (private or group).
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.
voice β The voice note of the message (shorthand for
audioif itβs a voice note).caption β The caption of the message media (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 (ContactList | 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).
unsupported (Unsupported | None) β The unsupported content of the message.
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, use
has_mediainstead.
- download_media(filepath=None, filename=None, *, chunk_size=None, **httpx_kwargs)#
Download the media of the message to the specified path.
Shortcut for
message.media.download(...),message.image.download(...)etc.Use
get_media_bytes()if you want to get the file as bytes instead of saving it to disk.
>>> from pywa import WhatsApp, types, filters >>> wa = WhatsApp(...)
>>> @wa.on_message(filters.image) ... def on_message(_: WhatsApp, msg: types.Message): ... msg.download_media(path=pathlib.Path('/path/to/save'), filename='my_image.jpg')
- Parameters:
filepath (str | None) β The path where to save the file (if not provided, the current working directory will be used).
filename (str | None) β The name of the file to save (if not provided, it will be extracted from the
Content-Dispositionheader or a SHA256 hash of the URL will be used).chunk_size (int | None) β The size (in bytes) of each chunk to read when downloading the media (default:
64KB).**httpx_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.
- Return type:
- copy(to, header=None, body=None, footer=None, buttons=None, preview_url=False, reply_to_message_id=None, tracker=None, sender=None)#
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 (str) β The phone ID of the WhatsApp user to copy the message to.
header (str | None) β The header of the message (if keyboard is provided, optional, up to 60 characters, no markdown allowed).
body (str | None) β The body/caption of the message (if buttons are provided, optional, up to 1024 characters, markdown allowed).
footer (str | None) β The footer of the message (if buttons is provided, optional, markdown has no effect).
buttons (Iterable[Button] | URLButton | VoiceCallButton | FlowButton | SectionList | None) β 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 (str) β The message ID to reply to (optional).
preview_url (bool) β Whether to show a preview of the URL in the message (if any).
tracker (str | None) β The track data of the message.
sender (str | int | None) β 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.- Return type:
- 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.SYSTEM β Only used in
PhoneNumberChangeandIdentityChange
- class pywa.types.User#
Represents a WhatsApp user.
- Variables:
bsuid β The WhatsApp userβs BSUID. See developers.facebook.com for more information.
wa_id β
The userβs phone number in international format (without the β+β sign). Will be unavailable if the user enables the username feature. See developers.facebook.com for more information.
name (str) β The name of the user.
username β The username of the user.
identity_key_hash (str | None) β The identity key hash of the user (Only if identity key check is enabled on the phone number settings).
parent_bsuid β
The Parent business-scoped user ID. See developers.facebook.com for more information.
- block()#
Block the user.
Shortcut for
block_users()with the user.
- Returns:
True if the user was blocked
- Return type:
- Raises:
BlockUserError β If the user was not blocked
- unblock()#
Unblock the user.
Shortcut for
unblock_users()with the user.
- Returns:
True if the user was unblocked, False otherwise.
- Return type:
- class pywa.types.Image#
Bases:
ArrivedMediaRepresents a received image.
- Variables:
id β The ID of the file (can be used to download or re-send the image).
sha256 β The SHA256 hash of the image.
mime_type β The MIME type of the image.
url β The URL of the image (Use
get_bytes()/stream()/download()to access the image file).caption (str | None) β The caption of the image.
uploaded_by β Who uploaded the image.
uploaded_at β The timestamp when the image was uploaded (in UTC).
uploaded_to β The phone ID the image was uploaded to (optional).
- class pywa.types.Video#
Bases:
ArrivedMediaRepresents a video.
- Variables:
id β The ID of the file (can be used to download or re-send the video).
sha256 β The SHA256 hash of the video.
mime_type β The MIME type of the video.
url β The URL of the video (Use
get_bytes()/stream()/download()to access the video file).caption (str | None) β The caption of the video.
uploaded_by β Who uploaded the video.
uploaded_at β The timestamp when the video was uploaded (in UTC).
uploaded_to β The phone ID the video was uploaded to (optional).
- class pywa.types.Audio#
Bases:
ArrivedMediaRepresents an audio.
- Variables:
id β The ID of the file (can be used to download or re-send the audio).
sha256 β The SHA256 hash of the audio.
mime_type β The MIME type of the audio.
voice (bool) β Whether the audio is a voice message or just an audio file.
url β The URL of the audio (Use
get_bytes()/stream()/download()to access the audio file).uploaded_by β Who uploaded the audio.
uploaded_at β The timestamp when the audio was uploaded (in UTC).
uploaded_to β The phone ID the audio was uploaded to (optional).
- class pywa.types.Document#
Bases:
ArrivedMediaRepresents a document.
- Variables:
id β The ID of the file (can be used to download or re-send the document).
sha256 β The SHA256 hash of the document.
mime_type β The MIME type of the document.
filename β The filename of the document (optional).
url β The URL of the document (Use
get_bytes()/stream()/download()to access the document file).caption (str | None) β The caption of the document.
uploaded_by β Who uploaded the document.
uploaded_at β The timestamp when the document was uploaded (in UTC).
uploaded_to β The phone ID the document was uploaded to (optional).
- class pywa.types.Sticker#
Bases:
ArrivedMediaRepresents a sticker.
- Variables:
id β The ID of the file (can be used to download or re-send the sticker).
sha256 β The SHA256 hash of the sticker.
mime_type β The MIME type of the sticker.
animated (bool) β Whether the sticker is animated.
url β The URL of the sticker (Use
get_bytes()/stream()/download()to access the sticker file).uploaded_by β Who uploaded the sticker.
uploaded_at β The timestamp when the sticker was uploaded (in UTC).
uploaded_to β The phone ID the sticker was uploaded to (optional).
- class pywa.types.Reaction#
Represents a reaction to a message.
- Variables:
- class pywa.types.Location#
Represents a location.
- Variables:
- class pywa.types.others.ContactList#
-
Represents an shared contacts in a message, which can be iterated over to get the individual contacts.
- Variables:
origin (pywa.types.others.ContactsOrigin) β The origin of the shared contacts (e.g.
contact_requestif the contacts were shared as a contact request,otherotherwise).
- class pywa.types.others.ContactsOrigin#
Represents an origin of a contact.
- Variables:
CONTACT_REQUEST β The contacts were shared as a contact request.
OTHER β The contacts were shared in another way.
- class pywa.types.Contact#
Represents a contact.
- Variables:
name (Name) β The name of the contact.
birthday (str | None) β The birthday of the contact (in
YYYY-MM-DDformat, optional).phones (Iterable[Phone]) β The phone numbers of the contact.
emails (Iterable[Email]) β The email addresses of the contact.
urls (Iterable[Url]) β The URLs of the contact.
addresses (Iterable[Address]) β The addresses of the contact.
org (Org | None) β The organization of the contact (optional).
- class Name(formatted_name, first_name=None, last_name=None, middle_name=None, suffix=None, prefix=None)#
Represents a contactβs name.
At least one of the optional parameters needs to be included along with the formatted_name parameter.
- Variables:
formatted_name (str) β The formatted name of the contact.
first_name (str | None) β The first name of the contact (optional).
last_name (str | None) β The last name of the contact (optional).
middle_name (str | None) β The middle name of the contact (optional).
suffix (str | None) β The suffix of the contact (optional).
prefix (str | None) β The prefix of the contact (optional).
- class Phone(phone=None, type=None, wa_id=None)#
Represents a contactβs phone number.
- class Email(email=None, type=None)#
Represents a contactβs email address.
- class Url(url=None, type=None)#
Represents a contactβs URL.
- class Org(company=None, department=None, title=None)#
Represents a contactβs organization.
- class Address(street=None, city=None, state=None, zip=None, country=None, country_code=None, type=None)#
Represents a contactβs address.
- Variables:
street (str | None) β The street number and name of the address (optional).
city (str | None) β The city name of the address (optional).
state (str | None) β State abbreviation.
zip (str | None) β Zip code of the address (optional).
country (str | None) β Full country name.
country_code (str | None) β Two-letter country abbreviation (e.g. US, GB, IN. optional).
type (str | None) β The type of the address (Standard Values are WORK and HOME. optional).
- class pywa.types.Product#
Represents a product in an order.
- Variables:
- class pywa.types.Order#
Represents an order.
- Variables:
catalog_id (str) β The ID for the catalog the ordered item belongs to.
products (tuple[pywa.types.others.Product, ...]) β The ordered products.
text (str | None) β Text message from the user sent along with the order (optional).
- Properties:
total_price: Total price of the order.
- class pywa.types.Referral#
Represents a referral object in a message.
This object is included in the messages object when a customer clicks an ad that redirects to WhatsApp.
- Variables:
source_url (str | None) β The Meta URL that leads to the ad or post clicked by the customer.
source_type (str | None) β The type of the adβs source;
adorpost.source_id (str | None) β Meta ID for an ad or a post.
headline (str | None) β Headline used in the ad or post.
body (str | None) β Body for the ad or post.
media_type (str | None) β Media present in the ad or post;
imageorvideo.image_url (str | None) β URL of the image, when
media_typeis animage.video_url (str | None) β URL of the video, when
media_typeis avideo.thumbnail_url (str | None) β URL for the thumbnail, when media_type is a video.
ctwa_clid (str | None) β Click ID generated by Meta for ads that click to WhatsApp.
- class pywa.types.Unsupported#
Represents an unsupported message.
- Variables:
type (Literal['edit', 'poll', 'button']) β Contains the type of message that is unsupported (e.g., βeditβ, βpollβ, βbuttonβ).
- class pywa.types.Metadata#
Represents the metadata of a message.
- class pywa.types.ReplyToMessage#
Represents a message that was replied to.
- Variables:
id (str) β The ID of the message that was replied to.
from_user_id (str | None) β The ID of the user who sent the message that was replied to.
referred_product (pywa.types.others.ReferredProduct | None) β Referred product describing the product the user is requesting information about.