Skip to content

xtr_slack_notifier

Slack for xtr-notifier: chat messages posted, scheduled and updated in a channel.

Install it beside xtr-notifier and a slack://TOKEN@default?channel=CHANNEL DSN is served by :class:SlackTransportFactory with no wiring. Build a message's options with :class:SlackOptions and the blocks of :mod:.block; update a posted message through the :class:SlackSentMessage the transport returns.

SlackOptions

SlackOptions(options: Mapping[str, object] | None = None)

Bases: MessageOptionsInterface

The options of a Slack message, one fluent call at a time.

Each method sets a field of chat.postMessage, and :meth:to_options renders them, in the order they were set, as the :class:~xtr_notifier.message.MessageOptions a message carries. Setting post_at schedules the message instead of posting it.

Not final: :class:~xtr_slack_notifier.UpdateMessageSlackOptions renders the same fields as an update of a message already posted.

Start from options, the Web API's fields, plus recipient_id for the channel.

Raises:

  • LogicError –

    When options holds more than 50 blocks.

recipient_id property

recipient_id: str | None

The channel the message goes to, or None for the transport's channel.

from_notification classmethod

from_notification(notification: NotificationSummary) -> SlackOptions

Render notification: its emoji as the icon, its subject, content and exception.

The exception's traceback follows a divider, as a section of its own.

to_options

to_options() -> MessageOptions

Render the fields set so far — a copy, so later calls do not change it.

recipient

recipient(recipient_id: str) -> Self

Post to the channel or user recipient_id rather than the transport's channel.

as_user

as_user(as_user: bool) -> Self

Post as the authenticated user rather than as the app.

post_at

post_at(timestamp: datetime) -> Self

Schedule the message for timestamp instead of posting it now.

block

block(block: SlackBlockInterface) -> Self

Add block to the message's layout.

Raises:

  • LogicError –

    When the message already has 50 blocks.

icon_emoji

icon_emoji(emoji: str) -> Self

Show emoji as the poster's icon.

icon_url

icon_url(url: str) -> Self

Show the image at url as the poster's icon.

link_names(link_names: bool) -> Self

Turn @names and #channels in the text into links.

mrkdwn

mrkdwn(mrkdwn: bool) -> Self

Read the text as Slack's markup, or as plain text.

parse

parse(parse: str) -> Self

Set how the text is parsed: full or none.

unfurl_links(unfurl: bool) -> Self

Preview the text's links to articles and pages.

unfurl_media

unfurl_media(unfurl: bool) -> Self

Preview the text's links to media.

username

username(username: str) -> Self

Post under username.

thread_ts

thread_ts(thread_ts: str) -> Self

Reply in the thread of the message posted at thread_ts.

SlackSentMessage

SlackSentMessage(original: MessageInterface, transport: str, channel_id: str, message_id: str)

Bases: SentMessage

A posted message, with the channel and the ts that identify it for an update.

Record original, posted through transport to channel_id at message_id.

channel_id property

channel_id: str

The id of the channel the message was posted to.

get_update_message

get_update_message(subject: str, options: Mapping[str, object] | None = None) -> ChatMessage

Return the message updating this one to subject, with options.

SlackTransport

SlackTransport(access_token: str, channel: str | None, http_client: HttpClientInterface, event_dispatcher: EventDispatcherInterface | None = None)

Bases: AbstractTransport

Posts, schedules and updates messages through Slack's Web API.

The channel a message goes to is the one its options name, else its recipient, else the channel the transport was built with. A message made from a notification and carrying no options is rendered by :meth:SlackOptions.from_notification. Options of the slack.update kind call chat.update; a post_at field calls chat.scheduleMessage; anything else chat.postMessage.

Post with access_token to channel unless a message names its own.

Parameters:

  • access_token (str) –

    A bot (xoxb-), user (xoxp-) or app (xoxa-2) token; never shown.

  • channel (str | None) –

    The channel posted to when a message names none, or None.

  • http_client (HttpClientInterface) –

    What the Web API is reached through.

  • event_dispatcher (EventDispatcherInterface | None, default: None ) –

    Where the message events go; None announces nothing.

Raises:

supports

supports(message: MessageInterface) -> bool

Send chat messages carrying no options, Slack's, or Slack update options.

SlackTransportFactory

SlackTransportFactory(http_client: HttpClientInterface, event_dispatcher: EventDispatcherInterface | None = None)

Bases: AbstractTransportFactory

Builds a :class:SlackTransport from slack://TOKEN@default?channel=CHANNEL.

default stands for slack.com; channel is the channel posted to when a message names none; ssl=false reaches the host over plain HTTP.

create

create(dsn: Dsn) -> SlackTransport

Build the transport dsn describes.

Raises:

  • UnsupportedSchemeError –

    If the scheme is not slack.

  • IncompleteDsnError –

    If the DSN carries no token.

  • InvalidArgumentError –

    If the token is not of a kind Slack issues, or ssl is not a boolean.

UpdateMessageSlackOptions

UpdateMessageSlackOptions(channel_id: str, message_id: str, options: Mapping[str, object] | None = None)

Bases: SlackOptions

The options of an update to the message posted at ts in a channel.

They render with the kind slack.update: once rendered, options are plain data that can cross a message bus, so the kind — not the builder's class — is what tells the transport to call chat.update.

Update the message message_id (its ts) in channel_id, with options.