Repository navigation
Expand file tree
/
Copy pathslack_notify.py
More file actions
305 lines (222 loc) · 7.78 KB
/
Copy pathslack_notify.py
File metadata and controls
305 lines (222 loc) · 7.78 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
Module for sending messages to Slack
.. versionadded:: 2015.5.0
:configuration: This module can be used by either passing an api key and version
directly or by specifying both in a configuration profile in the salt
master/minion config.
For example:
.. code-block:: yaml
slack:
api_key: peWcBiMOS9HrZG15peWcBiMOS9HrZG15
"""
import logging
import salt.ext.six.moves.http_client
import salt.utils.json
import salt.utils.slack
from salt.exceptions import SaltInvocationError
from salt.ext.six.moves import range
# pylint: disable=import-error,no-name-in-module,redefined-builtin
from salt.ext.six.moves.urllib.parse import urlencode as _urlencode
from salt.ext.six.moves.urllib.parse import urljoin as _urljoin
# pylint: enable=import-error,no-name-in-module
log = logging.getLogger(__name__)
__virtualname__ = "slack"
def __virtual__():
"""
Return virtual name of the module.
:return: The virtual name of the module.
"""
return __virtualname__
def _get_api_key():
api_key = __salt__["config.get"]("slack.api_key") or __salt__["config.get"](
"slack:api_key"
)
if not api_key:
raise SaltInvocationError("No Slack API key found.")
return api_key
def _get_hook_id():
url = __salt__["config.get"]("slack.hook") or __salt__["config.get"]("slack:hook")
if not url:
raise SaltInvocationError("No Slack WebHook url found")
return url
def list_rooms(api_key=None):
"""
List all Slack rooms.
:param api_key: The Slack admin api key.
:return: The room list.
CLI Example:
.. code-block:: bash
salt '*' slack.list_rooms
salt '*' slack.list_rooms api_key=peWcBiMOS9HrZG15peWcBiMOS9HrZG15
"""
if not api_key:
api_key = _get_api_key()
return salt.utils.slack.query(function="rooms", api_key=api_key, opts=__opts__)
def list_users(api_key=None):
"""
List all Slack users.
:param api_key: The Slack admin api key.
:return: The user list.
CLI Example:
.. code-block:: bash
salt '*' slack.list_users
salt '*' slack.list_users api_key=peWcBiMOS9HrZG15peWcBiMOS9HrZG15
"""
if not api_key:
api_key = _get_api_key()
return salt.utils.slack.query(function="users", api_key=api_key, opts=__opts__)
def find_room(name, api_key=None):
"""
Find a room by name and return it.
:param name: The room name.
:param api_key: The Slack admin api key.
:return: The room object.
CLI Example:
.. code-block:: bash
salt '*' slack.find_room name="random"
salt '*' slack.find_room name="random" api_key=peWcBiMOS9HrZG15peWcBiMOS9HrZG15
"""
if not api_key:
api_key = _get_api_key()
# search results don't include the name of the
# channel with a hash, if the passed channel name
# has a hash we remove it.
if name.startswith("#"):
name = name[1:]
ret = list_rooms(api_key)
if ret["res"]:
rooms = ret["message"]
if rooms:
for room in range(0, len(rooms)):
if rooms[room]["name"] == name:
return rooms[room]
return False
def find_user(name, api_key=None):
"""
Find a user by name and return it.
:param name: The user name.
:param api_key: The Slack admin api key.
:return: The user object.
CLI Example:
.. code-block:: bash
salt '*' slack.find_user name="ThomasHatch"
salt '*' slack.find_user name="ThomasHatch" api_key=peWcBiMOS9HrZG15peWcBiMOS9HrZG15
"""
if not api_key:
api_key = _get_api_key()
ret = list_users(api_key)
if ret["res"]:
users = ret["message"]
if users:
for user in range(0, len(users)):
if users[user]["name"] == name:
return users[user]
return False
def post_message(channel, message, from_name, api_key=None, icon=None):
"""
Send a message to a Slack channel.
:param channel: The channel name, either will work.
:param message: The message to send to the Slack channel.
:param from_name: Specify who the message is from.
:param api_key: The Slack api key, if not specified in the configuration.
:param icon: URL to an image to use as the icon for this message
:return: Boolean if message was sent successfully.
CLI Example:
.. code-block:: bash
salt '*' slack.post_message channel="Development Room" message="Build is done" from_name="Build Server"
"""
if not api_key:
api_key = _get_api_key()
if not channel:
log.error("channel is a required option.")
# channel must start with a hash or an @ (direct-message channels)
if not channel.startswith("#") and not channel.startswith("@"):
log.warning(
"Channel name must start with a hash or @. "
'Prepending a hash and using "#%s" as '
"channel name instead of %s",
channel,
channel,
)
channel = "#{}".format(channel)
if not from_name:
log.error("from_name is a required option.")
if not message:
log.error("message is a required option.")
if not from_name:
log.error("from_name is a required option.")
parameters = {"channel": channel, "username": from_name, "text": message}
if icon is not None:
parameters["icon_url"] = icon
# Slack wants the body on POST to be urlencoded.
result = salt.utils.slack.query(
function="message",
api_key=api_key,
method="POST",
header_dict={"Content-Type": "application/x-www-form-urlencoded"},
data=_urlencode(parameters),
opts=__opts__,
)
if result["res"]:
return True
else:
return result
def call_hook(
message,
attachment=None,
color="good",
short=False,
identifier=None,
channel=None,
username=None,
icon_emoji=None,
):
"""
Send message to Slack incoming webhook.
:param message: The topic of message.
:param attachment: The message to send to the Slack WebHook.
:param color: The color of border of left side
:param short: An optional flag indicating whether the value is short
enough to be displayed side-by-side with other values.
:param identifier: The identifier of WebHook.
:param channel: The channel to use instead of the WebHook default.
:param username: Username to use instead of WebHook default.
:param icon_emoji: Icon to use instead of WebHook default.
:return: Boolean if message was sent successfully.
CLI Example:
.. code-block:: bash
salt '*' slack.call_hook message='Hello, from SaltStack'
"""
base_url = "https://hooks.slack.com/services/"
if not identifier:
identifier = _get_hook_id()
url = _urljoin(base_url, identifier)
if not message:
log.error("message is required option")
if attachment:
payload = {
"attachments": [
{
"fallback": message,
"color": color,
"pretext": message,
"fields": [{"value": attachment, "short": short}],
}
]
}
else:
payload = {
"text": message,
}
if channel:
payload["channel"] = channel
if username:
payload["username"] = username
if icon_emoji:
payload["icon_emoji"] = icon_emoji
data = _urlencode({"payload": salt.utils.json.dumps(payload)})
result = salt.utils.http.query(url, method="POST", data=data, status=True)
if result["status"] <= 201:
return True
else:
return {"res": False, "message": result.get("body", result["status"])}