WhatsPlay 🚀
Modern WhatsApp Web automation with Playwright and Python
📖 Documentation: https://markbus-ai.github.io/whatsplay/
✨ Features
- Stable selectors — attribute-based (
data-pre-plain-text,aria-label,data-testid), not minified CSS classes that break on every WA update - Safe sending — text is only typed into the chat you asked for, and
send_message()returnsTrueonly once WhatsApp shows the new message with a delivery status - Exact chat matching —
is_grouptells groups from contacts, phone numbers match in full, ambiguous titles are refused instead of guessed - Accurate message text — read from WhatsApp's own message model (captions and replies included), with the DOM as fallback
- Multi-language — works with WhatsApp Web in Spanish and English out of the box
- Voice messages — duration, download, and codec detection
- Persistent sessions — scan QR once, reuse the session
- Unread chat detection — scans from the top of the All filter every time
- File & media support — send any file; download documents, images, and voice notes
- Event-driven —
on_logged_in,on_unread_chat,on_error,on_refresh, etc. - Concurrency-safe — every page operation runs under one re-entrant lock (
client.page_lock) - Long-running bots —
client.refresh()andcollect_messages(dispose=True)keep renderer memory in check - Standard logging — everything goes through
loggingunder thewhatsplaynamespace - Headless mode — works on servers without GUI
Quick Start
pip install whatsplay
python -m playwright install chromium
import asyncio
from pathlib import Path
from whatsplay import Client
from whatsplay.auth import LocalProfileAuth
async def main():
auth = LocalProfileAuth(Path.home() / "whatsapp_session")
client = Client(auth=auth, headless=False)
@client.event("on_unread_chat")
async def on_unread_chat(chats):
for chat in chats:
await client.send_message(chat["name"], "🤖 Automated with WhatsPlay!")
await client.start()
asyncio.run(main())
📦 Installation
From PyPI
pip install whatsplay
After installing, download the Playwright browser:
python -m playwright install chromium
If a system Chromium (/usr/bin/chromium) or Google Chrome (/usr/bin/google-chrome) is installed, WhatsPlay uses it instead.
From source
git clone https://github.com/markbus-ai/whatsplay.git
cd whatsplay
pip install -e .
python -m playwright install chromium
📖 Usage Guide
Basic connection
from pathlib import Path
from whatsplay import Client
from whatsplay.auth import LocalProfileAuth
auth = LocalProfileAuth(data_dir=Path.home() / "whatsapp_session")
client = Client(auth=auth, headless=False)
Events
@client.event("on_logged_in")
async def on_logged_in():
print("✅ Logged in")
@client.event("on_qr")
async def on_qr(qr_bytes):
print("📸 Scan the QR code")
@client.event("on_unread_chat")
async def on_unread_chat(chats):
# chats: [{"name": ..., "unread_count": ..., "last_message": ..., ...}, ...]
for chat in chats:
print(f"📩 {chat['unread_count']} unread in {chat['name']}")
@client.event("on_error")
async def on_error(message):
print(f"❌ {message}")
Reading messages
if await client.open("My Group", is_group=True):
messages = await client.collect_messages()
for msg in messages:
direction = "→" if msg.is_outgoing else "←"
print(f"{direction} [{msg.timestamp}] {msg.sender}: {msg.text}")
await client.close()
Sending messages
# By chat name (exact title)
await client.send_message("My Group", "Hello!", is_group=True)
# By phone number (full international number)
await client.send_message("5491123456789", "Direct message", open_via_url=True)
# Files
await client.send_file("My Group", "/path/to/report.pdf")
Both return True only after WhatsApp shows the new message with a delivery status. On failure they return False and log why — WhatsPlay never raises custom exceptions.
Voice messages and files
from whatsplay import VoiceMessage
await client.open("My Friend")
for msg in await client.collect_messages():
if isinstance(msg, VoiceMessage):
print(f"🎤 Voice message: {msg.duration}")
# Download every file and voice note in the open chat
files = await client.download_all_files(carpeta="./downloads")
for info in files:
print(info["type"], info["path"], info.get("codec"))
Running several operations atomically
async with client.page_lock:
await client.open("Work Chat")
messages = await client.collect_messages()
await client.close()
Long-running bots
messages = await client.collect_messages(dispose=True) # release browser handles right away
await client.refresh() # reload WhatsApp Web to free renderer memory; no new QR needed
Logging
import logging
logging.getLogger("whatsplay").setLevel(logging.WARNING) # quiet
logging.getLogger("whatsplay.wa_elements").setLevel(logging.DEBUG) # DOM detail
If your app doesn't configure logging, WhatsPlay prints INFO messages to stdout.
📚 Examples
The examples/ directory includes ready-to-run scripts:
| File | Description |
|---|---|
simple_example.py |
Basic event-driven auto-reply |
collect_messages_event.py |
Collect messages from a chat after login |
open_example.py |
Open a specific chat programmatically |
search_example.py |
Search conversations with detailed results |
send_file.py |
Send a file to a chat |
react.py |
React to a collected message |
wsp.py |
CLI tool for quick message sending |
verify_selectors.py |
E2E validation of selectors against live WA Web |
investigate_voice.py |
Live DOM investigation for voice message structure |
🛠 Development
Setup
pip install -e . pytest pytest-asyncio
python -m playwright install chromium
Running tests
pytest tests/ -v
Documentation
The docs live in docs/ (English and Spanish) and are built with MkDocs Material. They are deployed to GitHub Pages on every push to main.
pip install -r requirements-docs.txt
mkdocs serve
🗺 Roadmap
Completed ✅
- Async event system
- Persistent session (QR once)
- Message sending / receiving with delivery confirmation
- File & media support, codec detection
- Voice message detection, duration & download
- Unread chat detection
- Search with exact, unambiguous matching
- Stable attribute-based selectors (no more fragile CSS classes)
- Multi-language (Spanish / English)
- Group management (create, add / remove members)
- Reactions (
react_to_last_message(),Message.react()) - Structured logging
- Page lock for concurrent handlers
- Memory management for long-running bots (
refresh(),dispose)
In progress 🚧
- Real
on_messageevent (no more manual open + collect polling) - Selector health check that warns loudly when WhatsApp Web changes its DOM
Planned 🔮
- Send results that explain why a send failed (ambiguous chat, wrong composer, no delivery status)
- Reading older chat history by scrolling
- Warning for unsupported (non English / Spanish) locales
❓ FAQ
Is it safe? It uses WhatsApp Web's official interface — as safe as using WhatsApp in a browser.
Can WhatsApp detect it? There's always risk with automation. Use responsibly.
Does it work headless? Yes, Playwright's headless mode is supported.
Does it work in Spanish? Yes. WhatsApp Web interfaces in Spanish and English are supported; with any other interface language, sending is refused instead of risking the wrong chat.
Why did send_message() return False?
The chat couldn't be verified (wrong or ambiguous title), the composer belonged to another chat, or no delivery status appeared. Check the whatsplay logs — see Troubleshooting.
🐞 Report a bug
Open an issue with:
- Problem description
- Reproduction steps
- Python version and dependencies
- Relevant logs (
logging.getLogger("whatsplay").setLevel(logging.DEBUG))
🤝 Contributing
- Fork the repo
- Create a branch (
git checkout -b feature/amazing) - Commit using Conventional Commits (
git commit -m 'feat: add amazing feature') - Push (
git push origin feature/amazing) - Open a Pull Request
📄 License
Apache 2.0 — see LICENSE.
⭐ Star on GitHub if you find it useful
Made with ❤️ by @markbus-ai
Metadata
Release files for whatsplay 2.6.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| whatsplay-2.6.12.tar.gz | 88.1 kB | Without provenance | Details for whatsplay-2.6.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| whatsplay-2.6.12-py3-none-any.whl | Python 3 | none | any | Details for whatsplay-2.6.12-py3-none-any.whl |
Total release size: 160.5 kB
Release files / whatsplay-2.6.12.tar.gz
| Download URL | whatsplay-2.6.12.tar.gz |
|---|---|
| Size | 88.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
377a4871e720415e75c41c30fde8107b45779103edfcd350fbaf55b3d3b1f385
|
|
BLAKE2b-256 checksum How to use checksums |
0bf98fb852f15bce9f80c2c1a2c3d595e8fa58920b6a6ba2b1a5a291d1642b72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.12
|
Release files / whatsplay-2.6.12-py3-none-any.whl
| Download URL | whatsplay-2.6.12-py3-none-any.whl |
|---|---|
| Size | 72.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8aeeab64fd18b039b3de4b9f3be42d6daff435d7e2f10370e2694770f4f18088
|
|
BLAKE2b-256 checksum How to use checksums |
4a94823f0f829c61bb40e399d10ea293e294e8a35cc077c13e7b332d7a357b06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.12
|
Security for whatsplay 2.6.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.