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
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' %}"
"<b><font color='green'>RÉSOLU</font></b> "
"{% 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,
},
)