-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathlist.py
52 lines (45 loc) · 1.67 KB
/
list.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
from copy import deepcopy as copy
from button import ButtonTemplate
ButtonTemplate.get_buttons = lambda self: self.template['attachment']['payload']['buttons']
TITLE_CHARACTER_LIMIT = 80
SUBTITLE_CHARACTER_LIMIT = 80
BUTTON_TITLE_CHARACTER_LIMIT = 20
BUTTON_LIMIT = 3
ELEMENTS_LIMIT = 4
template = {
'template_type': 'list',
'value': {
'attachment': {
'type': 'template',
'payload': {
'template_type': 'list',
'top_element_style': 'compact',
'elements': []
}
}
}
}
class ListTemplate:
def __init__(self, top_element_style=''):
self.template = copy(template['value'])
self.elements = []
if top_element_style!= '':
self.template['attachment']['payload']['top_element_style'] = top_element_style
def add_element(self, title='', image_url='', subtitle='', buttons=[], default_action={}):
element = {}
element['title'] = title[:TITLE_CHARACTER_LIMIT]
# won't render without an image_url
element['image_url'] = image_url
if subtitle != '':
element['subtitle'] = subtitle[:SUBTITLE_CHARACTER_LIMIT]
for button in buttons:
button['title'] = button['title'][:BUTTON_TITLE_CHARACTER_LIMIT]
if len(buttons) > 0:
element['buttons'] = buttons[:BUTTON_LIMIT]
if len(default_action) > 0:
element['default_action'] = default_action
if len(self.elements) < ELEMENTS_LIMIT:
self.elements.append(element)
def get_message(self):
self.template['attachment']['payload']['elements'] = self.elements
return self.template