msteams: MS_TEAMS_PAYLOAD: use custom json payload for msteams messagecard
and use requests.post to send json payload to msteams. If MS_TEAMS_PAYLOAD is not set continue to use pymsteams for creating/sending connectorcard.
This commit is contained in:
parent
8a2453446c
commit
ab1d4fc241
2 changed files with 54 additions and 30 deletions
plugins/msteams
|
@ -2,6 +2,7 @@
|
|||
import json
|
||||
import logging
|
||||
import os
|
||||
import requests
|
||||
import pymsteams
|
||||
|
||||
try:
|
||||
|
@ -54,6 +55,7 @@ class SendConnectorCardMessage(PluginBase):
|
|||
MS_TEAMS_WEBHOOK_URL = self.get_config('MS_TEAMS_WEBHOOK_URL', default='', type=str, **kwargs)
|
||||
MS_TEAMS_SUMMARY_FMT = self.get_config('MS_TEAMS_SUMMARY_FMT', default=None, type=str, **kwargs) # Message summary(title) format
|
||||
MS_TEAMS_TEXT_FMT = self.get_config('MS_TEAMS_TEXT_FMT', default=None, type=str, **kwargs) # Message text format
|
||||
MS_TEAMS_PAYLOAD = self.get_config('MS_TEAMS_PAYLOAD', default=None, type=str, **kwargs) # json/Jinja2 MS teams messagecard payload
|
||||
DASHBOARD_URL = self.get_config('DASHBOARD_URL', default='', type=str, **kwargs)
|
||||
|
||||
if alert.repeat:
|
||||
|
@ -64,45 +66,67 @@ class SendConnectorCardMessage(PluginBase):
|
|||
|
||||
template_vars = {
|
||||
'alert': alert,
|
||||
'config': app.config
|
||||
'config': app.config,
|
||||
'color': color,
|
||||
'url': url
|
||||
}
|
||||
|
||||
if MS_TEAMS_SUMMARY_FMT:
|
||||
template = self._load_template(MS_TEAMS_SUMMARY_FMT)
|
||||
if MS_TEAMS_PAYLOAD:
|
||||
# Use "raw" json ms teams message card format
|
||||
payload_template = self._load_template(MS_TEAMS_PAYLOAD)
|
||||
try:
|
||||
summary = template.render(**template_vars)
|
||||
card_json = payload_template.render(**template_vars)
|
||||
LOG.debug('MS Teams payload(json): %s', card_json)
|
||||
# Try to check that we've valid json
|
||||
json.loads(card_json)
|
||||
except Exception as e:
|
||||
LOG.error('MS Teams: ERROR - Template render failed: %s', e)
|
||||
LOG.error('MS Teams: ERROR - Template(PAYLOAD) render failed: %s', e)
|
||||
return
|
||||
else:
|
||||
summary = ('<b>[{status}] {environment} {service} {severity} - <i>{event} on {resource}</i></b>').format(
|
||||
status=alert.status.capitalize(),
|
||||
environment=alert.environment.upper(),
|
||||
service=','.join(alert.service),
|
||||
severity=alert.severity.capitalize(),
|
||||
event=alert.event,
|
||||
resource=alert.resource
|
||||
)
|
||||
# Use pymsteams to format/construct message card
|
||||
if MS_TEAMS_SUMMARY_FMT:
|
||||
template = self._load_template(MS_TEAMS_SUMMARY_FMT)
|
||||
try:
|
||||
summary = template.render(**template_vars)
|
||||
except Exception as e:
|
||||
LOG.error('MS Teams: ERROR - Template render failed: %s', e)
|
||||
return
|
||||
else:
|
||||
summary = ('<b>[{status}] {environment} {service} {severity} - <i>{event} on {resource}</i></b>').format(
|
||||
status=alert.status.capitalize(),
|
||||
environment=alert.environment.upper(),
|
||||
service=','.join(alert.service),
|
||||
severity=alert.severity.capitalize(),
|
||||
event=alert.event,
|
||||
resource=alert.resource
|
||||
)
|
||||
|
||||
if MS_TEAMS_TEXT_FMT:
|
||||
txt_template = self._load_template(MS_TEAMS_TEXT_FMT)
|
||||
try:
|
||||
text = txt_template.render(**template_vars)
|
||||
except Exception as e:
|
||||
LOG.error('MS Teams: ERROR - Template(TEXT_FMT) render failed: %s', e)
|
||||
return
|
||||
else:
|
||||
text = alert.text
|
||||
if MS_TEAMS_TEXT_FMT:
|
||||
txt_template = self._load_template(MS_TEAMS_TEXT_FMT)
|
||||
try:
|
||||
text = txt_template.render(**template_vars)
|
||||
except Exception as e:
|
||||
LOG.error('MS Teams: ERROR - Template(TEXT_FMT) render failed: %s', e)
|
||||
return
|
||||
else:
|
||||
text = alert.text
|
||||
|
||||
LOG.debug('MS Teams payload: %s', summary)
|
||||
LOG.debug('MS Teams payload: %s', summary)
|
||||
|
||||
try:
|
||||
msTeamsMessage = pymsteams.connectorcard(hookurl=MS_TEAMS_WEBHOOK_URL, http_timeout=MS_TEAMS_DEFAULT_TIMEOUT)
|
||||
msTeamsMessage.title(summary)
|
||||
msTeamsMessage.text(text)
|
||||
msTeamsMessage.addLinkButton("Open in Alerta", url)
|
||||
msTeamsMessage.color(color)
|
||||
msTeamsMessage.send()
|
||||
if MS_TEAMS_PAYLOAD:
|
||||
# Use requests.post to send raw json message card
|
||||
LOG.debug("MS Teams sending(json payload) POST to %s", MS_TEAMS_WEBHOOK_URL)
|
||||
r = requests.post(MS_TEAMS_WEBHOOK_URL, data=card_json, timeout=MS_TEAMS_DEFAULT_TIMEOUT)
|
||||
LOG.debug('MS Teams response: %s / %s' % (r.status_code, r.text))
|
||||
else:
|
||||
# Use pymsteams to send card
|
||||
msTeamsMessage = pymsteams.connectorcard(hookurl=MS_TEAMS_WEBHOOK_URL, http_timeout=MS_TEAMS_DEFAULT_TIMEOUT)
|
||||
msTeamsMessage.title(summary)
|
||||
msTeamsMessage.text(text)
|
||||
msTeamsMessage.addLinkButton("Open in Alerta", url)
|
||||
msTeamsMessage.color(color)
|
||||
msTeamsMessage.send()
|
||||
except Exception as e:
|
||||
raise RuntimeError("MS Teams: ERROR - %s", e)
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
version = '5.1.1'
|
||||
version = '5.2.0'
|
||||
|
||||
setup(
|
||||
name="alerta-msteams",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue