dmf.alerts.slack_backend.SlackBackend#
- class dmf.alerts.slack_backend.SlackBackend(token: str, channel: str | None = None, fail_silently: bool = True)[source]#
Bases:
AlertBackendA backend for sending alerts through Slack.
This class allows you to send alerts to a specified Slack channel using a bot token. It supports sending plain text messages, as well as messages with attachments.
To use this backend, you’ll need to provide a Slack bot token and specify a channel where the alerts should be sent.
Getting a Slack Token:
Go to the Slack API page and create a new Slack App: https://api.slack.com/apps
Under “OAuth & Permissions”, generate a “Bot User OAuth Token”.
This token is required to authenticate your bot and should be passed as the token parameter.
Ensure the following OAuth scopes are granted to your app: - chat:write: Allows the bot to send messages to channels. - files:write: Allows the bot to upload and share files in channels. - chat:write.public: Allows the bot to post in public channels without being specifically invited to them.
Channel Parameter:
The channel parameter can be either the name (e.g., “#general”) or the ID (e.g., “C01234567”) of a Slack channel.
The channel can be public or private.
If you’re sending messages to a private channel, ensure that your Slack bot has been invited to that channel.
Example Usage:
To send a simple message using the Slack backend:
backend = SlackBackend(token="xoxb-your-slack-bot-token", channel="#general") backend("Hello, world!")
- Parameters:
token – The Slack bot token used for authentication. This token is necessary to send messages to Slack.
channel – Optional; The Slack channel where messages should be sent. Can be specified by name or ID. If not provided, the channel will be determined by the DMF_DEFAULT_CHANNEL environment variable.
fail_silently – Optional; If True, errors will be logged instead of raising an exception. Default is True.
- __init__(token: str, channel: str | None = None, fail_silently: bool = True)[source]#
Initialize the Slack backend with the token and default channel.
- Parameters:
token – The Slack bot token used for authentication. This token is necessary to send messages to Slack.
channel – Optional; The Slack channel where messages should be sent. Can be specified by name or ID. If not provided, the channel will be determined by the DMF_DEFAULT_CHANNEL environment variable.
fail_silently – Optional; If True, errors will be logged instead of raising an exception. Default is True.
Methods
__init__(token[, channel, fail_silently])Initialize the Slack backend with the token and default channel.
get_alert_text([text, level, params, separator])Format the notification text with the given arguments.
send_alert([text, attachment, params, level])Send a notification with a template message.
send_message([text, attachment, scheduled_time])Send a message to a Slack channel, optionally with a file attachment or scheduled time.
- get_alert_text(text: str | None = None, level: Literal['success', 'info', 'warning', 'error'] = 'info', params: dict | None = None, separator: str = '\n • ') str#
Format the notification text with the given arguments.
- send_alert(text: str | None = None, attachment: str | Path | None = None, params: dict | None = None, level: Literal['success', 'info', 'warning', 'error'] = 'info') None#
Send a notification with a template message.
- send_message(text: str = '', attachment: str | Path | None = None, scheduled_time: int | datetime | None = None) None[source]#
Send a message to a Slack channel, optionally with a file attachment or scheduled time. Raises AlertException if an error occurs, or if both attachment and scheduled_time are provided.