feat(ntfy): Support ntfy.sh servers without authentication

Closes: jiriks74/nextcloud-ntfy.sh#3
This commit is contained in:
Jiří Štefka 2025-01-15 00:12:14 +01:00
parent 8ed8cd1f84
commit 7b9161899d
Signed by: jiriks74
GPG key ID: 1D5E30D3DB2264DE
2 changed files with 36 additions and 15 deletions

View file

@ -1,7 +1,8 @@
{ {
"ntfy_base_url": "https://ntfy.example.com", "ntfy_base_url": "https://ntfy.sh",
"ntfy_topic": "nextcloud", "ntfy_topic": "nextcloud",
"ntfy_token": "default_token", "ntfy_auth": "false",
"ntfy_token": "authentication_token",
"nextcloud_base_url": "https://nextcloud.example.com", "nextcloud_base_url": "https://nextcloud.example.com",

38
main.py
View file

@ -8,6 +8,11 @@ import base64
from time import sleep from time import sleep
from datetime import datetime from datetime import datetime
# Exit codes
# - 1: Response from 'ntfy' was < 400 while pushing notification
# - 2: No ntfy authentication token was provided
# - 3: Provided 'ntfy' authentication token is invalid
log_levels = { log_levels = {
"DEBUG": log.DEBUG, "DEBUG": log.DEBUG,
"INFO": log.INFO, "INFO": log.INFO,
@ -49,11 +54,15 @@ def push_to_ntfy(url: str, token: str, topic: str, title: str, click = "", messa
"actions": actions "actions": actions
} }
response = requests.post(url, if token != "":
data=json.dumps(jsonData), response = requests.post(url,
headers={ data=json.dumps(jsonData),
"Authorization": f"Bearer {token}" headers={
}) "Authorization": f"Bearer {token}"
})
else:
response = requests.post(url,
data=json.dumps(jsonData))
return response return response
@ -92,9 +101,10 @@ def arg_parser() -> argparse.Namespace:
def load_config(config_file: str) -> dict: def load_config(config_file: str) -> dict:
# Default values for the configuration # Default values for the configuration
default_config = { default_config = {
"ntfy_base_url": "https://ntfy.example.com", "ntfy_base_url": "https://ntfy.sh",
"ntfy_topic": "nextcloud", "ntfy_topic": "nextcloud",
"ntfy_token": "default_token", "ntfy_auth": "false",
"ntfy_token": "authentication_token",
"nextcloud_base_url": "https://nextcloud.example.com", "nextcloud_base_url": "https://nextcloud.example.com",
"nextcloud_notification_path": "/ocs/v2.php/apps/notifications/api/v2/notifications", "nextcloud_notification_path": "/ocs/v2.php/apps/notifications/api/v2/notifications",
@ -102,8 +112,8 @@ def load_config(config_file: str) -> dict:
"nextcloud_username": "user", "nextcloud_username": "user",
"nextcloud_password": "application_password", "nextcloud_password": "application_password",
"nextcloud_poll_interval_seconds": 10, "nextcloud_poll_interval_seconds": 60,
"nextcloud_error_sleep_seconds": 300, "nextcloud_error_sleep_seconds": 600,
"nextcloud_204_sleep_seconds": 3600, "nextcloud_204_sleep_seconds": 3600,
"rate_limit_sleep_seconds": 600 "rate_limit_sleep_seconds": 600
@ -119,6 +129,16 @@ def load_config(config_file: str) -> dict:
if key not in config_data: if key not in config_data:
config_data[key] = value config_data[key] = value
if config_data["ntfy_auth"] == "false":
config_data["ntfy_token"] == ""
elif config_data["ntfy_auth"] == "true" and (
config_data["ntfy_token"] == "" or config_data["ntfy_token"] == "authentication_token"):
print("Error: Option 'ntfy_auth' is set to 'true' but not 'ntfy_token' was set!")
exit(2)
elif config_data["ntfy_auth"] == "true" and not config_data["ntfy_token"].startswith("tk_"):
print("Error: Authentication token set in 'ntfy_token' is invalid!")
exit(3)
return config_data return config_data
except FileNotFoundError: except FileNotFoundError: