platforms.base module
Abstract base types for platform adapters.
Every chat platform (Matrix, Discord, Slack, …) implements
PlatformAdapter. Incoming events are normalised into
IncomingMessage before being handed to the shared
MessageProcessor.
- class platforms.base.Attachment(data, mimetype, filename, source_url='')[source]
Bases:
objectA downloaded media attachment, already in raw bytes.
- class platforms.base.IncomingMessage(platform, channel_id, user_id, user_name, text, is_addressed, attachments=<factory>, channel_name='', timestamp=<factory>, message_id='', reply_to_id='', extra=<factory>, reactions='')[source]
Bases:
objectPlatform-agnostic representation of an incoming chat message.
Platform adapters construct one of these for every event and pass it to the
MessageProcessor.- Parameters:
- attachments: list[Attachment]
Media attachments that have already been downloaded.
- class platforms.base.HistoricalMessage(user_id, user_name, text, timestamp, message_id='', is_bot=False, reply_to_id='', reactions='')[source]
Bases:
objectLightweight representation of a message fetched from platform history.
Used by
PlatformAdapter.fetch_history()to return recent messages for backfilling conversation context after downtime.- Parameters:
- class platforms.base.PlatformAdapter(message_handler)[source]
Bases:
ABCInterface that every platform must implement.
Subclasses wire up their SDK’s event loop, convert native events to
IncomingMessage, and forward them to the message_handler callback supplied at construction time.- Parameters:
message_handler (MessageHandler)
- __init__(message_handler)[source]
Initialize the instance.
- Parameters:
message_handler (
Callable[[IncomingMessage,PlatformAdapter],Awaitable[None]]) – The message handler value.- Return type:
None
- property bot_identity: dict[str, str]
Return the bot’s own identity on this platform.
Returns a dict with at minimum
platformanduser_id. Adapters should override to providedisplay_nameandmentionwhere available. The default returns an emptyuser_id(safe to call before login completes).
- abstractmethod async start()[source]
Connect to the service, authenticate, and begin listening.
- Return type:
- abstractmethod async send(channel_id, text)[source]
Send a plain-text message to channel_id.
Returns the platform message ID of the sent message, or
""if the send failed or the platform does not expose one.
- abstractmethod async send_file(channel_id, data, filename, mimetype='application/octet-stream')[source]
Send a file/media attachment to channel_id.
- Parameters:
channel_id (
str) – Platform-specific channel / room identifier.data (
bytes) – Raw file bytes.filename (
str) – Suggested filename for the attachment.mimetype (
str) – MIME type of the file (used for determining how to present the attachment on platforms that distinguish images, audio, video, and generic files).
- Returns:
A platform-specific content URL for the uploaded file (
mxc://on Matrix, CDN URL on Discord), orNoneif the upload failed.- Return type:
- async send_with_buttons(channel_id, text, view=None)[source]
Send a message with interactive buttons attached.
- Parameters:
- Return type:
strignore this and send plain text.
:param The default implementation falls back to
send().:
- async edit_message(channel_id, message_id, new_text)[source]
Edit an existing message sent by the bot.
- Parameters:
channel_id (
str) – Platform-specific channel / room identifier.message_id (
str) – Platform-specific ID of the message to edit.new_text (
str) – Replacement text content.success (Returns True on)
not (False if the platform does)
default (support editing or the operation failed. The)
False. (implementation is a no-op that returns)
- Return type:
- async start_typing(channel_id)[source]
Begin showing a typing indicator in channel_id.
Implementations should spawn a background task that periodically refreshes the indicator until
stop_typing()is called. The default implementation is a no-op.
- async stop_typing(channel_id)[source]
Stop showing the typing indicator in channel_id.
Must be safe to call even if
start_typing()was never called for the given channel. The default is a no-op.
- async fetch_history(channel_id, limit=100)[source]
Fetch recent messages from the platform for channel_id.
Returns up to limit messages in chronological order (oldest first). The default implementation returns an empty list; platform adapters should override when the underlying SDK supports history retrieval.
- Return type:
- Parameters:
- async get_channel_webhooks(channel_id)[source]
Return webhooks configured for channel_id.
The default implementation returns an empty list. Platform adapters that support webhooks (e.g. Discord) should override.
- async list_servers_and_channels()[source]
Return all servers/guilds and their channels.
Each platform adapter should override this to return a list of dicts describing servers/guilds (or rooms) the bot is active in, along with their channels. The format is platform-specific but should include at minimum
server_name,server_id, andchannels(a list of channel dicts).The default implementation returns an empty list.