Skip to content

Commit

Permalink
Add Hangouts Chat as alert destination (#3525)
Browse files Browse the repository at this point in the history
* 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 <pieterventer@geotab.com>

* Fix pep8 warnings. Rename image to match desitnation type.

* Show message for unknown alert state in default color
  • Loading branch information
pieter-venter authored and arikfr committed Mar 5, 2019
1 parent bc22797 commit b68051d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions redash/destinations/hangoutschat.py
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)
1 change: 1 addition & 0 deletions redash/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def all_settings():
'redash.destinations.mattermost',
'redash.destinations.chatwork',
'redash.destinations.pagerduty',
'redash.destinations.hangoutschat'
]

enabled_destinations = array_from_string(os.environ.get("REDASH_ENABLED_DESTINATIONS", ",".join(default_destinations)))
Expand Down

0 comments on commit b68051d

Please sign in to comment.