-
Notifications
You must be signed in to change notification settings - Fork 8
/
slack.py
82 lines (69 loc) · 2.26 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
import config
import json
import urllib2
import urllib
import logging
class Attachment(object):
def __init__(self, value, fallback=None, text=None, pretext=None, color=None, title=None, short=None):
self.value = value
self.fallback=fallback
self.text=text
self.pretext=pretext
self.color=color
self.title=title
self.short = True if short else False
def render(self):
attachment = {
'fallback': self.fallback,
'text': self.text,
'pretext': self.pretext,
'color': self.color,
'fields': [{
'title': self.title,
'value': self.value,
'short': self.short,
}]
}
for obj in [attachment['fields'][0], attachment]:
for key in obj.keys():
if not obj[key]:
del obj[key]
return attachment
class BaseSlack:
def post(self, text, channel=None, username=None, icon_emoji=None, attachments=None):
pass
def render_webhook_payload(self, text, channel=None, username=None, icon_emoji=None, attachments=None):
if attachments is None:
attachments = []
payload = {
'text': text,
'channel': channel,
'username': username,
'icon_emoji': icon_emoji,
'attachments': map(lambda attachment: attachment.render(), attachments),
'link_names': 1
}
for key in payload.keys():
if not payload[key]:
del payload[key]
return payload
class TestSlack(BaseSlack):
def __init__(self):
self.last_payload = None
def post(self, text, channel=None, username=None, icon_emoji=None, attachments=None):
self.last_payload = self.render_webhook_payload(text, channel, username, icon_emoji, attachments)
class Slack(BaseSlack):
def post(self, text, channel=None, username=None, icon_emoji=None, attachments=None):
payload = self.render_webhook_payload(text, channel, username, icon_emoji, attachments)
logging.info(payload)
encoded_data = urllib.urlencode({'payload': json.dumps(payload)})
logging.info(encoded_data)
cfg = config.get_config()
res = urllib2.urlopen(
'https://{0}.slack.com/services/hooks/incoming-webhook?token={1}'.format(
cfg.team_domain,
cfg.incoming_webhook_token),
encoded_data,
2.0)
logging.info(res.getcode())
return res