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

Add webex as destination #5574

Merged
merged 25 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added client/app/assets/images/destinations/webex.png
guidopetri marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 158 additions & 0 deletions redash/destinations/webex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import logging
import requests

from redash.destinations import *


class Webex(BaseDestination):
@classmethod
def configuration_schema(cls):
return {
"type": "object",
"properties": {
"webex_bot_token": {"type": "string", "title": "Webex Bot Token"},
"to_person_emails": {
"type": "string",
"title": "People (comma-separated)",
},
"to_room_ids": {
"type": "string",
"title": "Rooms (comma-separated)",
},
"organization": {"type": "string", "title": "Organization"},
},
"secret": ["webex_bot_token"],
guidopetri marked this conversation as resolved.
Show resolved Hide resolved
}

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

def notify(self, alert, query, user, new_state, app, host, options):
guidopetri marked this conversation as resolved.
Show resolved Hide resolved
# Documentation: https://developer.webex.com/docs/api/guides/cards

query_link = "{host}/queries/{query_id}".format(host=host, query_id=query.id)
alert_link = "{host}/alerts/{alert_id}".format(host=host, alert_id=alert.id)
description = alert.custom_body if alert.custom_body else ""

if new_state == "triggered":
if alert.custom_subject:
subject = alert.custom_subject
else:
subject = alert.name + " just triggered"
else:
subject = alert.name + " went back to normal"

attachments = [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": 4,
"items": [
{
"type": "TextBlock",
"text": subject,
"weight": "bolder",
"size": "medium",
"wrap": True,
},
{
"type": "TextBlock",
"text": description,
"isSubtle": True,
"wrap": True,
},
{
"type": "TextBlock",
"text": f"Click [here]({query_link}) to check your query!",
"wrap": True,
"isSubtle": True,
},
{
"type": "TextBlock",
"text": f"Click [here]({alert_link}) to check your alert!",
"wrap": True,
"isSubtle": True,
},
],
},
{
"type": "Column",
"width": 1,
"items": [
{
"type": "Image",
"url": "https://cdn.pixabay.com/photo/2016/09/16/09/20/alarm-1673577_960_720.png",
guidopetri marked this conversation as resolved.
Show resolved Hide resolved
"size": "medium",
}
],
},
],
}
],
},
}
]

payload = {"markdown": subject + "\n" + description, "attachments": attachments}

headers = {"Authorization": "Bearer {}".format(options.get("webex_bot_token"))}

to_person_ids = (
options.get("to_person_emails").split(",")
if options.get("to_person_emails")
else []
)
to_room_ids = (
options.get("to_room_ids").split(",") if options.get("to_room_ids") else []
)

for to_person_email in to_person_ids:
try:
payload["toPersonEmail"] = to_person_email
resp = requests.post(
"https://webexapis.com/v1/messages",
json=payload,
headers=headers,
timeout=5.0,
)
logging.warning(resp.text)
if resp.status_code != 200:
logging.error(
"Webex send ERROR. status_code => {status}".format(
status=resp.status_code
)
)
except Exception:
logging.exception("Webex send ERROR.")
guidopetri marked this conversation as resolved.
Show resolved Hide resolved

for to_room_id in to_room_ids:
try:
payload["roomId"] = to_room_id
resp = requests.post(
"https://webexapis.com/v1/messages",
json=payload,
headers=headers,
timeout=5.0,
)
logging.warning(resp.text)
if resp.status_code != 200:
logging.error(
"Webex send ERROR. status_code => {status}".format(
status=resp.status_code
)
)
except Exception:
logging.exception("Webex send ERROR.")


register(Webex)
1 change: 1 addition & 0 deletions redash/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def email_server_is_configured():
"redash.destinations.pagerduty",
"redash.destinations.hangoutschat",
"redash.destinations.microsoft_teams_webhook",
"redash.destinations.webex",
]

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