slidge.plugins.whatsapp.session#

Module Contents#

Classes#

Session

Represents a gateway user logged in to the legacy network and performing actions.

Attachment

A file attachment to a message

Functions#

ignore_contact_is_user(func)

make_sync(func, loop)

Wrap async function in synchronous operation, running against the given loop in thread-safe mode.

strip_quote_prefix(text)

Return multi-line text without leading quote marks (i.e. the ">" character).

Attributes#

slidge.plugins.whatsapp.session.MESSAGE_PAIR_SUCCESS = 'Pairing successful! You might need to repeat this process in the future if the Linked Device is...'[source]#
slidge.plugins.whatsapp.session.MESSAGE_LOGGED_OUT = 'You have been logged out, please use the re-login adhoc command and re-scan the QR code on your...'[source]#
slidge.plugins.whatsapp.session.URL_SEARCH_REGEX = '(?P<url>https?://[^\\s]+)'[source]#
slidge.plugins.whatsapp.session.Recipient[source]#
slidge.plugins.whatsapp.session.ignore_contact_is_user(func)[source]#
class slidge.plugins.whatsapp.session.Session(user)[source]#

Bases: slidge.BaseSession[str, Recipient]

Represents a gateway user logged in to the legacy network and performing actions.

Will be instantiated automatically when a user sends an online presence to the gateway component, as per XEP-0100.

Must be subclassed for a functional slidge plugin.

Parameters:

user (slidge.GatewayUser) –

xmpp: slidge.plugins.whatsapp.gateway.Gateway[source]#
contacts: slidge.plugins.whatsapp.contact.Roster[source]#
bookmarks: slidge.plugins.whatsapp.group.Bookmarks[source]#
shutdown()[source]#
async login()[source]#

Initiate login process and connect session to WhatsApp. Depending on existing state, login might either return having initiated the Linked Device registration process in the background, or will re-connect to a previously existing Linked Device session.

async logout()[source]#

Logout from the active WhatsApp session. This will also force a remote log-out, and thus require pairing on next login. For simply disconnecting the active session, look at the Session.disconnect() function.

async disconnect()[source]#

Disconnect the active WhatsApp session. This will not remove any local or remote state, and will thus allow previously authenticated sessions to re-authenticate without needing to pair.

async handle_event(event, ptr)[source]#

Handle incoming event, as propagated by the WhatsApp adapter. Typically, events carry all state required for processing by the Gateway itself, and will do minimal processing themselves.

async handle_chat_state(state)[source]#
Parameters:

state (slidge.plugins.whatsapp.generated.whatsapp.ChatState) –

async handle_receipt(receipt)[source]#

Handle incoming delivered/read receipt, as propagated by the WhatsApp adapter.

Parameters:

receipt (slidge.plugins.whatsapp.generated.whatsapp.Receipt) –

async handle_call(call)[source]#
Parameters:

call (slidge.plugins.whatsapp.generated.whatsapp.Call) –

async _get_reply_to(message)[source]#
Parameters:

message (slidge.plugins.whatsapp.generated.whatsapp.Message) –

async _get_preview(text)[source]#
Parameters:

text (str) –

Return type:

Optional[slidge.plugins.whatsapp.generated.whatsapp.Preview]

async handle_message(message)[source]#

Handle incoming message, as propagated by the WhatsApp adapter. Messages can be one of many types, including plain-text messages, media messages, reactions, etc., and may also include other aspects such as references to other messages for the purposes of quoting or correction.

Parameters:

message (slidge.plugins.whatsapp.generated.whatsapp.Message) –

async send_text(chat, text, *, reply_to_msg_id=None, reply_to_fallback_text=None, reply_to=None, **_)[source]#

Send outgoing plain-text message to given WhatsApp contact.

Parameters:
  • chat (Recipient) –

  • text (str) –

  • reply_to_msg_id (Optional[str]) –

  • reply_to_fallback_text (Optional[str]) –

async send_file(chat, url, http_response, reply_to_msg_id=None, **_)[source]#

Send outgoing media message (i.e. audio, image, document) to given WhatsApp contact.

Parameters:
  • chat (Recipient) –

  • url (str) –

  • reply_to_msg_id (Optional[str]) –

async active(c, thread=None)[source]#

WhatsApp has no equivalent to the “active” chat state, so calls to this function are no-ops.

Parameters:

c (Recipient) –

async inactive(c, thread=None)[source]#

WhatsApp has no equivalent to the “inactive” chat state, so calls to this function are no-ops.

Parameters:

c (Recipient) –

async composing(c, thread=None)[source]#

Send “composing” chat state to given WhatsApp contact, signifying that a message is currently being composed.

Parameters:

c (Recipient) –

async paused(c, thread=None)[source]#

Send “paused” chat state to given WhatsApp contact, signifying that an (unsent) message is no longer being composed.

Parameters:

c (Recipient) –

async displayed(c, legacy_msg_id, thread=None)[source]#

Send “read” receipt, signifying that the WhatsApp message sent has been displayed on the XMPP client.

Parameters:
  • c (Recipient) –

  • legacy_msg_id (str) –

async react(c, legacy_msg_id, emojis, thread=None)[source]#

Send or remove emoji reaction to existing WhatsApp message. Slidge core makes sure that the emojis parameter is always empty or a single emoji.

Parameters:
  • c (Recipient) –

  • legacy_msg_id (str) –

  • emojis (list[str]) –

async retract(c, legacy_msg_id, thread=None)[source]#

Request deletion (aka retraction) for a given WhatsApp message.

Parameters:
  • c (Recipient) –

  • legacy_msg_id (str) –

async correct(c, text, legacy_msg_id, thread=None)[source]#

Triggered when the user corrected a message using XEP-0308

This is only possible if a valid legacy_msg_id was passed when transmitting a message from a contact to the user in LegacyContact.send_text() or slidge.LegacyContact.send_file().

Parameters:
  • text (str) –

  • legacy_msg_id (str) –

  • c (Recipient) –

async search(form_values)[source]#

Triggered when the user uses Jabber Search (XEP-0055) on the component

Form values is a dict in which keys are defined in BaseGateway.SEARCH_FIELDS

Parameters:

form_values (dict[str, str]) – search query, defined for a specific plugin by overriding in BaseGateway.SEARCH_FIELDS

Returns:

async get_contact_or_participant(legacy_contact_id, legacy_group_jid)[source]#

Return either a Contact or a Participant instance for the given contact and group JIDs.

Parameters:
  • legacy_contact_id (str) –

  • legacy_group_jid (str) –

_is_carbon(c, legacy_msg_id)[source]#
Parameters:
  • c (Recipient) –

  • legacy_msg_id (str) –

class slidge.plugins.whatsapp.session.Attachment[source]#

Bases: slidge.util.types.LegacyAttachment

A file attachment to a message

At the minimum, one of the path, steam, data or url attribute has to be set

To be used with LegacyContact.send_files() or LegacyParticipant.send_files()

static convert_list(attachments)[source]#
Parameters:

attachments (list) –

static convert(wa_attachment)[source]#
Parameters:

wa_attachment (slidge.plugins.whatsapp.generated.whatsapp.Attachment) –

slidge.plugins.whatsapp.session.make_sync(func, loop)[source]#

Wrap async function in synchronous operation, running against the given loop in thread-safe mode.

slidge.plugins.whatsapp.session.strip_quote_prefix(text)[source]#

Return multi-line text without leading quote marks (i.e. the “>” character).

Parameters:

text (str) –