Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Make the message into a configurable template.
  • Loading branch information
Kevin Chiang committed Jan 27, 2022
commit 10bb655b8f31a925fd57fbedd5f284d4c5672812
99 changes: 74 additions & 25 deletions redash/destinations/microsoft_teams_webhook.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
import logging
import requests
from string import Template

from redash.destinations import *
from redash.utils import json_dumps
from redash.serializers import serialize_alert


def json_string_substitute(j, substitutions):
"""
Alternative to string.format when the string has braces.
:param j: json string that will have substitutions
:type j: str
:param substitutions: dictionary of values to be replaced
:type substitutions: dict
"""
if substitutions:
substitution_candidate = j.replace("{", "${")
string_template = Template(substitution_candidate)
substituted = string_template.safe_substitute(substitutions)
out_str = substituted.replace("${", "{")
return out_str
else:
return j


class MicrosoftTeamsWebhook(BaseDestination):
ALERTS_DEFAULT_MESSAGE_TEMPLATE = json_dumps({
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "0076D7",
"summary": "A Redash Alert was Triggered",
"sections": [{
"activityTitle": "A Redash Alert was Triggered",
"facts": [{
"name": "Alert Name",
"value": "{alert_name}"
}, {
"name": "Alert URL",
"value": "{alert_url}"
}, {
"name": "Query",
"value": "{query_text}"
}, {
"name": "Query URL",
"value": "{query_url}"
}],
"markdown": True
}]
})


@classmethod
def name(cls):
return "Microsoft Teams Webhook"
Expand All @@ -20,7 +64,15 @@ def configuration_schema(cls):
return {
"type": "object",
"properties": {
"url": {"type": "string", "title": "Microsoft Teams Webhook URL"},
"url": {
"type": "string",
"title": "Microsoft Teams Webhook URL"
},
"message_template": {
"type": "string",
"default": MicrosoftTeamsWebhook.ALERTS_DEFAULT_MESSAGE_TEMPLATE,
"title": "Message Template",
},
},
"required": ["url"],
}
Expand All @@ -34,35 +86,32 @@ def notify(self, alert, query, user, new_state, app, host, options):
:type app: redash.Redash
"""
try:
data = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "0076D7",
"summary": "A Redash Alert was Triggered",
"sections": [{
"activityTitle": "A Redash Alert was Triggered",
"facts": [{
"name": "Alert Name",
"value": alert.name
}, {
"name": "Alert Author",
"value": user.name
}, {
"name": "Query",
"value": query.query_text
}, {
"name": "Query URL",
"value": "%s/queries/%s" % (host, query.id)
}],
"markdown": True
}]
}
alert_url = "{host}/alerts/{alert_id}".format(
host=host, alert_id=alert.id
)

query_url = "{host}/queries/{query_id}".format(
host=host, query_id=query.id
)

message_template = options.get(
"message_template", MicrosoftTeamsWebhook.ALERTS_DEFAULT_MESSAGE_TEMPLATE
)

# Doing a string Template substitution here because the template contains braces, which
# result in keyerrors when attempting string.format
payload = json_string_substitute(message_template, {
"alert_name": alert.name,
"alert_url": alert_url,
"query_text": query.query_text,
"query_url": query_url
})

headers = {"Content-Type": "application/json"}

resp = requests.post(
options.get("url"),
data=json_dumps(data),
data=payload,
headers=headers,
timeout=5.0,
)
Expand Down