Protocol (astra.protocol)
Overview
The protocol layer is the bridge between Python and the injected JavaScript engine running inside the browser page. It exposes two main primitives:
- class ProtocolBridge(page=None)[source]
Manages the two-way bridge between Python and JavaScript.
This class handles the injection of the Astra engine into the browser and facilitates calling remote methods with automatic result normalization.
- Parameters:
page (playwright.async_api.Page | None)
- async connect()[source]
Initializes the bridge by injecting the core engine scripts.
This method uses persistent init scripts to ensure the bridge survives page reloads and navigations.
- async call(method, params=None, timeout=15.0, progress=None)[source]
Calls a remote JS method via the bridge with automatic recovery.
If the bridge is unreachable (page closed, bridge missing), it attempts a single self-heal via ensure_bridge() before retrying.
- async ensure_bridge()[source]
Verifies the JS bridge is alive and re-injects it if needed.
Returns True if the bridge is healthy after this call.
- Return type:
- async execute(code)[source]
Executes raw JavaScript in the page context. This is used for bootstrapping and complex low-level operations.
- async edit_message_native(message_id, text)[source]
High-speed native Playwright fallback for editing messages.
- class EngineAPI(bridge, client=None)[source]
Interface for WhatsApp engine operations.
This class handles calls to the ProtocolBridge and translates the responses into Python objects.
- Parameters:
bridge (ProtocolBridge)
client (Any)
- async bulk_delete(message_ids, for_everyone=True)[source]
Removes multiple messages from the thread.
- async send_media(to, media, mimetype, filename=None, caption=None, options=None)[source]
Sends a media file.
- async create_group(title, participants)[source]
Creates a new group with the specified title and members. Returns the new Group ID.
- async send_media_status(data, mimetype, caption='', options=None)[source]
Sends a media status update.
- async set_profile_name(name)[source]
Updates the user’s pushname. JS bridge first → Playwright keyboard fallback.
- async set_about_text(text)[source]
Updates the user’s ‘About’ text content. JS bridge first → Playwright keyboard fallback.
- async set_privacy(category, value)[source]
Sets a privacy setting. Categories: ‘last_seen’, ‘profile_pic’, ‘about’, ‘status’, ‘read_receipts’ JS bridge first → Playwright keyboard/mouse fallback.
- async get_contact(contact_id)[source]
Fetches metadata for a specific contact.
- Parameters:
contact_id (str)
- Return type:
User
- async get_chat(chat_id)[source]
Fetches a specific chat thread.
- Parameters:
chat_id (str)
- Return type:
Chat
Public highlights
ProtocolBridge.connect()— inject engine and expose uplinkProtocolBridge.call(method, params)— remote call to JS engineEngineAPI.send_text/send_media/get_chatsetc.
Example
bridge = ProtocolBridge()
await bridge.connect()
result = await bridge.call('getChats')
Notes
Bridge calls are proxied to the JS engine and return serializable JSON-like
results.
DataTransformermaps the payloads into Python models.
Call timeouts and progress callbacks are supported by
ProtocolBridge.