A very small package that allows using Ntfy to send notifications
Project description
NtfyPy

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)
The CLI tool
The ntfypy CLI tool is made to notify you when executing a command is done, you provide it withe a topic and a command it executes the command, and when it's donoe it broadcasts a notification to the topic provided.
ntfypy [topic] [command]
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ntfypy-0.0.5.tar.gz.
File metadata
- Download URL: ntfypy-0.0.5.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c82ca57705b98200a92a6618a06d8dbfebcaa219ba5f53c1589d3dcfaf22ea6
|
|
| MD5 |
bc38bb2303461cbaf327410424485e65
|
|
| BLAKE2b-256 |
ce7bb004928da02eda1558ecc490a9031ba7da167009dcbd0c68850dcaedd6a2
|
File details
Details for the file ntfypy-0.0.5-py3-none-any.whl.
File metadata
- Download URL: ntfypy-0.0.5-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8392239b95babea9454faa327b9a42d482864d93a9df6bb7861d00a97e8166c
|
|
| MD5 |
a941e26852ca5f9bc42df5f87c3b32ca
|
|
| BLAKE2b-256 |
4d627b9ae1aa75f64d2cf76ba2372cee67c27ab7839306692b48b7e9e73b29a6
|