From 494bbc3f27a05d827abcbb8e427cdac3896a0906 Mon Sep 17 00:00:00 2001 From: jiriks74 Date: Wed, 15 Jan 2025 00:12:14 +0100 Subject: [PATCH] feat(ntfy): Support `ntfy.sh` servers without authentication Closes: jiriks74/nextcloud-ntfy.sh#3 --- config-example.json | 13 +++++++------ main.py | 36 +++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/config-example.json b/config-example.json index ea011b9..05cb70b 100644 --- a/config-example.json +++ b/config-example.json @@ -1,16 +1,17 @@ { - "ntfy_base_url": "https://ntfy.example.com", + "ntfy_base_url": "https://ntfy.sh", "ntfy_topic": "nextcloud", - "ntfy_token": "default_token", - + "ntfy_auth": "false", + "ntfy_token": "authentication_token", + "nextcloud_base_url": "https://nextcloud.example.com", - + "nextcloud_username": "user", "nextcloud_password": "application_password", - + "nextcloud_poll_interval_seconds": 60, "nextcloud_error_sleep_seconds": 600, "nextcloud_204_sleep_seconds": 3600, - + "rate_limit_sleep_seconds": 600 } diff --git a/main.py b/main.py index e99d590..1f44a3a 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,11 @@ import base64 from time import sleep 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 = { "DEBUG": log.DEBUG, "INFO": log.INFO, @@ -49,11 +54,15 @@ def push_to_ntfy(url: str, token: str, topic: str, title: str, click = "", messa "actions": actions } - response = requests.post(url, - data=json.dumps(jsonData), - headers={ - "Authorization": f"Bearer {token}" - }) + if token != "": + response = requests.post(url, + data=json.dumps(jsonData), + headers={ + "Authorization": f"Bearer {token}" + }) + else: + response = requests.post(url, + data=json.dumps(jsonData)) return response @@ -92,9 +101,10 @@ def arg_parser() -> argparse.Namespace: def load_config(config_file: str) -> dict: # Default values for the configuration default_config = { - "ntfy_base_url": "https://ntfy.example.com", + "ntfy_base_url": "https://ntfy.sh", "ntfy_topic": "nextcloud", - "ntfy_token": "default_token", + "ntfy_auth": "false", + "ntfy_token": "authentication_token", "nextcloud_base_url": "https://nextcloud.example.com", "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_password": "application_password", - "nextcloud_poll_interval_seconds": 10, - "nextcloud_error_sleep_seconds": 300, + "nextcloud_poll_interval_seconds": 60, + "nextcloud_error_sleep_seconds": 600, "nextcloud_204_sleep_seconds": 3600, "rate_limit_sleep_seconds": 600 @@ -118,6 +128,14 @@ def load_config(config_file: str) -> dict: for key, value in default_config.items(): if key not in config_data: config_data[key] = value + + if 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