Finish implementing webhook delivery

This commit is contained in:
2023-01-15 23:02:13 +00:00
parent 2dd9efcc6f
commit 75603570ff
7 changed files with 144 additions and 31 deletions

View File

@@ -8,7 +8,13 @@ log = logs.get_logger(__name__)
# Actual function to send a message to a topic
def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None):
def ntfy_sendmsg(msg, **kwargs):
title = kwargs.get("title", None)
priority = kwargs.get("priority", None)
tags = kwargs.get("tags", None)
url = kwargs.get("url", NTFY_URL)
topic = kwargs.get("topic", None)
headers = {"Title": "Fisk"}
if title:
headers["Title"] = title
@@ -22,15 +28,23 @@ def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None)
data=msg,
headers=headers,
)
print("Sent notification to", url)
print("topic", topic)
print("msg", msg)
except requests.exceptions.ConnectionError as e:
log.error(f"Error sending notification: {e}")
def webhook_sendmsg(msg, url):
try:
requests.post(
f"{url}",
data=msg,
)
except requests.exceptions.ConnectionError as e:
log.error(f"Error sending webhook: {e}")
# Sendmsg helper to send a message to a user's notification settings
def sendmsg(user, *args, **kwargs):
def sendmsg(user, msg, **kwargs):
service = kwargs.get("service", "ntfy")
notification_settings = user.get_notification_settings()
# No custom topic specified
@@ -42,11 +56,11 @@ def sendmsg(user, *args, **kwargs):
else:
kwargs["topic"] = notification_settings.topic
if "url" in kwargs:
url = kwargs["url"]
elif notification_settings.url is not None:
url = notification_settings.url
else:
url = NTFY_URL
if "url" not in kwargs:
if notification_settings.url is not None:
kwargs["url"] = notification_settings.url
raw_sendmsg(*args, **kwargs, url=url)
if service == "ntfy":
ntfy_sendmsg(msg, **kwargs)
elif service == "webhook":
webhook_sendmsg(msg, kwargs["url"])