-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.py
128 lines (108 loc) · 3.95 KB
/
message.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
126
127
128
import json
from auth import auth_vk
vk = auth_vk()
class User(object):
def __init__(self, id):
self.id = id
self.user = vk.users.get(user_ids=self.id, fields='photo_50')[0]
def footer(self):
footer = self.user['first_name'] + ' ' + self.user['last_name']
footer_icon = self.user['photo_50']
return footer, footer_icon
class Group(object):
def __init__(self, id):
self.id = id
self.group = vk.groups.getById(group_id=self.id)[0]
def footer(self):
footer = self.group['name']
footer_icon = self.group['photo']
return footer, footer_icon
class Slack(object):
def __init__(self, post):
try:
if post['copy_history']:
self.repost = Repost(post['copy_history'][0])
self.post = Post(post)
except KeyError:
self.post = Post(post, attachments=True)
def create_attachments(self):
data_to_send = self.post.json_prepare()
try:
if self.repost:
data_repost_to_send = self.repost.json_prepare()
return json.dumps([data_to_send, data_repost_to_send])
except AttributeError:
data_to_send['mrkdwn_in'] = ['text']
return json.dumps([data_to_send])
@staticmethod
def send_message(auth, channel, text, attachments=None, as_user=True):
auth.chat.post_message(channel=channel,
text=text,
attachments=attachments,
as_user=as_user)
class Post(object):
def __init__(self, post, attachments=None):
self.text = post['text']
self.ts = post['date']
self.color = '#0093DA'
self.footer = 'Lambda'
self.footer_icon = 'https://i.imgur.com/cIcCMA7.png'
if attachments:
try:
if post['attachments']:
self.image_url, self.thumb_url = self.get_image(
post['attachments'])
except KeyError:
self.image_url, self.thumb_url = None, None
else:
self.image_url, self.thumb_url = None, None
@staticmethod
def get_image(attachments):
for attachment in attachments:
if attachment['type'] == 'photo':
image = attachment['photo']
try:
image_url = image['photo_1280']
except KeyError:
try:
image_url = image['photo_807']
except KeyError:
image_url = image['photo_604']
thumb_url = image['photo_75']
return image_url, thumb_url
else:
return None, None
def json_prepare(self):
return {
'fallback': '',
'color': self.color,
'text': self.text,
'ts': self.ts,
'footer': self.footer,
'footer_icon': self.footer_icon,
'image_url': self.image_url,
'thumb_url': self.thumb_url,
}
class Repost(Post):
def __init__(self, repost):
Post.__init__(self, post=repost)
self.text = repost['text']
self.ts = repost['date']
self.color = '#1C6047'
self.footer, self.footer_icon = self.get_footer(repost)
try:
if repost['attachments']:
self.image_url, self.thumb_url = self.get_image(
repost['attachments'])
except KeyError:
self.image_url, self.thumb_url = None, None
@staticmethod
def get_footer(post):
if post['owner_id'] < 0:
author = Group(id=str(post['owner_id'])[1:])
footer, footer_icon = author.footer()
return footer, footer_icon
else:
author = User(id=post['owner_id'])
footer, footer_icon = author.footer()
return footer, footer_icon