dmf.alerts.telegram_backend.TelegramBackend#
- class dmf.alerts.telegram_backend.TelegramBackend(token: str, channel: str | None = None, fail_silently: bool = True)[source]#
Bases:
AlertBackendA backend for sending alerts through Telegram.
This class allows you to send alerts to a specified Telegram 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 Telegram bot token and specify a channel (chat ID) where the alerts should be sent.
Getting a Telegram Bot Token:
Open the Telegram app and start a conversation with the BotFather (@BotFather).
Use the /newbot command to create a new bot and follow the prompts to name your bot.
After creating the bot, BotFather will provide you with a token. This token is required to authenticate your bot and should be passed as the token parameter when initializing the TelegramBackend.
Channel Parameter:
The channel parameter should be the chat ID where messages should be sent.
The chat ID can be obtained by adding the bot to the desired channel or group and retrieving the chat ID using the Telegram API or a bot command.
If you’re sending messages to a private group or channel, ensure that your Telegram bot has been added to that group or channel.
Example Usage:
To send a simple message using the Telegram backend:
backend = TelegramBackend(token="your-telegram-bot-token", channel="@your_channel_id") backend("Hello, Telegram!")
- Parameters:
token – The Telegram bot token used for authentication. This token is necessary to send messages to Telegram.
channel – Optional; The Telegram channel (chat ID) where messages should be sent. 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 Telegram backend with the token and default channel (chat ID).
- Parameters:
token – The Telegram bot token used for authentication. This token is necessary to send messages to Telegram.
channel – Optional; The Telegram channel (chat ID) where messages should be sent. 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.
- Raises:
AlertException – If the channel (chat ID) is not provided and is not set in the environment variables.
Methods
__init__(token[, channel, fail_silently])Initialize the Telegram backend with the token and default channel (chat ID).
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 Telegram channel, optionally with a file attachment.
- get_alert_text(text: str | None = None, level: Literal['success', 'info', 'warning', 'error'] = 'info', params: dict | None = None, separator: str = '\n • ') str[source]#
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 Telegram channel, optionally with a file attachment. If both attachment and scheduled_time are provided, scheduling is ignored and the message is sent immediately. Raises AlertException if an error occurs.