Support sending messages when a rule no longer matches and fix dual-use notification sender

This commit is contained in:
2023-01-16 00:10:41 +00:00
parent 75603570ff
commit a2207bbcf4
6 changed files with 173 additions and 63 deletions

View File

@@ -8,12 +8,15 @@ log = logs.get_logger(__name__)
# Actual function to send a message to a topic
def ntfy_sendmsg(msg, **kwargs):
def ntfy_sendmsg(**kwargs):
msg = kwargs.get("msg", None)
notification_settings = kwargs.get("notification_settings")
title = kwargs.get("title", None)
priority = kwargs.get("priority", None)
priority = notification_settings.get("priority", None)
tags = kwargs.get("tags", None)
url = kwargs.get("url", NTFY_URL)
topic = kwargs.get("topic", None)
url = notification_settings.get("url") or NTFY_URL
topic = notification_settings.get("topic", None)
headers = {"Title": "Fisk"}
if title:
@@ -32,7 +35,10 @@ def ntfy_sendmsg(msg, **kwargs):
log.error(f"Error sending notification: {e}")
def webhook_sendmsg(msg, url):
def webhook_sendmsg(**kwargs):
msg = kwargs.get("msg", None)
notification_settings = kwargs.get("notification_settings")
url = notification_settings.get("url")
try:
requests.post(
f"{url}",
@@ -43,24 +49,17 @@ def webhook_sendmsg(msg, url):
# Sendmsg helper to send a message to a user's notification settings
def sendmsg(user, msg, **kwargs):
service = kwargs.get("service", "ntfy")
notification_settings = user.get_notification_settings()
def sendmsg(**kwargs):
user = kwargs.get("user", None)
notification_settings = kwargs.get(
"notification_settings", user.get_notification_settings().__dict__
)
if not notification_settings:
return
# No custom topic specified
if "topic" not in kwargs:
# No user topic set either
if notification_settings.topic is None:
# No topic set, so don't send
return
else:
kwargs["topic"] = notification_settings.topic
if "url" not in kwargs:
if notification_settings.url is not None:
kwargs["url"] = notification_settings.url
service = notification_settings.get("service")
if service == "ntfy":
ntfy_sendmsg(msg, **kwargs)
ntfy_sendmsg(**kwargs)
elif service == "webhook":
webhook_sendmsg(msg, kwargs["url"])
webhook_sendmsg(**kwargs)