Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

WAsock

wasock (WhatsApp Socket) is a lightweight Python library for interacting with WhatsApp, built on top of Baileys via a Node.js subprocess that communicates with Python over a TCP socket.

Made by an Egyptian developer 🇪🇬 — Ahmed Mohmmed-AM.

Status: 0.5.1-Beta — still under development, the API may change before a stable release.


Overview

wasock gives you a simple Python interface for WhatsApp: receiving messages, replying to them, sending new messages, deleting messages, and displaying a QR code or pairing code for login — without writing a single line of JavaScript.

wasock is a lightweight Python wrapper around Baileys. It spawns a Node.js subprocess and talks to it over a local TCP socket, so you can send, receive, reply to, and delete WhatsApp messages entirely from Python.


Requirements

  • Python >= 3.8
  • Node.js (>= 18 recommended)
  • An internet connection to install the initial npm dependencies

Installation

pip install wasock

After installing, you need to install the Node.js dependencies once (Baileys and pino) inside the library's folder:

cd $(python -c "import wasock, os; print(os.path.join(os.path.dirname(wasock.__file__), 'node'))")
npm install

Quick Start

from wasock import WhatsAppSocket, Message, QRCode, Connection

bot = WhatsAppSocket(authName="auth", loggerLevel="silent")

def onQr(data):
    qr = QRCode(data["qr"], bot.nodeJS, small=True, type="img", imgWidth=500)
    qr.render("qr.png")
    print("Scan qr.png with WhatsApp on your phone")

def onConnection(data):
    conn = Connection(data)
    if conn.connected:
        print("Connection opened!")
    else:
        print(f"Connection closed: {conn.reason}")

def onMessage(data):
    message = Message(data, bot.nodeJS)
    if message.fromBot:
        return

    if message.text == "!ping":
        message.reply("pong")

bot.on("login", onQr)
bot.on("connection", onConnection)
bot.on("message", onMessage)

bot.start()

try:
    input("Bot is running, press Enter to exit...\n")
finally:
    bot.end()

API

WhatsAppSocket(authName="auth", loggerLevel="silent")

Starts the connection to the Node server and sets up the session.

  • .start() — starts the actual connection to WhatsApp.
  • .on(event, callback) — registers an event listener ("login", "connection", "message").
  • .requestPairingCode(phoneNumber, customPairingCode=None) — gets a pairing code for the given phone number, as an alternative to scanning a QR code. customPairingCode must be exactly 8 uppercase letters/digits if provided.
  • .end() — closes the connection and stops the Node process.

Message

Automatically built for each incoming message. Key properties:

Property Description
.text The message text
.chat The chat's JID
.fromBot True if the message was sent by the bot itself
.quoted / .quotedText / .quotedSender The quoted message's data (if this message is a reply)

Methods:

  • .reply(msg, chat=None, quoted=None) — replies to the message.
  • .send(msg, chat=None) — sends a new message (without a quote).
  • .delete() — deletes the message (Delete for Everyone).

QRCode(data, nodeJS, small=True, type="terminal", imgWidth=500)

  • data: the raw QR string received from the "login" event.
  • nodeJS: the bot.nodeJS instance, used to send the QR to the Node server for rendering.
  • small: True for a compact QR (terminal mode only), False for a larger one.
  • type: "terminal" (prints to the terminal) or "img" (saves as an image file).
  • imgWidth: width in pixels for the generated image, only used when type="img". Must be an int.

Methods:

  • .render(imgName="qr.png") — renders the QR code. For type="terminal", prints it directly to the terminal. For type="img", saves it as an image file with the given name.

Connection

  • .connectedTrue/False
  • .statusCode — the disconnect status code (if any)
  • .reason — the disconnect reason (if any)
  • .isAuthFailure()True if the reason was a 401 (a new QR code is needed)

Important Notes

  • The auth/ folder contains sensitive WhatsApp session data — never commit it to GitHub.
  • The location of auth/ is resolved relative to the script's working directory, not the library's location.

License

MIT License — see the LICENSE file for details.


Author

Ahmed Mohmmed-AM — Egyptian developer 🇪🇬 GitHub: @Ahmed-Mohmmed-AM

Download files

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

Source Distribution

wasock-0.5.1b0.tar.gz (13.9 MB view details)

Uploaded Source

Built Distribution

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

wasock-0.5.1b0-py3-none-any.whl (15.2 MB view details)

Uploaded Python 3

File details

Details for the file wasock-0.5.1b0.tar.gz.

File metadata

  • Download URL: wasock-0.5.1b0.tar.gz
  • Upload date:
  • Size: 13.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for wasock-0.5.1b0.tar.gz
Algorithm Hash digest
SHA256 99b5dd3c66a1cd686479a9eab690867fce13831b09e4bbe3dba16244345d9f35
MD5 63795a806f0860b74ac686a28f7b7a71
BLAKE2b-256 75c2bdbd0a8eb8af8db9efdc48641575f114984b3e941dda01ea1a47d8be0dd8

See more details on using hashes here.

File details

Details for the file wasock-0.5.1b0-py3-none-any.whl.

File metadata

  • Download URL: wasock-0.5.1b0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for wasock-0.5.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 da6d9e8069c903f2f57fbbfa91bb0999afd68bf5459042fd233b1cd13a55b3e0
MD5 979acd157e30e2447f385cdea54fa91d
BLAKE2b-256 aef8b55f1cc3824c04c1cd39b6425aacd6d21daa914c5db116ab63d668623622

See more details on using hashes here.

Release history Release notifications | RSS feed

0.5.1

2 files

This release

0.5.1b0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page