Skip to main content

A Python library for WhatsApp Web communication using the Multi-Device protocol

Project description

WhatPyLib

A Python library for WhatsApp Web communication using the Multi-Device protocol.

⚠️ Disclaimer: This is an unofficial library. WhatsApp may change their protocol at any time, potentially breaking functionality. Using this library may violate WhatsApp's Terms of Service.

Features

  • 🔌 WebSocket-based - Direct connection to WhatsApp servers
  • 🔐 End-to-end encrypted - Uses Signal Protocol
  • 📱 Multi-device support - No phone required after linking
  • 🔄 Auto-reconnect - Automatic session restoration
  • 📨 Full messaging - Text, media, location, contacts, polls
  • 👥 Group support - Manage participants, settings
  • Async-first - Built for Python 3.10+

Installation

pip install whatpylib

Or from source:

git clone https://github.com/Ali-hmede-codes/whatpylib.git
cd whatpylib
pip install -e .

Quick Start

import asyncio
from whatpylib import WhatsAppClient

async def main():
    # Create client with persistent auth
    client = WhatsAppClient(auth_state_path="./auth")
    
    # Register message handler
    @client.on("message")
    async def on_message(msg):
        print(f"Received from {msg.sender_jid}: {msg.text}")
        
        # Echo back
        if msg.text.lower() == "ping":
            await client.send_message(msg.chat_jid, "pong!")
    
    # Connect (will show QR code on first run)
    await client.connect()
    
    # Keep running
    await client.wait_until_disconnect()

asyncio.run(main())

Sending Messages

Text Messages

# Simple text
await client.send_message("1234567890", "Hello!")

# With mentions
from whatpylib import MessageBuilder

msg = (
    MessageBuilder()
    .to("group_id@g.us")
    .text("Hello @user!")
    .mention(["user@s.whatsapp.net"])
    .build()
)
await client.send_message("group_id@g.us", msg)

Media Messages

# Send image
await client.send_image("1234567890", "photo.jpg", caption="Check this out!")

# Send voice note
await client.send_audio("1234567890", "voice.ogg", ptt=True)

# Send document
await client.send_document("1234567890", "file.pdf", filename="Report.pdf")

# Send location
await client.send_location("1234567890", 40.7128, -74.0060, name="New York")

Interactive Messages

# React to a message
await client.react(message, "👍")

# Reply to a message
await client.reply(message, "I agree!")

# Send poll
await client.send_poll(
    "group@g.us",
    "What's for lunch?",
    ["Pizza", "Burger", "Sushi"],
)

Events

Subscribe to various events:

@client.on("message")
async def on_message(msg):
    """New message received"""
    pass

@client.on("message.reaction")
async def on_reaction(reaction):
    """Reaction added/removed"""
    pass

@client.on("group.participants.update")
async def on_group_update(update):
    """Group members changed"""
    pass

@client.on("presence.update")
async def on_presence(presence):
    """User online/offline/typing"""
    pass

@client.on("connection.update")
async def on_connection(update):
    """Connection state changed"""
    pass

Groups

# Get group info
metadata = await client.get_group_metadata("group@g.us")

# Create group
group_jid = await client.create_group("My Group", ["user1", "user2"])

# Manage participants
await client.add_group_participants("group@g.us", ["user3"])
await client.remove_group_participants("group@g.us", ["user2"])
await client.promote_group_participants("group@g.us", ["user1"])

# Update group settings
await client.update_group_subject("group@g.us", "New Name")
await client.update_group_description("group@g.us", "New Description")

# Get invite link
link = await client.get_group_invite_link("group@g.us")

Presence

# Update your presence
await client.update_presence("available")  # or "unavailable"

# Send typing indicator
await client.send_typing("user@s.whatsapp.net")

# Send recording indicator
await client.send_recording("user@s.whatsapp.net")

Configuration

from whatpylib import WhatsAppClient, Config

config = Config(
    connect_timeout=30,
    qr_timeout=60,
    auto_reconnect=True,
    max_reconnect_attempts=5,
    log_level="DEBUG",
)

client = WhatsAppClient(
    auth_state_path="./auth",
    config=config,
    print_qr=True,
)

Authentication

For a detailed guide on authentication, socket handling, and session persistence, see docs/authentication.md.

QR Code (Default)

On first run, a QR code will be displayed. Scan it with WhatsApp:

  1. Open WhatsApp on your phone
  2. Go to Settings > Linked Devices
  3. Tap "Link a Device"
  4. Scan the QR code

Pairing Code

from whatpylib.auth import PairingCodeHandler

handler = PairingCodeHandler()
code = await handler.request_pairing_code("1234567890")
# Enter the code in WhatsApp on your phone

Storage

# File-based (default)
from whatpylib import FileAuthState
auth = FileAuthState("./auth")

# Multi-file (better for many sessions)
from whatpylib.auth import MultiFileAuthState
auth = MultiFileAuthState("./auth_dir")

# Memory (no persistence)
from whatpylib import MemoryAuthState
auth = MemoryAuthState()

# Custom database
from whatpylib.auth import AuthState

class DatabaseAuthState(AuthState):
    async def load(self) -> bool:
        # Load from database
        pass
    
    async def save(self) -> None:
        # Save to database
        pass
    
    async def clear(self) -> None:
        # Clear from database
        pass

Requirements

  • Python 3.10+
  • websockets
  • cryptography
  • protobuf
  • qrcode
  • Pillow
  • aiohttp
  • pydantic

License

MIT License

Disclaimer

This project is not affiliated with, endorsed by, or connected to WhatsApp or Meta Platforms, Inc. Use responsibly and in accordance with WhatsApp's Terms of Service.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

whatpylib-0.1.1.tar.gz (72.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

whatpylib-0.1.1-py3-none-any.whl (81.0 kB view details)

Uploaded Python 3

File details

Details for the file whatpylib-0.1.1.tar.gz.

File metadata

  • Download URL: whatpylib-0.1.1.tar.gz
  • Upload date:
  • Size: 72.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for whatpylib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2ceb2d45f9afa3bf0c699c9232cc25b6a3f5cf6326afa8c068a7e47923d9fdc6
MD5 b80c7a40a7508143fd0505f8b3768e70
BLAKE2b-256 95231a6e8f1e7e8006af7e66c9494699be360287575b129c3bcd9cfbf42b75e8

See more details on using hashes here.

File details

Details for the file whatpylib-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: whatpylib-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 81.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for whatpylib-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 64ac829617cd5cf224c7bd06251cba51a55121f4d151f22c9acee8a709af3a1b
MD5 8758dae279652b32340e277b590cc63f
BLAKE2b-256 543c3a3d5c1f0f3af9959f4563a1074b6d4c976ef50c5e116f80a636384cdc46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page