def create_message_blocks( message_template: List[dict], message_type: MessageType, items: Optional[List] = None, **kwargs, ): """Creates all required blocks for a given message type and template.""" if not items: items = [] if kwargs: items.append(kwargs) # combine items and kwargs template_func, description = get_template(message_type) blocks = [] if description: # include optional description text (based on message type) blocks.append({"type": "section", "text": {"type": "mrkdwn", "text": description}}) for item in items: rendered_items = render_message_template(message_template, **item) blocks += template_func(rendered_items) blocks_grouped = [] if items[0].get("items_grouped"): for item in items[0]["items_grouped"]: rendered_items_grouped = render_message_template( items[0]["items_grouped_template"], **item ) blocks_grouped += template_func(rendered_items_grouped) return blocks + blocks_grouped
def create_message_body(message_template: dict, message_type: MessageType, **kwargs): """Creates the correct message body based on message type.""" template, description = get_template(message_type) rendered = render_message_template(message_template, **kwargs) kwargs.update({"items": rendered, "description": description}) return render_html(template.render(**kwargs))
def create_message_body(message_template: dict, message_type: MessageType, **kwargs): """Creates the correct message body based on message type.""" template, description = get_template(message_type) items_grouped_rendered = [] if kwargs.get("items_grouped"): items_grouped_template = kwargs["items_grouped_template"] for item in kwargs["items_grouped"]: item_rendered = render_message_template(items_grouped_template, **item) items_grouped_rendered.append(item_rendered) kwargs.update({"items": items_grouped_rendered, "description": description}) return render_html(template.render(**kwargs)) items_rendered = render_message_template(message_template, **kwargs) kwargs.update({"items": items_rendered, "description": description}) return render_html(template.render(**kwargs))
def create_multi_message_body(message_template: dict, message_type: MessageType, items: list, **kwargs): """Creates a multi message message body based on message type.""" template, description = get_template(message_type) master_map = [] for item in items: master_map.append(render_message_template(message_template, **item)) kwargs.update({"items": master_map, "description": description}) return render_html(template.render(**kwargs))