forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/Add Discord Alert Destination (getredash#6106)
* Add discord webhook * Fix icon * Boto3 dependency * Add unit test for Discord webhook * Add suggestions * Apply suggestions from code review Co-authored-by: Jun <junnplus@gmail.com> * Misunderstood suggestion ) * Add suggestions * Apply suggestions from code review Co-authored-by: Jun <junnplus@gmail.com> * Fix test * Fix variables in strings * Fix formatting using our pre-commit hook --------- Co-authored-by: Jun <junnplus@gmail.com> Co-authored-by: Justin Clift <justin@postgresql.org>
- Loading branch information
1 parent
0ddd6f1
commit 6036d20
Showing
4 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
|
||
import requests | ||
|
||
from redash.destinations import BaseDestination, register | ||
from redash.models import Alert | ||
from redash.utils import json_dumps | ||
|
||
colors = { | ||
# Colors are in a Decimal format as Discord requires them to be Decimals for embeds | ||
Alert.OK_STATE: "2600544", # Green Decimal Code | ||
Alert.TRIGGERED_STATE: "12597547", # Red Decimal Code | ||
Alert.UNKNOWN_STATE: "16776960", # Yellow Decimal Code | ||
} | ||
|
||
|
||
class Discord(BaseDestination): | ||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
"type": "object", | ||
"properties": {"url": {"type": "string", "title": "Discord Webhook URL"}}, | ||
"secret": ["url"], | ||
"required": ["url"], | ||
} | ||
|
||
@classmethod | ||
def icon(cls): | ||
return "fa-discord" | ||
|
||
def notify(self, alert, query, user, new_state, app, host, options): | ||
# Documentation: https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html | ||
fields = [ | ||
{ | ||
"name": "Query", | ||
"value": f"{host}/queries/{query.id}", | ||
"inline": True, | ||
}, | ||
{ | ||
"name": "Alert", | ||
"value": f"{host}/alerts/{alert.id}", | ||
"inline": True, | ||
}, | ||
] | ||
if alert.options.get("custom_body"): | ||
fields.append({"name": "Description", "value": alert.options["custom_body"]}) | ||
if new_state == Alert.TRIGGERED_STATE: | ||
if alert.options.get("custom_subject"): | ||
text = alert.options["custom_subject"] | ||
else: | ||
text = f"{alert.name} just triggered" | ||
else: | ||
text = f"{alert.name} went back to normal" | ||
color = colors.get(new_state) | ||
payload = {"content": text, "embeds": [{"color": color, "fields": fields}]} | ||
headers = {"Content-Type": "application/json"} | ||
try: | ||
resp = requests.post( | ||
options.get("url"), | ||
data=json_dumps(payload), | ||
headers=headers, | ||
timeout=5.0, | ||
) | ||
if resp.status_code != 200 and resp.status_code != 204: | ||
logging.error(f"Discord send ERROR. status_code => {resp.status_code}") | ||
except Exception as e: | ||
logging.exception("Discord send ERROR: %s", e) | ||
|
||
|
||
register(Discord) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters