-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathslack.py
73 lines (66 loc) · 3 KB
/
slack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright 2023 Google LLC
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
from .base import Output, NotConfiguredException
from helpers.base import get_user_agent
import requests
import json
class SlackOutput(Output):
"""
Sends messages to Slack and call other Slack APIs too.
Args:
token (str): A Slack Bot User OAuth Token.
api (str): A Slack API call, such as chat.postMessage.
request (dict): The API call body.
"""
def output(self):
if 'token' not in self.output_config:
raise NotConfiguredException('No Slack token configured!')
if 'api' not in self.output_config:
raise NotConfiguredException('No Slack API call configured!')
if 'request' not in self.output_config:
raise NotConfiguredException('No Slack API call body configured!')
token = self._jinja_expand_string(self.output_config['token'], 'token')
api = self._jinja_expand_string(self.output_config['api'], 'api')
request = self._jinja_expand_dict_all(self.output_config['request'],
'request')
self.logger.info('Calling Slack API: %s' % (api),
extra={
"slack_api": api,
})
self.logger.debug('Calling Slack API: %s' % (api),
extra={
"slack_api": api,
"slack_request": request,
})
api_path = 'https://slack.com/api/%s' % (api)
request_body = json.dumps(request)
headers = {
'User-Agent': get_user_agent(),
'Content-type': 'application/json; charset=utf-8',
'Authorization': 'Bearer %s' % (token)
}
response = requests.post(api_path, data=request_body, headers=headers)
response.raise_for_status()
response_json = response.json()
if 'error' in response_json:
self.logger.error('Error when calling Slack API: %s',
response_json['error'],
extra={'slack_error': response_json['error']})
else:
self.logger.error('Slack API %s called successfully.' % (api),
extra={
'slack_api': api,
'slack_request': request_body,
'slack_response': response_json
})