##// END OF EJS Templates
setup: change url to github
setup: change url to github

File last commit:

r153:32f4b641
r196:472d1df0 master
Show More
slack.py
65 lines | 2.0 KiB | text/x-python | PythonLexer
project: initial commit
r0 # -*- coding: utf-8 -*-
license: change the license to Apache 2.0
r112 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # http://www.apache.org/licenses/LICENSE-2.0
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project: initial commit
r0
import logging
import requests
black: reformat source
r153 from appenlight.models.integrations import IntegrationBase, IntegrationException
project: initial commit
r0 from appenlight.lib.ext_json import json
_ = str
log = logging.getLogger(__name__)
class NotFoundException(Exception):
pass
class SlackIntegration(IntegrationBase):
black: reformat source
r153 __mapper_args__ = {"polymorphic_identity": "slack"}
project: initial commit
r0 front_visible = False
as_alert_channel = True
supports_report_alerting = True
action_notification = True
black: reformat source
r153 integration_action = "Message via Slack"
project: initial commit
r0
@classmethod
def create_client(cls, api_token):
client = SlackClient(api_token)
return client
class SlackClient(object):
def __init__(self, api_url):
self.api_url = api_url
def make_request(self, data=None):
black: reformat source
r153 headers = {"User-Agent": "appenlight-slack", "Content-Type": "application/json"}
project: initial commit
r0 try:
black: reformat source
r153 resp = getattr(requests, "post")(
self.api_url, data=json.dumps(data), headers=headers, timeout=3
)
project: initial commit
r0 except Exception as e:
black: reformat source
r153 raise IntegrationException(_("Error communicating with Slack: %s") % (e,))
project: initial commit
r0 if resp.status_code != requests.codes.ok:
black: reformat source
r153 msg = "Error communicating with Slack - status code: %s"
project: initial commit
r0 raise IntegrationException(msg % resp.status_code)
return resp
def send(self, payload):
black: reformat source
r153 return self.make_request("/rooms/message", method="post", data=payload).json()