Python API#

You can post to Slack from your own code, without a content rule.

from contentrules.slack.slack_notifier import notify_slack

notify_slack(
    "https://hooks.slack.com/services/T.../B.../...",
    text="The nightly import finished.",
)

Every keyword argument other than webhook_url, timeout, and verify becomes part of the JSON payload. See Slack's message payload reference for the keys Slack accepts.

Notifier#

contentrules.slack.slack_notifier.notify_slack(webhook_url: str = '', timeout: int = 2, verify: bool = True, **payload: Any) → Thread | None[source]#

Post a message to Slack using the registered ISlackNotifier.

Always use this function to send Slack notifications, so a replacement utility is honored.

Parameters:
  • webhook_url -- The Slack webhook URL. When empty, the value of the SLACK_WEBHOOK_URL environment variable is used instead.

  • timeout -- Seconds to wait for Slack before giving up.

  • verify -- Whether to verify SSL certificates.

  • payload -- Sent as the JSON payload of the request.

Returns:

The thread performing the request, or None when the notification is deactivated.

contentrules.slack.slack_notifier.NOTIFICATION_DEACTIVATION_VALUE#

Use this value, as a webhook URL or in the DEACTIVATE_SLACK_NOTIFICATION environment variable, to deactivate notifications.

class contentrules.slack.interfaces.ISlackNotifier[source]#

A utility posting messages into a Slack channel through a webhook.

Its notify method has the signature of SlackNotifier.notify(). Register a utility providing this interface to replace how messages are sent.

__init__(name, bases=(), attrs=None, __doc__=None, __module__=None)#
__new__(name=None, bases=(), attrs=None, __doc__=None, __module__=None)#
class contentrules.slack.slack_notifier.SlackNotifier[source]#

Default ISlackNotifier, posting to a webhook in a thread.

THREAD_NAME = 'SlackNotifier-Thread'#

Name of the thread performing the request.

__init__()#
classmethod __new__(*args, **kwargs)#
notify(webhook_url: str = '', timeout: int = 2, verify: bool = True, **payload: Any) → Thread | None[source]#

Post a message to a Slack webhook in a background thread.

Parameters:
  • webhook_url -- The Slack webhook URL. When empty, the value of the SLACK_WEBHOOK_URL environment variable is used instead.

  • timeout -- Seconds to wait for Slack before giving up.

  • verify -- Whether to verify SSL certificates.

  • payload -- Sent as the JSON payload of the request.

Returns:

The started thread, or None when the notification is deactivated -- globally, or because no webhook URL is available.

Helpers#

contentrules.slack.utils.extract_fields_from_text(text: str) → list[dict][source]#

Parse attachment field definitions, one per line.

Each line has the format title|value|short, and short is true or false (case-insensitive; anything else reads as false). A line without exactly three parts is skipped:

Title|${title}|true
Review State|${review_state_title}|false
Parameters:

text -- Field definitions, one per line.

Returns:

One {"title", "value", "short"} dictionary per valid line.

Logging#

contentrules.slack writes to the contentrules.slack logger.

When a request to Slack fails, it logs the following message at the ERROR level.

Slack notification to channel <channel> failed: <error>
<channel>

The channel of the payload, or (default) when the payload has none.

<error>

The class of the error, such as HTTPError for an error status from Slack, Timeout, or ConnectionError.

The message never includes the webhook URL.