diff --git a/matrix_alertbot/alertmanager.py b/matrix_alertbot/alertmanager.py index da1fd1b..d499ce2 100644 --- a/matrix_alertbot/alertmanager.py +++ b/matrix_alertbot/alertmanager.py @@ -14,7 +14,7 @@ class AlertmanagerClient: self.cache = cache def get_alerts(self) -> List[Dict]: - response = requests.get(f"{self.api_url}/alert") + response = requests.get(f"{self.api_url}/alerts") response.raise_for_status() return response.json() @@ -40,8 +40,8 @@ class AlertmanagerClient: silence = { "matchers": matchers, - "startsAt": start_time, - "endsAt": end_time, + "startsAt": start_time.isoformat(), + "endsAt": end_time.isoformat(), "createdBy": user, "comment": "Acknowledge alert from Matrix", } diff --git a/matrix_alertbot/bot_commands.py b/matrix_alertbot/bot_commands.py index 4fd7fe3..9a2b7a3 100644 --- a/matrix_alertbot/bot_commands.py +++ b/matrix_alertbot/bot_commands.py @@ -73,19 +73,23 @@ class Command: except KeyError: logger.debug("Unable to find the event ID of the alert") return - logger.debug(f"Read alert fingerprint for event {alert_event_id} from cache") - alert_fingerprint = self.cache[alert_event_id] - logger.debug( - f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}" - ) - silence_id = self.alertmanager.create_silence( - alert_fingerprint, duration, self.room.user_name(self.event.sender) - ) + logger.debug(f"Read alert fingerprints for event {alert_event_id} from cache") + + silence_ids = [] + alert_fingerprints = self.cache[alert_event_id] + for alert_fingerprint in alert_fingerprints: + logger.debug( + f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}" + ) + silence_id = self.alertmanager.create_silence( + alert_fingerprint, duration, self.room.user_name(self.event.sender) + ) + silence_ids.append(silence_id) + silences = ", ".join(silence_ids) await send_text_to_room( self.client, self.room.room_id, - f"Created silence {silence_id} for {duration}", - reply_to_event_id=alert_event_id, + f"Created silences {silences} for a duration of {duration}", ) async def _react(self) -> None: diff --git a/matrix_alertbot/webhook.py b/matrix_alertbot/webhook.py index 4131e9f..4f5f0b1 100644 --- a/matrix_alertbot/webhook.py +++ b/matrix_alertbot/webhook.py @@ -19,7 +19,7 @@ async def create_alert(request: web_request.Request) -> web.Response: data = await request.json() logger.info(f"Received alert: {data}") client = request.app["client"] - cache = request.app['cache'] + cache = request.app["cache"] plaintext = "" html = "" @@ -33,13 +33,14 @@ async def create_alert(request: web_request.Request) -> web.Response: html += alert.html() try: - event = await send_text_to_room(client, request.app["room_id"], plaintext, html) + event = await send_text_to_room( + client, request.app["room_id"], plaintext, html, notice=False + ) except SendRetryError as e: logger.error(e) return web.Response(status=500) cache[event.event_id] = tuple(alert["fingerprint"] for alert in data["alerts"]) - return web.Response(status=200)