-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hangouts Chat as alert destination (#3525)
* Add support for Google Hangouts Chat as alert destination * Remove redundant imports * Remove code used for debugging * Fix pep8 warnings * Update redash/destinations/hangoutschat.py Add friendly name by separating type and description Co-Authored-By: pieter-venter <[email protected]> * Fix pep8 warnings. Rename image to match desitnation type. * Show message for unknown alert state in default color
- Loading branch information
1 parent
bc22797
commit b68051d
Showing
3 changed files
with
97 additions
and
0 deletions.
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,96 @@ | ||
import logging | ||
import requests | ||
|
||
from redash.destinations import * | ||
from redash.utils import json_dumps | ||
|
||
|
||
class HangoutsChat(BaseDestination): | ||
@classmethod | ||
def name(cls): | ||
return "Google Hangouts Chat" | ||
|
||
@classmethod | ||
def type(cls): | ||
return "hangouts_chat" | ||
|
||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
"type": "object", | ||
"properties": { | ||
"url": { | ||
"type": "string", | ||
"title": "Webhook URL (get it from the room settings)" | ||
}, | ||
"icon_url": { | ||
"type": "string", | ||
"title": "Icon URL (32x32 or multiple, png format)" | ||
} | ||
}, | ||
"required": ["url"] | ||
} | ||
|
||
@classmethod | ||
def icon(cls): | ||
return 'fa-bolt' | ||
|
||
def notify(self, alert, query, user, new_state, app, host, options): | ||
try: | ||
if new_state == "triggered": | ||
message = "<b><font color=\"#c0392b\">Triggered</font></b>" | ||
elif new_state == "ok": | ||
message = "<font color=\"#27ae60\">Went back to normal</font>" | ||
else: | ||
message = "Unable to determine status. Check Query and Alert configuration." | ||
|
||
data = { | ||
"cards": [ | ||
{ | ||
"header": { | ||
"title": alert.name | ||
}, | ||
"sections": [ | ||
{ | ||
"widgets": [ | ||
{ | ||
"textParagraph": { | ||
"text": message | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
||
if options.get("icon_url"): | ||
data["cards"][0]["header"]["imageUrl"] = options.get("icon_url") | ||
|
||
# Hangouts Chat will create a blank card if an invalid URL (no hostname) is posted. | ||
if host: | ||
data["cards"][0]["sections"][0]["widgets"].append({ | ||
"buttons": [ | ||
{ | ||
"textButton": { | ||
"text": "OPEN QUERY", | ||
"onClick": { | ||
"openLink": { | ||
"url": "{host}/queries/{query_id}".format(host=host, query_id=query.id) | ||
} | ||
} | ||
} | ||
} | ||
] | ||
}) | ||
|
||
headers = {"Content-Type": "application/json; charset=UTF-8"} | ||
resp = requests.post(options.get("url"), data=json_dumps(data), headers=headers, timeout=5.0) | ||
if resp.status_code != 200: | ||
logging.error("webhook send ERROR. status_code => {status}".format(status=resp.status_code)) | ||
except Exception: | ||
logging.exception("webhook send ERROR.") | ||
|
||
|
||
register(HangoutsChat) |
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