fluxer-admin-alerts/notifiers/fluxer.py
Clarth 73b6d38113 Send one Fluxer webhook message per alert
Post each alert as its own webhook message instead of batching multiple embeds into a single message.
2026-11-13 20:28:16 -05:00

40 lines
1.3 KiB
Python

from __future__ import annotations
from collections.abc import Sequence
from typing import Any
import requests
from .base import Message, Notifier, NotifierError
COLOR_URGENT = 0xE74C3C
COLOR_NORMAL = 0x3498DB
USERNAME = "Admin Alerts"
class FluxerWebhookNotifier(Notifier):
def __init__(self, webhook_url: str, timeout: float) -> None:
self._url = webhook_url
self._timeout = timeout
def send(self, messages: Sequence[Message]) -> None:
for message in messages:
payload = {"username": USERNAME, "embeds": [_embed(message)]}
try:
resp = requests.post(self._url, json=payload, timeout=self._timeout)
except requests.RequestException as exc:
# The exception text contains the webhook URL, which holds the token, so only the type is kept.
raise NotifierError(f"Fluxer webhook request failed ({type(exc).__name__})") from None
if not resp.ok:
raise NotifierError(f"Fluxer webhook returned {resp.status_code}")
def _embed(message: Message) -> dict[str, Any]:
embed: dict[str, Any] = {
"title": message.title[:256],
"description": message.body[:4096],
"color": COLOR_URGENT if message.urgent else COLOR_NORMAL,
}
if message.link:
embed["url"] = message.link
return embed