diff --git a/client/app/assets/images/destinations/chatwork.png b/client/app/assets/images/destinations/chatwork.png new file mode 100644 index 0000000000..2490ca39d6 Binary files /dev/null and b/client/app/assets/images/destinations/chatwork.png differ diff --git a/redash/destinations/chatwork.py b/redash/destinations/chatwork.py new file mode 100644 index 0000000000..a513aec1fc --- /dev/null +++ b/redash/destinations/chatwork.py @@ -0,0 +1,62 @@ +import json +import logging +import requests + +from redash.destinations import * + + +class ChatWork(BaseDestination): + ALERTS_DEFAULT_MESSAGE_TEMPLATE = u'{alert_name} changed state to {new_state}.\\n{alert_url}\\n{query_url}' + + @classmethod + def configuration_schema(cls): + return { + 'type': 'object', + 'properties': { + 'api_token': { + 'type': 'string', + 'title': 'API Token' + }, + 'room_id': { + 'type': 'string', + 'title': 'Room ID' + }, + 'message_template': { + 'type': 'string', + 'default': ChatWork.ALERTS_DEFAULT_MESSAGE_TEMPLATE, + 'title': 'Message Template' + } + }, + 'required': ['message_template', 'api_token', 'room_id'] + } + + @classmethod + def icon(cls): + return 'fa-comment' + + def notify(self, alert, query, user, new_state, app, host, options): + try: + # Documentation: http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-messages + url = 'https://api.chatwork.com/v2/rooms/{room_id}/messages'.format(room_id=options.get('room_id')) + + alert_url = '{host}/alerts/{alert_id}'.format(host=host, alert_id=alert.id) + query_url = '{host}/queries/{query_id}'.format(host=host, query_id=query.id) + + message_template = options.get('message_template', ChatWork.ALERTS_DEFAULT_MESSAGE_TEMPLATE) + + message = message_template.replace('\\n', '\n').format( + alert_name=alert.name, new_state=new_state.upper(), + alert_url=alert_url, + query_url=query_url) + + headers = {'X-ChatWorkToken': options.get('api_token')} + payload = {'body': message} + + resp = requests.post(url, headers=headers, data=payload) + logging.warning(resp.text) + if resp.status_code != 200: + logging.error('ChatWork send ERROR. status_code => {status}'.format(status=resp.status_code)) + except Exception: + logging.exception('ChatWork send ERROR.') + +register(ChatWork) diff --git a/redash/settings/__init__.py b/redash/settings/__init__.py index 6fba75444f..52830c510f 100644 --- a/redash/settings/__init__.py +++ b/redash/settings/__init__.py @@ -186,6 +186,7 @@ def all_settings(): 'redash.destinations.webhook', 'redash.destinations.hipchat', 'redash.destinations.mattermost', + 'redash.destinations.chatwork', ] enabled_destinations = array_from_string(os.environ.get("REDASH_ENABLED_DESTINATIONS", ",".join(default_destinations)))