Add an alternate plain text message template

This commit is contained in:
jeltz 2021-11-08 23:25:44 +01:00
parent 3552aff21b
commit c24d9d6953
Signed by: jeltz
GPG key ID: 800882B66C0C3326

32
bot.py
View file

@ -19,7 +19,19 @@ class Config:
webhook_token: str 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' %}" "{% if status == 'resolved' %}"
"<b><font color='green'>RÉSOLU</font></b> " "<b><font color='green'>RÉSOLU</font></b> "
"{% elif labels.severity == 'critical' %}" "{% elif labels.severity == 'critical' %}"
@ -35,14 +47,20 @@ TEMPLATE = (
async def format_messages(config, src, dest): async def format_messages(config, src, dest):
env = jinja2.Environment() env = jinja2.Environment()
template = env.from_string(TEMPLATE)
template_text = env.from_string(TEMPLATE_TEXT)
template_html = env.from_string(TEMPLATE_HTML)
while True: while True:
message = await src.get() message = await src.get()
for alert in message["alerts"]: for alert in message["alerts"]:
rendered = template.render(**alert) await dest.put(
await dest.put((rendered)) (
template_text.render(**alert),
template_html.render(**alert),
)
)
async def send_notices(config, messages): async def send_notices(config, messages):
@ -50,7 +68,7 @@ async def send_notices(config, messages):
await client.login(config.matrix_password) await client.login(config.matrix_password)
while True: while True:
formatted = await messages.get() text, html = await messages.get()
await client.room_send( await client.room_send(
config.matrix_room, config.matrix_room,
@ -58,8 +76,8 @@ async def send_notices(config, messages):
content={ content={
"msgtype": "m.text", "msgtype": "m.text",
"format": "org.matrix.custom.html", "format": "org.matrix.custom.html",
"body": formatted, # à corriger "body": text,
"formatted_body": formatted, "formatted_body": html,
}, },
) )