Notifiers
Notifiers is a Python library that provides a unified and simple interface for sending notifications through various providers like Pushover, Slack, Email, and Telegram. It aims to simplify the process of integrating notifications into applications and scripts without needing to implement individual provider APIs. The current version is 1.3.6, with releases occurring periodically, focusing on new provider additions, bug fixes, and maintenance.
Warnings
- breaking The HipChat notification provider was removed in version 1.3.0. Users relying on HipChat must migrate to another provider or downgrade to an earlier version. [cite: 0.6.4 release notes, 1.3.0 release notes]
- breaking Version 0.6.4 introduced a 'Major refactor' of provider resources and a significant overhaul of the CLI. This changed how provider-specific resources (like `telegram.updates()`) are accessed and often introduced new validation requirements (e.g., needing a `token` for resource calls). The CLI syntax also fundamentally changed from `keyword=value` to standard command options. [cite: 13, 0.6.4 release notes]
- gotcha Notifiers uses JSON Schema for provider validation. If required parameters are missing or invalid, a `notifiers.exceptions.BadArguments` exception will be raised. Each provider has its own schema, which can be inspected via `notifier.schema` or `notifier.required`.
- gotcha By default, the `NotificationResponse` object returned by `notify()` methods does *not* raise an exception on API errors from the notification provider. To explicitly catch errors, you must either call `response.raise_on_errors()` or pass `raise_on_errors=True` to the `notify()` method.
Install
-
pip install notifiers
Imports
- get_notifier
from notifiers import get_notifier
- notify
from notifiers import notify
- Pushover
from notifiers.providers.pushover import Pushover
Quickstart
import os
from notifiers import get_notifier
pushover_token = os.environ.get('NOTIFIERS_PUSHOVER_TOKEN', 'YOUR_PUSHOVER_API_TOKEN')
pushover_user = os.environ.get('NOTIFIERS_PUSHOVER_USER', 'YOUR_PUSHOVER_USER_KEY')
if pushover_token and pushover_user:
pushover = get_notifier('pushover')
try:
# Ensure required parameters are provided. Check pushover.required or pushover.schema
# for details. Using raise_on_errors=True for explicit error handling.
response = pushover.notify(
token=pushover_token,
user=pushover_user,
message='Hello from notifiers!',
title='Python Notification',
raise_on_errors=True
)
if response.status == 'success':
print(f"Pushover notification sent successfully: {response.data}")
else:
print(f"Pushover notification failed: {response.errors}")
except Exception as e:
print(f"An error occurred while sending notification: {e}")
else:
print("Pushover API token and user key not set. Please set NOTIFIERS_PUSHOVER_TOKEN and NOTIFIERS_PUSHOVER_USER environment variables or hardcode them.")