API Reference
This page provides an auto-generated API reference for the key components of the WhatsPlay library.
Client
The Client is the main entry point for interacting with WhatsApp.
| Method | Returns | Notes |
|---|---|---|
start() / stop() |
None |
start() runs the main loop until stop() is called. |
wait_until_logged_in(timeout=60) |
bool |
|
refresh(timeout=180) |
bool |
Reloads WhatsApp Web to release renderer memory; emits on_refresh. |
open(chat_name, timeout=10000, open_via_url=False, is_group=None) |
bool |
True only when the open chat is verifiably the requested one. |
close() |
None |
Closes the current chat. |
search_conversations(query, close=True) |
List[Dict] |
|
collect_messages(own_push_names=None, dispose=False) |
List[Message] |
Includes FileMessage and VoiceMessage items. |
send_message(chat_query, message, open_via_url=False, is_group=None) |
bool |
True only after WhatsApp shows the new message with a delivery status. |
send_file(chat_name, path, open_via_url=False, is_group=None) |
bool |
Same confirmation as send_message. |
react_to_last_message(emoji) |
bool |
|
download_all_files(carpeta=None, detect_codecs=True) |
List[Dict] |
Each dict: path, type, and codec (when detected). |
download_file_by_index(index, carpeta=None) |
Optional[Dict] |
Dict with path and type. |
new_group(group_name, members) |
bool |
|
add_members_to_group(group_name, members) |
bool |
|
del_members_from_group(group_name, members) |
bool |
|
page_lock (property) |
ReentrantLock |
async with client.page_lock: to run several operations atomically. |
running (property) |
bool |
WhatsPlay does not define custom exceptions: failures are reported as False, [], or None, plus an on_error event and a log record.
whatsplay.client.Client
Bases: BaseWhatsAppClient
High-level WhatsApp Web client with full automation capabilities.
This client provides a complete interface for WhatsApp Web automation including authentication, chat management, message sending, and event handling.
Attributes:
| Name | Type | Description |
|---|---|---|
locale |
Locale for the browser (default: "en-US") |
|
poll_freq |
Frequency of state polling in seconds |
|
current_state |
Optional[State]
|
Current WhatsApp Web state |
unread_messages_sleep |
Sleep time between unread message checks |
|
wa_elements |
Optional[WhatsAppElements]
|
WhatsApp Web elements helper |
chat_manager |
Optional[ChatManager]
|
Chat operations manager |
state_manager |
Optional[StateManager]
|
State transition manager |
Example
client = Client(auth=LocalProfileAuth("./session")) @client.event("on_logged_in") ... async def on_ready(): ... print("Client is ready!") await client.start()
Source code in src/whatsplay/client.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
page_lock
property
Lock held by every page operation. Hold it across several calls that must not be interleaved with the background scan, e.g.::
async with client.page_lock:
if await client.open(chat):
messages = await client.collect_messages()
running
property
Check if the client is currently running.
__init__(user_data_dir=None, headless=False, locale='en-US', auth=None)
Initialize the WhatsApp Web client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_data_dir
|
Optional[str]
|
Directory for browser profile data |
None
|
headless
|
bool
|
Run browser in headless mode |
False
|
locale
|
str
|
Browser locale setting |
'en-US'
|
auth
|
Optional[Any]
|
Authentication provider instance |
None
|
Source code in src/whatsplay/client.py
add_members_to_group(group_name, members)
async
Add members to an existing group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_name
|
str
|
Name of the group |
required |
members
|
List[str]
|
List of member names to add |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if members were added successfully |
Source code in src/whatsplay/client.py
close()
async
collect_messages(own_push_names=None, dispose=False)
async
Collect all visible messages in the current chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
own_push_names
|
Optional[Union[str, list]]
|
Account push names identifying own bubbles (e.g. ["Marcos Bustos"]). Forwarded to ChatManager so own continuation bubbles without tail markers are still flagged outgoing. Compared accent/case-insensitively. |
None
|
dispose
|
bool
|
When True, release each message's container handle before returning. Pass True for data-only consumers that read text/sender/timestamp but never call react()/download(); this prevents DOM handles from accumulating in the renderer across poll cycles. |
False
|
Returns:
| Type | Description |
|---|---|
List[Union[Message, FileMessage]]
|
List of Message and FileMessage objects |
Source code in src/whatsplay/client.py
del_members_from_group(group_name, members)
async
Elimina varios miembros de un grupo procesando la lista completa.
Source code in src/whatsplay/client.py
download_all_files(carpeta=None, detect_codecs=True)
async
Download all files from the current chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carpeta
|
Optional[str]
|
Optional custom download directory |
None
|
detect_codecs
|
bool
|
If True, detect codec info for each file |
True
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of dicts with path, type, and codec info |
Source code in src/whatsplay/client.py
download_file_by_index(index, carpeta=None)
async
Download a specific file by index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Zero-based file index |
required |
carpeta
|
Optional[str]
|
Optional custom download directory |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Dict with path and codec info, or None if failed |
Source code in src/whatsplay/client.py
new_group(group_name, members)
async
Create a new group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_name
|
str
|
Name for the new group |
required |
members
|
List[str]
|
List of member names |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if group was created successfully |
Source code in src/whatsplay/client.py
open(chat_name, timeout=10000, open_via_url=False, is_group=None)
async
Open a chat by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_name
|
str
|
Name of the chat to open |
required |
timeout
|
int
|
Maximum wait time in milliseconds |
10000
|
open_via_url
|
bool
|
Force opening via URL (for phone numbers) |
False
|
is_group
|
Optional[bool]
|
If set, the chat must be a group (True) or a contact (False) |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if chat was opened successfully |
Source code in src/whatsplay/client.py
react_to_last_message(emoji)
async
React to the last visible message in the current chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
emoji
|
str
|
The emoji to react with (e.g., "👍", "❤️") |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if reaction was successful |
Source code in src/whatsplay/client.py
reconnect()
async
Reconnect and rebind every helper to the new page.
The base reconnect opens a new page; helpers created for the old one would otherwise keep driving a closed page.
Source code in src/whatsplay/client.py
refresh(timeout=180)
async
Reload WhatsApp Web to release accumulated renderer memory.
WhatsApp Web accumulates detached DOM nodes and JS heap over hours of use. Left running, the renderer reaches V8's heap ceiling and crashes with SIGTRAP ("Target crashed"). A page reload destroys the JS context and frees all of it; cookies persist, so the session survives and no QR is needed.
Intended to be called periodically by the consumer (the schedule is an operational policy, not a library concern).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
int
|
Maximum seconds to wait for the client to log back in. |
180
|
Returns:
| Type | Description |
|---|---|
bool
|
True when the client is logged in again after the reload. |
Source code in src/whatsplay/client.py
search_conversations(query, close=True)
async
Search for conversations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search term |
required |
close
|
bool
|
Close search after completion |
True
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of matching conversations |
Source code in src/whatsplay/client.py
send_file(chat_name, path, open_via_url=False, is_group=None)
async
Send a file attachment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_name
|
str
|
Name of the chat |
required |
path
|
str
|
Absolute path to the file |
required |
open_via_url
|
bool
|
Open chat via URL before sending (required for phone numbers) |
False
|
is_group
|
Optional[bool]
|
If set, the chat must be a group (True) or a contact (False) |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if file was sent successfully |
Source code in src/whatsplay/client.py
send_message(chat_query, message, open_via_url=False, is_group=None)
async
Send a text message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_query
|
str
|
Chat name or identifier |
required |
message
|
str
|
Message text to send |
required |
open_via_url
|
bool
|
Open chat via URL before sending |
False
|
is_group
|
Optional[bool]
|
If set, the chat must be a group (True) or a contact (False) |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if message was sent successfully |
Source code in src/whatsplay/client.py
start()
async
Start the WhatsApp Web client.
Initializes all components and begins the main event loop. This method will run until the client is stopped.
Raises:
| Type | Description |
|---|---|
Exception
|
If initialization or main loop fails |
Source code in src/whatsplay/client.py
stop()
async
Stop the client and clean up resources.
This method ensures all resources are properly released including browser instances, pages, and the Playwright instance.
Source code in src/whatsplay/client.py
wait_until_logged_in(timeout=DEFAULT_LOGIN_TIMEOUT)
async
Wait until the client is logged in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
int
|
Maximum time to wait in seconds |
DEFAULT_LOGIN_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
bool
|
True if logged in within timeout, False otherwise |
Source code in src/whatsplay/client.py
Events
These events are emitted by the Client and can be handled using the @client.event decorator.
| Event Name | Trigger Condition | Arguments |
|---|---|---|
on_start |
The client has initialized and the browser page is open. | None |
on_auth |
The authentication screen (QR code page) is displayed. | None |
on_qr |
A new QR code is detected for the first time. | qr_binary (bytes) |
on_qr_change |
The displayed QR code has been refreshed/changed. | qr_binary (bytes) |
on_logged_in |
The client has successfully logged in to WhatsApp Web. | None |
on_loading |
The "Loading chats" screen is visible. | is_loading (bool) |
on_unread_chat |
Unread chats are detected in the sidebar (checked periodically). | chats (List[Dict]) |
on_refresh |
client.refresh() reloaded the page. |
None |
on_stop |
The client is stopping and cleaning up resources. | None |
on_disconnect |
The client has lost connection to the browser/WhatsApp. | None |
on_reconnect |
The client has successfully reconnected. | None |
on_state_change |
The connection state changed (e.g. CONNECTING). |
state (State enum) |
on_tick |
Emitted on every iteration of the main event loop. | None |
on_error |
An error occurred. | message (str) |
on_warning |
A non-critical issue occurred (e.g. a dialog that could not be closed). | message (str) |
on_info |
Informational message. | message (str) |
The on_unread_chat payload is described in Handling Messages.
Note: The on_message event is reserved for future use. Currently, use on_unread_chat to detect new activity.
Message Objects
client.collect_messages() returns these objects.
Message
A text message. Main attributes: text, sender (str), timestamp (datetime), is_outgoing (bool), msg_id, direction_signal, sender_inherited, kind (WhatsApp's message type such as "chat", "image", "sticker", "ptt", or None when unknown). Use is_from_self(own_push_names) to check whether you sent it, react(emoji) to react, and dispose() to release its browser handles.
whatsplay.object.message.Message
Represents a WhatsApp message.
This object encapsulates all data related to a single message in a chat, including its content, sender, timestamp, and interactivity methods.
Attributes:
| Name | Type | Description |
|---|---|---|
page |
Page
|
The Playwright Page object. |
sender |
str
|
The name or phone number of the sender. |
timestamp |
datetime
|
The time the message was received. |
text |
str
|
The text content of the message. |
container |
ElementHandle
|
The DOM element containing the message. |
is_outgoing |
bool
|
True if the message was sent by the user, False otherwise. |
msg_id |
str
|
The unique identifier of the message. |
kind |
Optional[str]
|
WhatsApp's message type ("chat", "image", "sticker", "ptt"...), set only by collect_messages; None otherwise or when unknown. |
Source code in src/whatsplay/object/message.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
dispose()
async
Release the DOM handle held by this message.
The container ElementHandle pins its DOM node in the renderer until disposed. A long-running consumer that only reads text/sender/ timestamp should call this after each message (or after each batch) so handles do not accumulate until the renderer runs out of committable V8 heap and crashes. Safe to call more than once and on messages whose interactive methods are never used.
Source code in src/whatsplay/object/message.py
from_element(elem, page, own_push_names=None, model_texts=None)
async
classmethod
Create a Message instance from a DOM element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elem
|
ElementHandle
|
The message container element. |
required |
page
|
Page
|
The Playwright page. |
required |
own_push_names
|
Optional[Iterable[str]]
|
Account push names identifying own bubbles (e.g. ["Bikes Amigorena"]). Compared accent/case-insensitively. |
None
|
Direction precedence is DIRECTION_ORDER (see module constants): tail is ground truth for group leaders; class and data-id are opportunistic (dead in some WA Web builds, never required) and outrank group inheritance; group inheritance (ChatManager, applied after this method) outranks positional; positional measures the inner bubble and abstains on inconclusive geometry; sender identity ("Tu", or own_push_names when explicitly passed) is an optional last resort. A bubble with no signal in either direction defaults to incoming with direction_signal "none" (documented fail-safe; ChatManager flags survivors via the fallback counter + log).
Returns:
| Type | Description |
|---|---|
Optional[Message]
|
A new Message instance or None if parsing fails. |
Source code in src/whatsplay/object/message.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | |
is_from_self(own_push_names=None)
Safe skip decision for bot consumers.
Returns True when the bubble is structurally outgoing OR its sender is the account identity ("Tu" or one of own_push_names). An inherited display sender alone never counts as self.
Source code in src/whatsplay/object/message.py
react(emoji)
async
Reacts to this message with the given emoji.
This method simulates the user interaction of hovering over the message, clicking the reaction button, and selecting an emoji.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
emoji
|
str
|
The emoji character to react with (e.g., "👍", "❤️"). |
required |
Source code in src/whatsplay/object/message.py
FileMessage
A document or media attachment. Adds filename and download(page, downloads_dir).
whatsplay.object.message.FileMessage
Bases: Message
Represents a message containing a downloadable file.
Inherits from Message and adds specific functionality for handling
media and document attachments.
Attributes:
| Name | Type | Description |
|---|---|---|
filename |
str
|
The name of the file (e.g., "document.pdf"). |
download_icon |
ElementHandle
|
The DOM element for the download button. |
Source code in src/whatsplay/object/message.py
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 | |
dispose()
async
Release the container and download-icon handles held by this file message.
download(page, downloads_dir)
async
Download the attached file.
Clicks the download icon and waits for the download to complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
Page
|
The Playwright Page object. |
required |
downloads_dir
|
Path
|
The directory where the file should be saved. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Path]
|
The Path to the saved file, or None if the download failed. |
Source code in src/whatsplay/object/message.py
from_element(elem, page, own_push_names=None, model_texts=None)
async
classmethod
Create a FileMessage from a DOM element.
Checks for the presence of a download/document icon and extracts the filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elem
|
ElementHandle
|
The message container element. |
required |
page
|
Page
|
The Playwright page. |
required |
own_push_names
|
Optional[Iterable[str]]
|
Account push names identifying own bubbles. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[FileMessage]
|
A new FileMessage instance or None if not a valid file message. |
Source code in src/whatsplay/object/message.py
get_codec_info(file_path)
Get codec information from a downloaded audio file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path
|
Path to the downloaded audio file |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Dict with codec info or None if detection fails |
Source code in src/whatsplay/object/message.py
VoiceMessage
A voice note. Adds duration and download(page, downloads_dir).
whatsplay.object.message.VoiceMessage
Bases: Message
Represents a voice message without visible download icon.
This class handles voice messages that need to be played first to trigger the download capability.
Source code in src/whatsplay/object/message.py
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 | |
dispose()
async
download(page, downloads_dir)
async
Download voice message using combined methods.
Tries: 1) play → download icon, 2) context menu, 3) blob.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
Page
|
The Playwright page. |
required |
downloads_dir
|
Path
|
The directory where the file should be saved. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Path]
|
The Path to the saved file, or None if download failed. |
Source code in src/whatsplay/object/message.py
download_via_blob(page, downloads_dir)
async
Download voice message by extracting audio blob directly.
Method 2: Extract audio src/blob via JavaScript → download
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
Page
|
The Playwright Page object. |
required |
downloads_dir
|
Path
|
The directory where the file should be saved. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Path]
|
The Path to the saved file, or None if download failed. |
Source code in src/whatsplay/object/message.py
download_via_context_menu(page, downloads_dir)
async
Download voice message via right-click context menu.
Method 3: Right-click → select "Descargar" → download
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
Page
|
The Playwright page. |
required |
downloads_dir
|
Path
|
The directory where the file should be saved. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Path]
|
The Path to the saved file, or None if download failed. |
Source code in src/whatsplay/object/message.py
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 | |
download_via_play(page, downloads_dir)
async
Download voice message by clicking play first, then download.
Method 1: Click play button → wait for download icon → download
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
Page
|
The Playwright Page object. |
required |
downloads_dir
|
Path
|
The directory where the file should be saved. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Path]
|
The Path to the saved file, or None if download failed. |
Source code in src/whatsplay/object/message.py
from_element(elem, page, own_push_names=None, model_texts=None)
async
classmethod
Create a VoiceMessage from a DOM element.
Checks for voice message indicators (mic icon, voice container).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elem
|
ElementHandle
|
The message container element. |
required |
page
|
Page
|
The Playwright page. |
required |
own_push_names
|
Optional[Iterable[str]]
|
Account push names identifying own bubbles. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[VoiceMessage]
|
A new VoiceMessage instance or None if not a valid voice message. |
Source code in src/whatsplay/object/message.py
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 | |
Managers
Internal managers that handle different aspects of the client's functionality.
Chat Manager
Responsible for finding and managing chats.
whatsplay.chat_manager.ChatManager
Manages chat operations for WhatsApp Web.
This class handles all chat-related operations including: - Opening and closing chats - Searching for conversations - Sending messages and files - Detecting unread chats - Downloading files from chats - Managing group operations
Attributes:
| Name | Type | Description |
|---|---|---|
client |
Reference to the main WhatsApp client |
|
_page |
Playwright page object for browser interactions |
|
wa_elements |
WhatsApp Web elements helper |
Source code in src/whatsplay/chat_manager.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 | |
__init__(client)
Initialize ChatManager with client reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client
|
The main WhatsApp client instance |
required |
Source code in src/whatsplay/chat_manager.py
add_members_to_group(group_name, members)
async
Add members to an existing group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_name
|
str
|
Name of the group |
required |
members
|
List[str]
|
List of member names to add |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if members were added successfully, False otherwise |
Source code in src/whatsplay/chat_manager.py
close()
async
Close an active search first, otherwise close the current chat.
Search owns its Escape event so it cannot bubble through and close the underlying chat in the same operation.
Source code in src/whatsplay/chat_manager.py
collect_messages(own_push_names=None, dispose=False)
async
Collect all currently visible messages in the active chat.
Scans all visible message containers and returns a list of Message, FileMessage, or VoiceMessage instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
own_push_names
|
Optional[Union[str, list]]
|
Optional account push names identifying own bubbles (e.g. ["Bikes Amigorena"]). Last-resort fallback only: forwarded to Message.from_element and consulted when tail + class + data-id + positional + group are all inconclusive. Compared accent/case-insensitively. The dispose: When True, release each message's container handle before returning. Use for data-only consumers (bots that only read text/sender/timestamp) to avoid accumulating DOM handles in the renderer; keep False (default) so react() and download() still work. default path (None) classifies direction config-free via positional alignment and group inheritance. |
None
|
Direction contract (precedence is DIRECTION_ORDER in whatsplay.object.message; config-free first): tail (ground truth) > class / data-id (opportunistic, outrank group) > group inheritance (tail-delimited groups, direction never leaks across an outgoing/incoming boundary) > positional inner-bubble alignment (right = outgoing, left = incoming; inconclusive geometry abstains) > sender identity (last resort).
Sender inheritance contract
Sender-less continuation bubbles inherit the previous bubble's sender for DISPLAY only (sender_inherited=True). Consumers must use is_outgoing / is_from_self() for skip logic, never the inherited sender. Direction is preserved through inheritance: a sender-less bubble with no direction signal of its own inherits the group direction; a bubble with its own positive signal (including position-out/position-in) keeps it.
Returns:
| Type | Description |
|---|---|
List[Union[Message, FileMessage, VoiceMessage]]
|
List of Message, FileMessage, or VoiceMessage instances |
Source code in src/whatsplay/chat_manager.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | |
download_all_files(carpeta=None, detect_codecs=True)
async
Download all file attachments from the current chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carpeta
|
Optional[str]
|
Optional custom download directory path |
None
|
detect_codecs
|
bool
|
If True, detect codec info for each file |
True
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of dicts with file path and codec info |
Source code in src/whatsplay/chat_manager.py
download_file_by_index(index, carpeta=None, detect_codecs=True)
async
Download a specific file by its index in the message list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Zero-based index of the file to download |
required |
carpeta
|
Optional[str]
|
Optional custom download directory path |
None
|
detect_codecs
|
bool
|
If True, detect codec info for the file |
True
|
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Dict with path, type, and codec info, or None if failed |
Source code in src/whatsplay/chat_manager.py
new_group(group_name, members)
async
Create a new WhatsApp group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_name
|
str
|
Name for the new group |
required |
members
|
List[str]
|
List of member names to add to the group |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if group was created successfully, False otherwise |
Source code in src/whatsplay/chat_manager.py
open(chat_name, timeout=DEFAULT_WAIT_TIMEOUT, open_via_url=False, is_group=None)
async
Open a chat by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_name
|
str
|
Name of the chat to open |
required |
timeout
|
int
|
Maximum time to wait in milliseconds |
DEFAULT_WAIT_TIMEOUT
|
open_via_url
|
bool
|
If True, forces opening via URL (for phone numbers) |
False
|
is_group
|
Optional[bool]
|
If set, the chat must be a group (True) or a contact (False) |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if chat was opened successfully, False otherwise |
Source code in src/whatsplay/chat_manager.py
react_to_last_message(emoji)
async
React to the last visible message in the current chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
emoji
|
str
|
The emoji to react with (e.g., "👍", "❤️") |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if reaction was successful, False otherwise |
Source code in src/whatsplay/chat_manager.py
search_conversations(query, close=True)
async
Search for conversations by term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search term |
required |
close
|
bool
|
If True, closes the search after completion |
True
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of matching conversations |
Source code in src/whatsplay/chat_manager.py
send_file(chat_name, path, open_via_url=False, is_group=None)
async
Send a file attachment to a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_name
|
str
|
Name of the chat to send the file to |
required |
path
|
str
|
Absolute path to the file to send |
required |
open_via_url
|
bool
|
If True, opens the chat via URL (required for phone numbers) |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if file was sent successfully, False otherwise |
Source code in src/whatsplay/chat_manager.py
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 | |
send_message(chat_query, message, open_via_url=False, is_group=None)
async
Send a text message to a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_query
|
str
|
Name or identifier of the chat |
required |
message
|
str
|
Text message to send |
required |
open_via_url
|
bool
|
If True, opens the chat via URL before sending |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if message was sent successfully, False otherwise |
Source code in src/whatsplay/chat_manager.py
wait_for_sent_confirmation(before_ids, timeout=DEFAULT_WAIT_TIMEOUT)
async
Wait until a message that was not in before_ids shows a delivery
status ("Enviado", "Entregado", "Leído") in the open chat.
An older outgoing message that is already read never counts. Verified live 2026-10-20: a just-sent bubble gets a new data-id and a status span.
Source code in src/whatsplay/chat_manager.py
State Manager
Responsible for managing the client's internal state.
whatsplay.state_manager.StateManager
Manages WhatsApp Web client state transitions and QR code handling.
This class monitors the WhatsApp Web state and triggers appropriate actions for each state transition, including QR code display, authentication handling, and unread message detection.
Attributes:
| Name | Type | Description |
|---|---|---|
client |
Reference to the main WhatsApp client |
|
_page |
Playwright page object for browser interactions |
|
wa_elements |
WhatsApp Web elements helper |
|
last_qr_shown |
Optional[bytes]
|
Binary data of the last displayed QR code |
qr_server_started |
bool
|
Flag indicating if QR server is running |
Source code in src/whatsplay/state_manager.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
__init__(client)
Initialize StateManager with client reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client
|
The main WhatsApp client instance |
required |
Source code in src/whatsplay/state_manager.py
Authentication
Authentication handlers are used to manage the WhatsApp session.
Local Profile Auth
The recommended handler for saving your session locally to avoid repeated QR scans.
whatsplay.auth.local_profile_auth.LocalProfileAuth
Bases: AuthBase
Authentication using a local browser profile
Source code in src/whatsplay/auth/local_profile_auth.py
get_browser_args()
Get browser launch arguments for profile
Source code in src/whatsplay/auth/local_profile_auth.py
load_session()
async
Check if profile exists and has state
save_session()
async
Save browser session state
setup_context(context)
async
Configure browser context with profile settings.
NOTA: NO guardar storage_state aquí — context.storage_state(path=...) cierra/cancela el contexto en versiones recientes de Playwright, causando "Target page, context or browser has been closed". El perfil ya persiste en el user_data_dir, así que no hace falta este paso.
Source code in src/whatsplay/auth/local_profile_auth.py
No Auth
A handler that does not save any session data, requiring a new login on every run.
whatsplay.auth.no_auth.NoAuth
Bases: AuthBase
Authentication strategy that doesn't persist any data
Source code in src/whatsplay/auth/no_auth.py
get_browser_args()
load_session()
async
save_session()
async
Filters
The filters module provides classes and instances to filter events.
Predefined Message Filters
A collection of ready-to-use filters for the on_message event.
whatsplay.filters.message_filter
Message filtering utilities for WhatsApp messages
MessageFilter
Helper class for filtering WhatsApp messages
Source code in src/whatsplay/filters/message_filter.py
filter_search_result(text)
classmethod
Filtra y formatea un resultado de búsqueda
Returns:
| Type | Description |
|---|---|
list[str]
|
List[str]: (texto_formateado) |
Source code in src/whatsplay/filters/message_filter.py
Custom Filter Base Class
Inherit from this class to create your own powerful, reusable filters.
whatsplay.filters.filters.CustomFilter
Bases: Filter
A filter that uses a custom function to test values.