From c24d9d6953293906141b4d72f7a60882509e2431 Mon Sep 17 00:00:00 2001 From: Jeltz Date: Mon, 8 Nov 2021 23:25:44 +0100 Subject: [PATCH] Add an alternate plain text message template --- bot.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index 6d517a8..ef928cd 100755 --- a/bot.py +++ b/bot.py @@ -19,7 +19,19 @@ class Config: webhook_token: str -TEMPLATE = ( +TEMPLATE_TEXT = ( + "{% if status == 'resolved' %}" + "RÉSOLU " + "{% elif labels.severity == 'critical' %}" + "@room CRITIQUE " + "{% elif labels.severity == 'warning' %}" + "ATTENTION " + "{% endif %}" + "{{ labels.alertname }} {{ labels.instance }}\n" + "{{ annotations.summary }}" +) + +TEMPLATE_HTML = ( "{% if status == 'resolved' %}" "RÉSOLU " "{% elif labels.severity == 'critical' %}" @@ -35,14 +47,20 @@ TEMPLATE = ( async def format_messages(config, src, dest): env = jinja2.Environment() - template = env.from_string(TEMPLATE) + + template_text = env.from_string(TEMPLATE_TEXT) + template_html = env.from_string(TEMPLATE_HTML) while True: message = await src.get() for alert in message["alerts"]: - rendered = template.render(**alert) - await dest.put((rendered)) + await dest.put( + ( + template_text.render(**alert), + template_html.render(**alert), + ) + ) async def send_notices(config, messages): @@ -50,7 +68,7 @@ async def send_notices(config, messages): await client.login(config.matrix_password) while True: - formatted = await messages.get() + text, html = await messages.get() await client.room_send( config.matrix_room, @@ -58,8 +76,8 @@ async def send_notices(config, messages): content={ "msgtype": "m.text", "format": "org.matrix.custom.html", - "body": formatted, # à corriger - "formatted_body": formatted, + "body": text, + "formatted_body": html, }, )