Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Add Discord Alert Destination #6106

Merged
merged 15 commits into from
Jul 21, 2023
21 changes: 12 additions & 9 deletions redash/destinations/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import requests

junnplus marked this conversation as resolved.
Show resolved Hide resolved
junnplus marked this conversation as resolved.
Show resolved Hide resolved
from redash.destinations import *
from redash.destinations import BaseDestination, register
from redash.utils import json_dumps

RED_ALERT_COLOR = "12597547"
junnplus marked this conversation as resolved.
Show resolved Hide resolved

GREEN_ALERT_COLOR = "2600544"
junnplus marked this conversation as resolved.
Show resolved Hide resolved


class Discord(BaseDestination):
@classmethod
Expand All @@ -15,13 +19,14 @@ def configuration_schema(cls):
"url": {"type": "string", "title": "Discord Webhook URL"}
},
"secret": ["url"],
junnplus marked this conversation as resolved.
Show resolved Hide resolved
"required": ["url"],
}

@classmethod
def icon(cls):
return "fa-discord"

def notify(self, alert, query, user, new_state, app, host, options):
def notify(self, alert, query, user, app, host, options):
# Documentation: https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html
fields = [
{
Expand All @@ -41,15 +46,15 @@ def notify(self, alert, query, user, new_state, app, host, options):
]
if alert.options.get("custom_body"):
fields.append({"name": "Description", "value": alert.options["custom_body"]})
if new_state == "triggered":
if alert.TRIGGERED_STATE == "triggered":
junnplus marked this conversation as resolved.
Show resolved Hide resolved
if alert.options.get("custom_subject"):
text = alert.options["custom_subject"]
else:
text = alert.name + " just triggered"
color = "12597547"
text = f"{alert.name} just triggered"
color = RED_ALERT_COLOR
junnplus marked this conversation as resolved.
Show resolved Hide resolved
else:
text = alert.name + " went back to normal"
color = "2600544"
text = f"{alert.name} went back to normal"
color = GREEN_ALERT_COLOR
junnplus marked this conversation as resolved.
Show resolved Hide resolved

payload = {"content": text, "embeds": [{"color": color, "fields": fields}]}
headers = {"Content-Type": "application/json"}
Expand All @@ -66,8 +71,6 @@ def notify(self, alert, query, user, new_state, app, host, options):
status=resp.status_code
)
)
else:
logging.info(msg=f"webhook send SUCCESS")
except Exception as e:
logging.exception("webhook send ERROR: %s", e)
VitalyVakhteev marked this conversation as resolved.
Show resolved Hide resolved

Expand Down