slidgram.session¶
Attributes¶
Classes¶
Module Contents¶
- class slidgram.session.Session(*a, **kw)[source]¶
Bases:
slidge.BaseSession
[int
,Recipient
]The session of a registered User.
Represents a gateway user logged in to the legacy network and performing actions.
Will be instantiated automatically on slidge startup for each registered user, or upon registration for new (validated) users.
Must be subclassed for a functional Legacy Module.
- xmpp: slidgram.gateway.Gateway[source]¶
The gateway instance singleton. Use it for low-level XMPP calls or custom methods that are not session-specific.
- bookmarks: slidgram.group.Bookmarks[source]¶
- static xmpp_to_legacy_msg_id(i)[source]¶
Convert a legacy XMPP ID to a valid XMPP msg ID. Needed for read marks and message corrections.
The default implementation just converts the legacy ID to a
str
, but this should be overridden in case some characters needs to be escaped, or to add some additional, legacy network <Legacy Network>-specific logic.The default implementation is an identity function.
- async login()[source]¶
Logs in the gateway user to the legacy network.
Triggered when the gateway start and on user registration. It is recommended that this function returns once the user is logged in, so if you need to await forever (for instance to listen to incoming events), it’s a good idea to wrap your listener in an asyncio.Task.
- Returns:
Optionally, a text to use as the gateway status, e.g., “Connected as ‘dude@legacy.network’”
- async logout()[source]¶
Logs out the gateway user from the legacy network.
Called on gateway shutdown.
- async on_active(*_args, **_kwargs)[source]¶
Triggered when the user sends an ‘active’ chat state (XEP-0085)
- Parameters:
chat – See
BaseSession.on_text()
thread
- async on_inactive(*_args, **_kwargs)[source]¶
Triggered when the user sends an ‘inactive’ chat state (XEP-0085)
- Parameters:
chat – See
BaseSession.on_text()
thread
- async on_text(chat, text, *, reply_to_msg_id=None, mentions=None, **_kwargs)[source]¶
Triggered when the user sends a text message from XMPP to a bridged entity, e.g. to
translated_user_name@slidge.example.com
, ortranslated_group_name@slidge.example.com
Override this and implement sending a message to the legacy network in this method.
- Parameters:
text (str) – Content of the message
chat (Recipient) – Recipient of the message.
LegacyContact
instance for 1:1 chat,MUC
instance for groups.reply_to_msg_id (int | None) – A legacy message ID if the message references (quotes) another message (XEP-0461)
reply_to_fallback_text – Content of the quoted text. Not necessarily set by XMPP clients
reply_to – Author of the quoted message.
LegacyContact
instance for 1:1 chat,LegacyParticipant
instance for groups. If None, should be interpreted as a self-reply if reply_to_msg_id is not None.link_previews – A list of sender-generated link previews. At the time of writing, only Cheogram supports it.
mentions (list[slidge.util.types.Mention] | None) – (only for groups) A list of Contacts mentioned by their nicknames.
thread
- Returns:
An ID of some sort that can be used later to ack and mark the message as read by the user
- Return type:
- async on_correct(chat, text, legacy_msg_id, *, reply_to_msg_id=None, mentions=None, **_kwargs)[source]¶
Triggered when the user corrects a message using XEP-0308
This is only possible if a valid
legacy_msg_id
was returned byon_text()
.- Parameters:
chat (Recipient) – See
BaseSession.on_text()
text (str) – The new text
legacy_msg_id (int) – Identifier of the edited message
thread
link_previews –
A list of sender-generated link previews. At the time of writing, only Cheogram supports it.
mentions (list[slidge.util.types.Mention] | None) – (only for groups) A list of Contacts mentioned by their nicknames.
reply_to_msg_id (int | None)
- Return type:
None
- async on_file(chat, url, *, http_response, reply_to_msg_id=None, **_kwargs)[source]¶
Triggered when the user sends a file using HTTP Upload (XEP-0363)
- Parameters:
url (str) – URL of the file
chat (slidge.util.types.RecipientType) – See
BaseSession.on_text()
http_response (aiohttp.ClientResponse) – The HTTP GET response object on the URL
reply_to_msg_id (int | None) – See
BaseSession.on_text()
reply_to_fallback_text – See
BaseSession.on_text()
reply_to – See
BaseSession.on_text()
thread
- Returns:
An ID of some sort that can be used later to ack and mark the message as read by the user
- Return type:
- async on_sticker(chat, sticker, *, reply_to_msg_id=None, **_kwargs)[source]¶
Triggered when the user sends a file using HTTP Upload (XEP-0363)
- Parameters:
chat (Recipient) – See
BaseSession.on_text()
sticker (slidge.util.types.Sticker) – The sticker sent by the user.
reply_to_msg_id (int | None) – See
BaseSession.on_text()
reply_to_fallback_text – See
BaseSession.on_text()
reply_to – See
BaseSession.on_text()
thread
- Returns:
An ID of some sort that can be used later to ack and mark the message as read by the user
- Return type:
- async on_react(chat, legacy_msg_id, emojis, thread=None)[source]¶
Triggered when the user sends message reactions (XEP-0444).
- Parameters:
- async on_composing(chat, thread=None)[source]¶
Triggered when the user starts typing in a legacy chat (XEP-0085)
- Parameters:
chat (slidge.util.types.RecipientType) – See
BaseSession.on_text()
thread
- async on_paused(chat, thread=None)[source]¶
Triggered when the user pauses typing in a legacy chat (XEP-0085)
- Parameters:
chat (slidge.util.types.RecipientType) – See
BaseSession.on_text()
thread
- async on_displayed(chat, legacy_msg_id, thread=None)[source]¶
Triggered when the user reads a message in a legacy chat. (XEP-0333)
This is only possible if a valid
legacy_msg_id
was passed when transmitting a message from a legacy chat to the user, eg inslidge.contact.LegacyContact.send_text()
orslidge.group.LegacyParticipant.send_text()
.- Parameters:
chat (slidge.util.types.RecipientType) – See
BaseSession.on_text()
legacy_msg_id (int) – Identifier of the message/
thread
- async on_moderate(muc, legacy_msg_id, reason)[source]¶
Triggered when the user attempts to retract a message that was sent in a MUC using XEP-0425.
If retraction is not possible, this should raise the appropriate XMPPError with a human-readable message.
NB: the legacy module is responsible for calling :method:`LegacyParticipant.moderate` when this is successful, because slidge will acknowledge the moderation IQ, but will not send the moderation message from the MUC automatically.
- Parameters:
muc (slidgram.group.MUC) – The MUC in which the message was sent
legacy_msg_id (int) – The legacy ID of the message to be retracted
reason (str | None) – Optionally, a reason for the moderation, given by the user-moderator.
- async on_create_group(name, contacts)[source]¶
Triggered when the user request the creation of a group via the dedicated Command.
- Parameters:
name (str) – Name of the group
contacts (list[slidgram.contact.Contact]) – list of contacts that should be members of the group
- Return type:
- async on_invitation(contact, muc, reason)[source]¶
Triggered when the user invites a Contact to a legacy MUC via XEP-0249.
The default implementation calls
LegacyMUC.on_set_affiliation()
with the ‘member’ affiliation. Override if you want to customize this behaviour.- Parameters:
contact (slidgram.contact.Contact) – The invitee
muc (slidgram.group.MUC) – The group
reason (str | None) – Optionally, a reason
- async on_retract(chat, legacy_msg_id, thread=None)[source]¶
Triggered when the user retracts (XEP-0424) a message.
- Parameters:
chat (Recipient) – See
BaseSession.on_text()
thread
legacy_msg_id (int) – Legacy ID of the retracted message
- async on_avatar(bytes_, hash_, type_, width, height)[source]¶
Triggered when the user uses modifies their avatar via XEP-0084.
- Parameters:
bytes – The data of the avatar. According to the spec, this should always be a PNG, but some implementations do not respect that. If None it means the user has unpublished their avatar.
hash – The SHA1 hash of the avatar data. This is an identifier of the avatar.
type – The MIME type of the avatar.
width (int | None) – The width of the avatar image.
height (int | None) – The height of the avatar image.
bytes_ (bytes | None)
hash_ (str | None)
type_ (str | None)
- Return type:
None
- async on_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:
- Return type:
slidge.command.SearchResult | None
- async on_leave_group(chat_id)[source]¶
Triggered when the user leaves a group via the dedicated slidge command or the XEP-0077
<remove />
mechanism.This should be interpreted as definitely leaving the group.
- Parameters:
muc_legacy_id – The legacy ID of the group to leave
chat_id (int)
- async _on_tg_msg(_tg, message)[source]¶
- Parameters:
_tg (slidgram.telegram.Client)
message (pyrogram.types.Message)
- Return type:
None
- async _on_tg_edit(_tg, message)[source]¶
- Parameters:
_tg (slidgram.telegram.Client)
message (pyrogram.types.Message)
- Return type:
None
- async _on_tg_status(_tg, user)[source]¶
- Parameters:
_tg (slidgram.telegram.Client)
user (pyrogram.types.User)
- Return type:
None
- async _on_tg_chat_member(_tg, update)[source]¶
- Parameters:
update (pyrogram.types.ChatMemberUpdated)
- async _on_tg_deleted_msg(_tg, messages)[source]¶
- Parameters:
messages (list[pyrogram.types.Message])
- Return type:
None
- async _on_tg_UpdateUserTyping(update, _users, _chats)[source]¶
- Parameters:
update (pyrogram.raw.types.UpdateUserTyping)
- Return type:
None
- async _on_tg_UpdateChatUserTyping(update, _users, _chats)[source]¶
- Parameters:
update (pyrogram.raw.types.UpdateChatUserTyping)
- Return type:
None
- async _on_tg_UpdateChannelUserTyping(update, _users, _chats)[source]¶
- Parameters:
update (pyrogram.raw.types.UpdateChannelUserTyping)
- Return type:
None
- _send_action(actor, action)[source]¶
- Parameters:
actor (slidgram.contact.Contact | slidgram.group.Participant)
action (pyrogram.raw.base.SendMessageAction)
- Return type:
None
- async _on_tg_UpdateReadHistoryOutbox(update, _users, _chats)[source]¶
- Parameters:
update (pyrogram.raw.types.UpdateReadHistoryOutbox)
- Return type:
None
- async _on_tg_UpdateReadHistoryInbox(update, _users, _chats)[source]¶
- Parameters:
update (pyrogram.raw.types.UpdateReadHistoryInbox)
_chats (list[pyrogram.types.Chat])
- Return type:
None
- async _on_tg_UpdateReadChannelInbox(update, _users, _chats)[source]¶
- Parameters:
update (pyrogram.raw.types.UpdateReadChannelInbox)
- Return type:
None
- async _on_tg_UpdatePinnedMessages(update, _users, _chats)[source]¶
- Parameters:
update (pyrogram.raw.types.UpdatePinnedMessages)
- Return type:
None
- async get_sender(update)[source]¶
- Parameters:
update (pyrogram.types.Message | pyrogram.types.MessageReactionUpdated)
- Return type:
tuple[slidgram.contact.Contact | slidgram.group.Participant, bool]
- async _get_actor_by_peer(peer, user=False)[source]¶
- Parameters:
peer (pyrogram.raw.base.Peer)
- Return type:
- async _get_muc_by_peer(peer)[source]¶
- Parameters:
peer (pyrogram.raw.base.Peer)
- Return type:
slidgram.group.MUC | None