|
| 1 | +import logging |
| 2 | +import requests |
| 3 | +from string import Template |
| 4 | + |
| 5 | +from redash.destinations import * |
| 6 | +from redash.utils import json_dumps |
| 7 | +from redash.serializers import serialize_alert |
| 8 | + |
| 9 | + |
| 10 | +def json_string_substitute(j, substitutions): |
| 11 | + """ |
| 12 | + Alternative to string.format when the string has braces. |
| 13 | + :param j: json string that will have substitutions |
| 14 | + :type j: str |
| 15 | + :param substitutions: dictionary of values to be replaced |
| 16 | + :type substitutions: dict |
| 17 | + """ |
| 18 | + if substitutions: |
| 19 | + substitution_candidate = j.replace("{", "${") |
| 20 | + string_template = Template(substitution_candidate) |
| 21 | + substituted = string_template.safe_substitute(substitutions) |
| 22 | + out_str = substituted.replace("${", "{") |
| 23 | + return out_str |
| 24 | + else: |
| 25 | + return j |
| 26 | + |
| 27 | + |
| 28 | +class MicrosoftTeamsWebhook(BaseDestination): |
| 29 | + ALERTS_DEFAULT_MESSAGE_TEMPLATE = json_dumps({ |
| 30 | + "@type": "MessageCard", |
| 31 | + "@context": "http://schema.org/extensions", |
| 32 | + "themeColor": "0076D7", |
| 33 | + "summary": "A Redash Alert was Triggered", |
| 34 | + "sections": [{ |
| 35 | + "activityTitle": "A Redash Alert was Triggered", |
| 36 | + "facts": [{ |
| 37 | + "name": "Alert Name", |
| 38 | + "value": "{alert_name}" |
| 39 | + }, { |
| 40 | + "name": "Alert URL", |
| 41 | + "value": "{alert_url}" |
| 42 | + }, { |
| 43 | + "name": "Query", |
| 44 | + "value": "{query_text}" |
| 45 | + }, { |
| 46 | + "name": "Query URL", |
| 47 | + "value": "{query_url}" |
| 48 | + }], |
| 49 | + "markdown": True |
| 50 | + }] |
| 51 | + }) |
| 52 | + |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def name(cls): |
| 56 | + return "Microsoft Teams Webhook" |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def type(cls): |
| 60 | + return "microsoft_teams_webhook" |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def configuration_schema(cls): |
| 64 | + return { |
| 65 | + "type": "object", |
| 66 | + "properties": { |
| 67 | + "url": { |
| 68 | + "type": "string", |
| 69 | + "title": "Microsoft Teams Webhook URL" |
| 70 | + }, |
| 71 | + "message_template": { |
| 72 | + "type": "string", |
| 73 | + "default": MicrosoftTeamsWebhook.ALERTS_DEFAULT_MESSAGE_TEMPLATE, |
| 74 | + "title": "Message Template", |
| 75 | + }, |
| 76 | + }, |
| 77 | + "required": ["url"], |
| 78 | + } |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def icon(cls): |
| 82 | + return "fa-bolt" |
| 83 | + |
| 84 | + def notify(self, alert, query, user, new_state, app, host, options): |
| 85 | + """ |
| 86 | + :type app: redash.Redash |
| 87 | + """ |
| 88 | + try: |
| 89 | + alert_url = "{host}/alerts/{alert_id}".format( |
| 90 | + host=host, alert_id=alert.id |
| 91 | + ) |
| 92 | + |
| 93 | + query_url = "{host}/queries/{query_id}".format( |
| 94 | + host=host, query_id=query.id |
| 95 | + ) |
| 96 | + |
| 97 | + message_template = options.get( |
| 98 | + "message_template", MicrosoftTeamsWebhook.ALERTS_DEFAULT_MESSAGE_TEMPLATE |
| 99 | + ) |
| 100 | + |
| 101 | + # Doing a string Template substitution here because the template contains braces, which |
| 102 | + # result in keyerrors when attempting string.format |
| 103 | + payload = json_string_substitute(message_template, { |
| 104 | + "alert_name": alert.name, |
| 105 | + "alert_url": alert_url, |
| 106 | + "query_text": query.query_text, |
| 107 | + "query_url": query_url |
| 108 | + }) |
| 109 | + |
| 110 | + headers = {"Content-Type": "application/json"} |
| 111 | + |
| 112 | + resp = requests.post( |
| 113 | + options.get("url"), |
| 114 | + data=payload, |
| 115 | + headers=headers, |
| 116 | + timeout=5.0, |
| 117 | + ) |
| 118 | + if resp.status_code != 200: |
| 119 | + logging.error( |
| 120 | + "MS Teams Webhook send ERROR. status_code => {status}".format( |
| 121 | + status=resp.status_code |
| 122 | + ) |
| 123 | + ) |
| 124 | + except Exception: |
| 125 | + logging.exception("MS Teams Webhook send ERROR.") |
| 126 | + |
| 127 | + |
| 128 | +register(MicrosoftTeamsWebhook) |
0 commit comments