Apprise Notification Library and CLI

repository·master·Indexed 12 days ago

https://github.com/caronc/apprise

A universal notification library and CLI tool that allows users to send messages to almost any popular notification service, such as Discord, Slack, Telegram, and Amazon SNS, using a single, standardized URL-based syntax. It supports asynchronous performance, attachments, and a wide array of productivity, SMS, desktop, and email notification services.

Tokens
33.1K
Snippets
109
Records
131
Agent score
96%

What's inside Apprise

  1. Overview of Apprise

    master

    Apprise is a unified notification library designed to send notifications to a wide variety of services (e.g., Telegram, Discord, Slack, Amazon SNS, Gotify) using a single, common, and intuitive syntax.

    Key features include:

    • Unified Syntax: One library to manage multiple notification services.
    • Attachment Support: Handles images and attachments (service-dependent).
    • Asynchronous Performance: Messages are sent asynchronously for high performance.
    • Lightweight: Minimal footprint.
    • Dual Use Cases:
      • Developers: Integrate a single library to gain access to many notification providers.
      • System Administrators/DevOps: Use the included apprise Command Line Interface (CLI) to send notifications from scripts or terminal environments.
  2. How to develop new Apprise plugins

    master

    When contributing new functionality:

    1. Template: Use apprise/plugins/demo.py as a template for new plugins.
    2. Testing: Write unit tests in the tests/ directory using the AppriseURLTester pattern.
    3. Requirements: All new plugins must include test coverage and pass linting to be accepted.
  3. Supported Notification Services

    master

    Apprise supports a vast array of notification services categorized into several types. For a complete and up-to-date list of all supported modules, visit the Official Documentation.

    Supported categories include:

    • Productivity Based Notifications
    • SMS Notifications
    • Desktop Notifications
    • Email Notifications
    • Custom Notifications
  4. Target specific services using tags

    master

    When adding a notification service via .add(), you can assign a tag to it. This allows you to selectively target specific groups of services during a .notify() call.

    • To notify only services with a specific tag: use tag='your_tag' in .notify().
    • To notify absolutely everything (including untagged services): use the reserved tag tag='all' in .notify().
    # Add a service with a specific tag
    apobj.add('mailto://myuser:mypass@hotmail.com', tag='admin')
    
    # Notify only the 'admin' group
    apobj.notify(body='admin message', title='Attention Admins', tag='admin')
    
    # Notify everything regardless of tags
    apobj.notify(body='broadcast', title='Hello All', tag='all')
  5. Configure Persistent Storage modes

    master

    Persistent storage allows Apprise to cache recurring actions to disk to reduce overhead.

    Operational States

    1. PersistentStoreMode.MEMORY: (Default for API) Disables writing to disk. All caching happens in memory.
    2. PersistentStoreMode.AUTO: Writes to disk on demand. (Default for CLI).
    3. PersistentStoreMode.FLUSH: Writes to disk during every transaction.

    API Configuration

    To enable persistent storage in the API, you must provide a storage_path in the AppriseAsset object. You can also configure the storage_mode and the length of the storage_idlen (default is 8).

    from apprise import Apprise, AppriseAsset, PersistentStoreMode
    
    # Enable persistent storage with specific settings
    asset = AppriseAsset(
        storage_path="/path/to/save/data",
        storage_mode=PersistentStoreMode.FLUSH,
        storage_idlen=8
    )
    
    aobj = Apprise(asset=asset)
  6. Configure SMS Notifications

    master

    SMS notifications in Apprise typically consist of a single body (usually limited to 160 characters). Because most SMS services do not support separate title and body fields, Apprise automatically combines the title and body into a single message before transmission.

    Use the specific Service ID and syntax provided for your chosen provider to configure these notifications.

  7. Filter services using CLI tags

    master

    Apprise supports tagging services in configuration files to organize them (e.g., devops, critical). Use the --tag (-g) switch to filter which services receive a notification.

    Tag Logic

    • OR Logic (Union): Specify -g multiple times to notify services that have either tag.
    • AND Logic (Intersection): Separate tags with a comma to notify services that have both tags.

    Priority and Retries

    Tags can include a priority prefix (N:tag) or a retry suffix (tag:N).

    • Escalation (no prefix): -g alerts dispatches priority-1 entries first. If they succeed, it returns early. If they fail, it triggers the next priority level as an escalation chain.
    • Exclusive (with prefix): -g "2:alerts" notifies only services whose alerts tag has priority 2.
    • Per-call retry: -g "alerts:3" retries each matched service up to 3 times for this specific call.
    • Combined: -g "2:alerts:3" uses exclusive priority-2 filter with up to 3 retries.

    Examples:

    # OR Logic: Notify 'devops' OR 'admin'
    apprise -vv -t "Union Test" --config=~/apprise.yml -g devops -g admin
    
    # AND Logic: Notify 'devops' AND 'critical'
    apprise -vv -t "Intersection Test" --config=~/apprise.yml -g devops,critical
    
    # Escalation: priority-1 first; skip priority-5 if all succeed
    apprise -vv -t "Alert" --config=~/apprise.yml -g alerts
    
    # Exclusive: only priority-2 alert services
    apprise -vv -t "Alert" --config=~/apprise.yml -g "2:alerts"
    
    # With retry override: retry each matched service up to 3 times
    apprise -vv -t "Alert" --config=~/apprise.yml -g "alerts:3"
    # OR Logic: Notify any service tagged 'devops' OR 'admin'
    apprise -vv -t "Union Test" \
       --config=~/apprise.yml \
       -g devops -g admin
  8. Lint and format Apprise code

    master

    Apprise uses Ruff for linting and formatting, configured via pyproject.toml. You can run these tasks through tox:

    • Linting: Checks for code violations.
    • Formatting: Automatically fixes formatting where possible.

    Note: Linting is enforced on all Pull Requests via GitHub Actions.

    # Run linting
    tox -e lint
    
    # Fix formatting automatically
    tox -e format
  9. Run Apprise tests with Tox

    master

    Use tox environments to run different levels of testing:

    • Full testing and plugin coverage: Use the qa environment.
    • Focused testing: Use the -k flag to filter specific tests (e.g., by keyword).
    • Minimal testing: Use the minimal environment for a faster, reduced dependency test set.
    # Full testing
    tox -e qa
    
    # Focused testing (e.g., email-related)
    tox -e qa -- -k email
    
    # Minimal dependency test set
    tox -e minimal
  10. Configure Apprise via configuration files

    master

    Instead of providing URLs manually, you can use the --config (-c) option to point to a configuration file (e.g., YAML) or a remote URL containing your notification service definitions. This allows you to manage multiple services centrally.

    # Load services from a local YAML file
    apprise -vv -t "my title" -b "my notification body" --config=~/apprise.yml
    
    # Load services from a remote configuration URL
    apprise -vv -t "my title" -b "my notification body" --config=https://localhost/my/apprise/config
  11. Rebuild Apprise man pages

    master

    Apprise man pages are generated from Markdown files in the man/*.md directory using Ronn. To regenerate the man page (e.g., man/apprise.1) after updating the source Markdown files, use the tox man target.

    # rebuild man page
    tox -e man
  12. Set up the Apprise development environment

    master

    Apprise uses tox to manage dependencies, linting, testing, and builds. Instead of manually installing requirements, use tox to orchestrate the development lifecycle.

    To get started, install tox via pip:

    python -m pip install tox