pluto/core/lib/notify.py

40 lines
1.0 KiB
Python
Raw Normal View History

2023-03-09 23:27:16 +00:00
import aiohttp
2023-03-05 20:09:31 +00:00
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-03-09 23:27:16 +00:00
async def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None):
2023-03-05 20:09:31 +00:00
if url is None:
url = NTFY_URL
headers = {"Title": "Pluto"}
if title:
headers["Title"] = title
if priority:
headers["Priority"] = priority
if tags:
headers["Tags"] = tags
2023-03-09 23:27:16 +00:00
cast = {
"headers": headers,
"data": msg,
}
async with aiohttp.ClientSession() as session:
await session.post(f"{url}/{topic}", **cast)
2023-03-05 20:09:31 +00:00
# Sendmsg helper to send a message to a user's notification settings
2023-03-09 23:27:16 +00:00
async def sendmsg(user, *args, **kwargs):
2023-03-05 20:09:31 +00:00
notification_settings = user.get_notification_settings()
if notification_settings.ntfy_topic is None:
# No topic set, so don't send
return
else:
topic = notification_settings.ntfy_topic
2023-03-09 23:27:16 +00:00
await raw_sendmsg(*args, **kwargs, url=notification_settings.ntfy_url, topic=topic)