Skip to main content

A very small package that allows using Ntfy to send notifications

Project description

NtfyPy python version pydantic version requests version

Following the Ntfy documentations we can create a docker container which will take care of all the underlying API for us. We simply run

sudo docker run -p 80:80 -itd binwiederhier/ntfy serve

You'll have to unstall the ntfy application, and change the default server to the address of your machine, then subscribe to a topic. Sending notifications can be done with curl

curl -d "Hello" 192.168.0.xxx/topic

Keep in mind this won't work for iphone devices.

Using the package

This is the example code provided in the notification.py script

from NtfyPy.Notification import Ntfy, Notification
from NtfyPy.Decorator import NotifyRes, Notify

# Before using this application the Ntfy server should
# be already running and everything is setup in the phone 
# side. Read the documentation for more informations
# https://docs.ntfy.sh/

# This function creates a Ntfy Client and will send
# notifications to the 'test' topic here
ntfy = Ntfy("test")

# Here we're setting up the notificaiton that we
# will be sending to the user
notification = Notification(
    message = "Your bot has lost 29$ trading today",
    priority = "urgent",
    tags = "warning",
    title = "Oh ooh..."
)

# And with this one line we send a notification to the user
ntfy.send(notification)

# For convenience you can you the @Notify decorator to skip the 
# initilization of the objects
@Notify("test", "The the training is done", "Your AI model has finished training", "high")
def train():
    pass

train()

# And for even more convenience you can use @NotifyRes which
# Which notifies you about the result of running your function
@NotifyRes("test", "high")
def sum(a: int, b: int):
    return a + b

sum(5, 3)

To change the default host and port you can modify the .env file following the example provided

NTFY_HOST=localhost
NTFY_PORT=333

Use Case: Notify When Build or Deployment Finishes

You can integrate NtfyPy into your deployment script to get a phone notification when the process completes. Note here that the notify.py and error.py are the ones using the NtfyPy package

Example Bash snippet:

# Run your deployment process
your_deploy_command
DEPLOY_STATUS=$?

# Notify via ntfy
if [[ "$*" == *"--ntfy"* ]]; then
    if [[ "$DEPLOY_STATUS" == "0" ]]; then
        ./utils/notify.py my_topic
    else
        ./utils/error.py my_topic
    fi
fi

The error.py file

#!/usr/bin/python3

from NtfyPy.Notification import Notification, Ntfy
from sys import argv

# Checking if a topic was provided
if len(argv) != 2:
    print("Wrong usage of the script")
    print("usage : ")
    print("     ./notify.py [topic]")
    exit(1)


TOPIC = argv[1]
ntfy = Ntfy(TOPIC)

# Sending the notification
notification = Notification(
    message = "Error building the images",
    priority = "high",
    tags = "rotating_light",
    title = "Alexandre"
)
ntfy.send(notification)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ntfypy-0.0.4.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ntfypy-0.0.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file ntfypy-0.0.4.tar.gz.

File metadata

  • Download URL: ntfypy-0.0.4.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ntfypy-0.0.4.tar.gz
Algorithm Hash digest
SHA256 1f7019c4bafb6751f67b26966bd6f546aab80d1f5f1cca00e17dd96661e8b972
MD5 9f45e6ec1d8eb0b836cb2ad8d1fe2163
BLAKE2b-256 8ab872b8db95252cc3be347b6c0b957bd0ab0457070acc34f91600f55b34165f

See more details on using hashes here.

File details

Details for the file ntfypy-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: ntfypy-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ntfypy-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 959805fc56bffffa6b258404752e8b635bad4af3321d6a2911142ceb81ac22eb
MD5 7ffb8752f261fa120bafe1f7dff32a75
BLAKE2b-256 b6cdb48e6f64311b2825b6e4ea5f23f920fa5ca1b1a48ca86d2432879c71ef76

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page