Skip to content

added template for list #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions templates/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a limit of 1. Can you check?

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only for the first item right, and when it's not compact.

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