Skip to content

Client

client

Client

Client(
    path: Path | str,
    *,
    transport_factory: Callable[[], Transport]
    | None = None,
    qr_callback: Callable[[object], Awaitable[None] | None]
    | None = None,
    push_name: str = DEFAULT_PUSH_NAME,
    platform: str = "chrome",
    version: tuple[int, int, int] = DEFAULT_VERSION,
    ack_timeout: float = DEFAULT_ACK_TIMEOUT,
    auto_receipts: bool = True,
    media_transport: MediaTransport | None = None,
    node_observer: Callable[[Node], None] | None = None,
    sent_observer: Callable[[Node], None] | None = None,
    reconnect: bool = True,
    reconnect_delay: float = 1.0,
    max_reconnect_delay: float = MAX_RECONNECT_DELAY,
)

A connected WhatsApp session.

Usage:

client = Client("bot.session", qr_callback=show_qr)
await client.connect()
await client.send_text("4917...@s.whatsapp.net", "hi")
await client.run()          # serves until closed

connect async

connect() -> ClientInfo

Log in (pairing first if the session file is new) and start serving.

Returns as soon as the server accepts the login payload. Pairing through a QR code calls qr_callback from the connection task while this method waits.

run async

run() -> None

Connect and then serve until close() is called.

close async

close() -> None

Stop serving and release the session file.

send_text async

send_text(
    jid: str,
    text: str,
    *,
    quote: str | None = None,
    wait_for_message: bool = True,
) -> SendResult

Send text; quote is the id of a message to reply to.

send_reaction async

send_reaction(
    chat_jid: str,
    message_id: str,
    text: str,
    *,
    sender_jid: str | None = None,
    wait_for_message: bool = False,
) -> SendResult

React with an emoji; pass an empty string to remove your reaction.

send_media async

send_media(
    jid: str,
    data: bytes,
    media_type: str,
    *,
    caption: str | None = None,
    file_name: str | None = None,
    mime_type: str | None = None,
    seconds: int | None = None,
    width: int | None = None,
    height: int | None = None,
    wait_for_message: bool = True,
) -> SendResult

Encrypt, upload and send an attachment (image, video, audio or document).

download_media async

download_media(media: MediaInfo) -> bytes

Fetch and decrypt an attachment announced by a MessageEvent.

sync_app_state async

sync_app_state() -> list[SyncedCollection]

Pull app state patches (contacts, mutes, pins, ...) for every collection.

Requires sync keys, which WhatsApp shares from the phone; without them the call raises and the local copy stays as it is.

save_message async

save_message(
    chat_jid: str,
    message_id: str,
    text: str | None = None,
    *,
    sender_jid: str | None = None,
    timestamp: int | None = None,
    media: MediaInfo | None = None,
    outgoing: bool = False,
) -> None

Record a message in the session file (useful for history imported from elsewhere).

get_chat_history async

get_chat_history(
    chat_jid: str,
    limit: int = 100,
    *,
    before_timestamp: int | None = None,
) -> list[StoredMessage]

Messages of one chat from the session file, newest first.

lid_for async

lid_for(jid: str) -> str | None

The LID address of a phone-number JID, when the phone shared it.

set_contact_name async

set_contact_name(jid: str, name: str) -> None

Local address-book name, like a phone contact list entry.

download_stored async

download_stored(message: StoredMessage) -> bytes

Download the attachment of a message read back from the session file.

mark_read async

mark_read(
    event: MessageEvent, *, receipt_type: str = RECEIPT_READ
) -> None

Tell the sender their message was read (or just delivered).

ClientInfo dataclass

ClientInfo(jid: str, lid: str | None, push_name: str)

ClientError

Bases: WapyError

The client cannot be started or is in the wrong state.

SendResult dataclass

SendResult(
    message_id: str,
    sent: list[str] = list(),
    failed: list[tuple[str, str]] = list(),
    acked: bool | None = None,
)

acked is None when the caller did not ask to wait for the ack.

OutgoingMessage dataclass

OutgoingMessage(chat_jid: str, payload: bytes)

What we remember about a sent message so a retry request can resend it.

default_mime_type

default_mime_type(media_type: str) -> str