-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathslack.py
125 lines (102 loc) · 3.37 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Library for interacting with Slack API
.. versionadded:: 2016.3.0
:configuration: This module can be used by specifying the name of a
configuration profile in the minion config, minion pillar, or master
config.
For example:
.. code-block:: yaml
slack:
api_key: peWcBiMOS9HrZG15peWcBiMOS9HrZG15
"""
import logging
import salt.ext.six.moves.http_client
# pylint: enable=import-error,no-name-in-module
import salt.utils.http
# pylint: disable=import-error,no-name-in-module,redefined-builtin
from salt.ext.six.moves.urllib.parse import urljoin as _urljoin
from salt.version import __version__
log = logging.getLogger(__name__)
def query(
function,
api_key=None,
args=None,
method="GET",
header_dict=None,
data=None,
opts=None,
):
"""
Slack object method function to construct and execute on the API URL.
:param api_key: The Slack api key.
:param function: The Slack api function to perform.
:param method: The HTTP method, e.g. GET or POST.
:param data: The data to be sent for POST method.
:return: The json response from the API call or False.
"""
ret = {"message": "", "res": True}
slack_functions = {
"rooms": {"request": "channels.list", "response": "channels"},
"users": {"request": "users.list", "response": "members"},
"message": {"request": "chat.postMessage", "response": "channel"},
}
if not api_key:
api_key = __salt__["config.get"]("slack.api_key") or __salt__["config.get"](
"slack:api_key"
)
if not api_key:
log.error("No Slack api key found.")
ret["message"] = "No Slack api key found."
ret["res"] = False
return ret
api_url = "https://slack.com"
base_url = _urljoin(api_url, "/api/")
path = slack_functions.get(function).get("request")
url = _urljoin(base_url, path, False)
if not isinstance(args, dict):
query_params = {}
else:
query_params = args.copy()
query_params["token"] = api_key
if header_dict is None:
header_dict = {}
if method != "POST":
header_dict["Accept"] = "application/json"
result = salt.utils.http.query(
url,
method,
params=query_params,
data=data,
decode=True,
status=True,
header_dict=header_dict,
opts=opts,
)
if result.get("status", None) == salt.ext.six.moves.http_client.OK:
_result = result["dict"]
response = slack_functions.get(function).get("response")
if "error" in _result:
ret["message"] = _result["error"]
ret["res"] = False
return ret
ret["message"] = _result.get(response)
return ret
elif result.get("status", None) == salt.ext.six.moves.http_client.NO_CONTENT:
return True
else:
log.debug(url)
log.debug(query_params)
log.debug(data)
log.debug(result)
if "dict" in result:
_result = result["dict"]
if "error" in _result:
ret["message"] = result["error"]
ret["res"] = False
return ret
ret["message"] = "Unknown response"
ret["res"] = False
else:
ret["message"] = "invalid_auth"
ret["res"] = False
return ret