from __future__ import annotations def build_quick_reply_items(options: list[str | dict]) -> list[dict]: items = [] for opt in options[:13]: if isinstance(opt, str): label = opt.strip() if not label: continue safe_label = label[:20] items.append( { "type": "action", "action": {"type": "message", "label": safe_label, "text": label}, } ) elif isinstance(opt, dict): label = opt.get("label", "").strip() if not label: continue safe_label = label[:20] action_type = opt.get("action_type", "message") if action_type == "postback": action = { "type": "postback", "label": safe_label, "data": opt.get("data", ""), "display_text": opt.get("display_text", safe_label), } if "input_option" in opt: action["inputOption"] = opt["input_option"] if "fill_in_text" in opt: action["fillInText"] = opt["fill_in_text"] elif action_type == "uri": action = { "type": "uri", "label": safe_label, "uri": opt.get("uri", ""), } if opt.get("alt_uri"): alt_uri = opt["alt_uri"] if isinstance(alt_uri, dict): action["altUri"] = alt_uri elif action_type == "camera": action = { "type": "camera", "label": safe_label, } elif action_type == "camera_roll": action = { "type": "cameraRoll", "label": safe_label, } elif action_type == "location": action = { "type": "location", "label": safe_label, } else: action = { "type": "message", "label": safe_label, "text": opt.get("text", label), } items.append({"type": "action", "action": action}) return items def build_text_with_quick_reply(text: str, options: list[str | dict]) -> dict: if not options: return {"type": "text", "text": text[:5000]} items = build_quick_reply_items(options) return { "type": "text", "text": text[:5000], "quickReply": {"items": items}, }