Apprise: Universal Push Notifications
Apprise is a Python library that provides a common, simple interface for sending push notifications to almost any platform. It supports a vast array of services, including Slack, Discord, email, SMS, Telegram, and many more, allowing developers to integrate notifications without platform-specific code. It maintains an active development pace with several releases per year, continuously adding new services and improving existing ones. The current version is 1.9.9.
Warnings
- breaking Version 1.9.4 introduced a major code refactor for Python 3.9+ compatibility. While efforts were made to maintain backward compatibility, users running older Python versions (pre-3.9) might experience unexpected behavior or require adjustments to their code.
- gotcha Many notification services have specific optional dependencies that are not installed by default with `pip install apprise`. Forgetting to install these can lead to runtime `ImportError` exceptions when trying to use certain services.
- gotcha Notification service URLs are critical for Apprise functionality. Incorrectly formatted URLs, missing authentication tokens, or invalid endpoints are common sources of errors that can prevent notifications from being sent.
- gotcha The `Apprise` object is designed to be initialized once and reused throughout your application's lifecycle. Repeatedly creating new `Apprise` instances (e.g., inside a loop or for every notification) can lead to unnecessary overhead and inefficient resource usage.
Install
-
pip install apprise -
pip install 'apprise[all]'
Imports
- Apprise
from apprise import Apprise
- AppriseAsset
from apprise import AppriseAsset
Quickstart
import apprise
import os
# Create an Apprise instance
apobj = apprise.Apprise()
# Add a service URL.
# Use a real service URL or a dummy one like 'pypi://' for stdout output.
# Example for Slack: 'slack://tokenA/tokenB/tokenC'
# Example for Telegram: 'tgram://bottoken/chatid'
# Replace with your actual URL. Using os.environ.get for security.
slack_url = os.environ.get('APPRISE_SLACK_URL', 'pypi://')
apobj.add(slack_url)
# Send a notification
apobj.notify(
body='Hello from Apprise!',
title='Apprise Quickstart',
notify_type='info' # info, success, warning, failure
)
print(f"Notification sent (or printed to stdout if using pypi://): {slack_url}")
# Example with a file attachment (requires AppriseAsset and a valid service)
# from apprise import AppriseAsset
# try:
# with open('example.txt', 'w') as f:
# f.write('This is an example attachment.')
# apobj.add('mailto://user@example.com') # Example service supporting attachments
# apobj.notify(
# body='Message with an attachment.',
# attach=AppriseAsset('example.txt')
# )
# except IOError: # mailto will silently fail without a server
# print("Could not create example.txt or send attachment.")