2023-01-12 07:20:48 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from core.util import logs
|
|
|
|
|
|
|
|
NTFY_URL = "https://ntfy.sh"
|
|
|
|
|
|
|
|
log = logs.get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# Actual function to send a message to a topic
|
2023-01-16 00:10:41 +00:00
|
|
|
def ntfy_sendmsg(**kwargs):
|
|
|
|
msg = kwargs.get("msg", None)
|
|
|
|
notification_settings = kwargs.get("notification_settings")
|
|
|
|
|
2023-01-15 23:02:13 +00:00
|
|
|
title = kwargs.get("title", None)
|
2023-01-16 00:10:41 +00:00
|
|
|
priority = notification_settings.get("priority", None)
|
2023-01-15 23:02:13 +00:00
|
|
|
tags = kwargs.get("tags", None)
|
2023-01-16 00:10:41 +00:00
|
|
|
url = notification_settings.get("url") or NTFY_URL
|
|
|
|
topic = notification_settings.get("topic", None)
|
2023-01-15 23:02:13 +00:00
|
|
|
|
2023-01-12 07:20:48 +00:00
|
|
|
headers = {"Title": "Fisk"}
|
|
|
|
if title:
|
|
|
|
headers["Title"] = title
|
|
|
|
if priority:
|
|
|
|
headers["Priority"] = priority
|
|
|
|
if tags:
|
|
|
|
headers["Tags"] = tags
|
2023-01-12 19:00:06 +00:00
|
|
|
try:
|
|
|
|
requests.post(
|
|
|
|
f"{url}/{topic}",
|
|
|
|
data=msg,
|
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
except requests.exceptions.ConnectionError as e:
|
|
|
|
log.error(f"Error sending notification: {e}")
|
2023-01-12 07:20:48 +00:00
|
|
|
|
|
|
|
|
2023-01-16 00:10:41 +00:00
|
|
|
def webhook_sendmsg(**kwargs):
|
|
|
|
msg = kwargs.get("msg", None)
|
|
|
|
notification_settings = kwargs.get("notification_settings")
|
|
|
|
url = notification_settings.get("url")
|
2023-01-15 23:02:13 +00:00
|
|
|
try:
|
|
|
|
requests.post(
|
|
|
|
f"{url}",
|
|
|
|
data=msg,
|
|
|
|
)
|
|
|
|
except requests.exceptions.ConnectionError as e:
|
|
|
|
log.error(f"Error sending webhook: {e}")
|
|
|
|
|
|
|
|
|
2023-01-12 07:20:48 +00:00
|
|
|
# Sendmsg helper to send a message to a user's notification settings
|
2023-01-16 00:10:41 +00:00
|
|
|
def sendmsg(**kwargs):
|
|
|
|
user = kwargs.get("user", None)
|
|
|
|
notification_settings = kwargs.get(
|
|
|
|
"notification_settings", user.get_notification_settings().__dict__
|
|
|
|
)
|
|
|
|
if not notification_settings:
|
|
|
|
return
|
2023-01-15 18:40:17 +00:00
|
|
|
|
2023-01-16 00:10:41 +00:00
|
|
|
service = notification_settings.get("service")
|
2023-01-12 07:20:48 +00:00
|
|
|
|
2023-01-15 23:02:13 +00:00
|
|
|
if service == "ntfy":
|
2023-01-16 00:10:41 +00:00
|
|
|
ntfy_sendmsg(**kwargs)
|
2023-01-15 23:02:13 +00:00
|
|
|
elif service == "webhook":
|
2023-01-16 00:10:41 +00:00
|
|
|
webhook_sendmsg(**kwargs)
|