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
|
|
|
|
def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None):
|
|
|
|
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,
|
|
|
|
)
|
2023-01-15 20:27:19 +00:00
|
|
|
print("Sent notification to", url)
|
|
|
|
print("topic", topic)
|
|
|
|
print("msg", msg)
|
2023-01-12 19:00:06 +00:00
|
|
|
except requests.exceptions.ConnectionError as e:
|
|
|
|
log.error(f"Error sending notification: {e}")
|
2023-01-12 07:20:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Sendmsg helper to send a message to a user's notification settings
|
|
|
|
def sendmsg(user, *args, **kwargs):
|
|
|
|
notification_settings = user.get_notification_settings()
|
|
|
|
|
2023-01-15 18:40:17 +00:00
|
|
|
# No custom topic specified
|
2023-01-12 07:20:48 +00:00
|
|
|
if "topic" not in kwargs:
|
2023-01-15 18:40:17 +00:00
|
|
|
# No user topic set either
|
|
|
|
if notification_settings.topic is None:
|
2023-01-12 07:20:48 +00:00
|
|
|
# No topic set, so don't send
|
|
|
|
return
|
|
|
|
else:
|
2023-01-15 18:40:17 +00:00
|
|
|
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
|
2023-01-12 07:20:48 +00:00
|
|
|
|
2023-01-15 18:40:17 +00:00
|
|
|
raw_sendmsg(*args, **kwargs, url=url)
|