2025-07-25 21:32:06 +00:00
|
|
|
from abc import abstractmethod
|
|
|
|
|
import apprise
|
|
|
|
|
from functools import lru_cache
|
2025-07-26 13:15:21 +00:00
|
|
|
import os
|
2025-07-25 21:32:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Surface:
|
|
|
|
|
@abstractmethod
|
2025-07-26 13:15:21 +00:00
|
|
|
def connection_string(self) -> str | None: ...
|
|
|
|
|
|
2025-07-25 21:32:06 +00:00
|
|
|
|
|
|
|
|
class Slack(Surface):
|
|
|
|
|
def connection_string(self) -> str | None:
|
2025-07-26 13:15:21 +00:00
|
|
|
return os.environ.get("SLACK_WEBHOOK_URL")
|
2025-07-25 21:32:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache(maxsize=None)
|
|
|
|
|
def get_notifier(surfaces: list[Surface] | None = None) -> apprise.Apprise:
|
2025-07-27 18:33:39 +00:00
|
|
|
surfaces = surfaces or list[Surface]([Slack()])
|
2025-07-25 21:32:06 +00:00
|
|
|
obj = apprise.Apprise()
|
|
|
|
|
for surface in surfaces:
|
2025-07-26 13:15:21 +00:00
|
|
|
if conn := surface.connection_string():
|
|
|
|
|
obj.add(conn)
|
2025-07-25 21:32:06 +00:00
|
|
|
return obj
|
|
|
|
|
|
2025-07-26 13:15:21 +00:00
|
|
|
|
|
|
|
|
async def send_notification(body: str, title: str = "") -> bool:
|
2025-07-25 21:32:06 +00:00
|
|
|
notifier = get_notifier()
|
|
|
|
|
return await notifier.async_notify(body=body, title=title)
|