from __future__ import annotations def create_confirm_template( text: str, confirm_action: dict, cancel_action: dict | None = None, ) -> dict: cancel = cancel_action or {"type": "message", "label": "Cancel", "text": "cancel"} return { "type": "confirm", "text": text, "actions": [ {"type": confirm_action.get("type", "message"), **{k: v for k, v in confirm_action.items() if k != "type"}}, {"type": cancel.get("type", "message"), **{k: v for k, v in cancel.items() if k != "type"}}, ], } def create_buttons_template( title: str, text: str, actions: list[dict], *, thumbnail_image_url: str | None = None, image_aspect_ratio: str | None = None, image_size: str | None = None, default_action: dict | None = None, ) -> dict: template: dict = { "type": "buttons", "text": text[:160], "actions": actions[:4], } if title: template["title"] = title[:40] if thumbnail_image_url: template["thumbnailImageUrl"] = thumbnail_image_url if image_aspect_ratio: template["imageAspectRatio"] = image_aspect_ratio if image_size: template["imageSize"] = image_size if default_action: template["defaultAction"] = { "type": default_action.get("type", "uri"), **{k: v for k, v in default_action.items() if k != "type"}, } return template def create_carousel_template(columns: list[dict]) -> dict: return { "type": "carousel", "columns": columns[:12], } def create_image_carousel_template(columns: list[dict]) -> dict: return { "type": "image_carousel", "columns": columns[:12], } def create_carousel_column( title: str, text: str, actions: list[dict], *, thumbnail_image_url: str | None = None, image_background_color: str | None = None, default_action: dict | None = None, ) -> dict: column: dict = { "text": text[:120], "actions": actions[:3], } if title: column["title"] = title[:40] if thumbnail_image_url: column["thumbnailImageUrl"] = thumbnail_image_url if image_background_color: column["imageBackgroundColor"] = image_background_color if default_action: column["defaultAction"] = { "type": default_action.get("type", "uri"), **{k: v for k, v in default_action.items() if k != "type"}, } return column def create_yes_no_confirm(text: str, *, yes_text: str = "Yes", no_text: str = "No") -> dict: return create_confirm_template( text=text, confirm_action={"type": "message", "label": yes_text, "text": yes_text.lower()}, cancel_action={"type": "message", "label": no_text, "text": no_text.lower()}, ) def create_button_menu(title: str, text: str, buttons: list[dict]) -> dict: return create_buttons_template(title=title, text=text, actions=buttons) def create_link_menu(title: str, text: str, links: list[tuple[str, str]]) -> dict: actions = [{"type": "uri", "label": label[:20], "uri": url} for label, url in links[:4]] return create_buttons_template(title=title, text=text, actions=actions) def create_product_carousel(title: str, products: list[dict]) -> dict: columns = [] for product in products[:12]: columns.append(create_carousel_column( title=product.get("title", ""), text=product.get("text", ""), actions=product.get("actions", []), thumbnail_image_url=product.get("thumbnail_image_url"), )) return create_carousel_template(columns) def to_template_message(alt_text: str, template: dict) -> dict: return { "type": "template", "altText": alt_text[:400], "template": template, }