-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.py
69 lines (50 loc) · 1.65 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
import json
import requests
class Slack:
baseUrl = ""
def __init__(self, baseUrl):
self.baseUrl = baseUrl
def notify(self, message, iconUrl=None):
payload={"text": message, "unfurl_links": True, "mrkdwn": True}
if (iconUrl is not None):
payload["icon_url"] = iconUrl
jsonData = json.dumps(payload)
response = requests.post(
self.baseUrl,
data=jsonData,
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
class SlackMessage(object):
_webhookUrl = ""
_package = ":package: "
_constructor = ":male-construction-worker: "
_release = "> "
_link = ":books: "
iconUrl = None
_message = []
def __init__(self, webhookUrl):
self._webhookUrl = webhookUrl
def save(self):
self._message.append(self._package)
self._message.append(self._constructor)
self._message.append(self._release)
self._message.append(self._link)
def __repr__(self):
return "\n".join(self._message)
def newLine(self, value):
self._message.append(value)
def package(self, value):
self._package+=value
def constructor(self, value):
self._constructor+=value
def link(self, value):
self._link+=value
def release(self, value):
self._release+=value
def notify(self):
Slack(self._webhookUrl).notify(str(self), self.iconUrl)