328532bcbb
- button_builder.py: _make_action_id() Helper + action_id in Regular/Special Buttons
- __init__.py _build_custom_buttons: action_id via SHA1(msg_payload:idx)
- adapter.py _get_build_custom_buttons local fallback: action_id ergänzt
- adapter.py send_clarify: action_id=f'clarify_{clarify_id}_{i}' (deterministisch)
- adapter.py _send_archivar_buttons: action_id via SHA1(selector:i)
Rocket.Chat 6.x ignoriert das actions-Array komplett wenn kein action_id-Feld
vorhanden ist. Buttons erscheinen als plain-text statt klickbar.
122 lines
4.4 KiB
Python
122 lines
4.4 KiB
Python
"""
|
||
Gemeinsamer Button-Builder für Onboarding und Review-Flow.
|
||
|
||
Extrahiert aus onboarding_review.py (P0-1), damit adapter.py und
|
||
onboarding_review.py die GLEICHE Button-Konstruktion nutzen — kein Drift.
|
||
|
||
Regeln:
|
||
- Nummerierte Buttons: max 3 pro Attachment, mit 1️⃣ 2️⃣ 3️⃣...
|
||
- Sonderschaltflächen (↩️ Zurück, ⏭️ Skip, ➕ Neu): KEINE Nummer,
|
||
IMMER in separatem (letztem) Attachment
|
||
- Jedes Attachment hat max 3 actions
|
||
"""
|
||
|
||
import hashlib
|
||
from typing import Any, List, Tuple
|
||
|
||
# Emoji-Nummern für bis zu 10 Buttons
|
||
EMOJI_NUMBERS = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"]
|
||
|
||
|
||
def _make_action_id(msg_payload: str, index: int = 0) -> str:
|
||
"""Deterministische action_id aus msg-Payload (SHA1, 12 Zeichen).
|
||
|
||
Rocket.Chat 6.x benötigt zwingend ein action_id-Feld im Button-Dict,
|
||
sonst wird das gesamte actions-Array ignoriert (P-051).
|
||
"""
|
||
raw = f"{msg_payload}:{index}".encode()
|
||
return hashlib.sha1(raw).hexdigest()[:12]
|
||
|
||
|
||
def build_button_attachments(
|
||
num_buttons: List[Tuple[Any, ...]],
|
||
special_buttons: List[Tuple[Any, ...]],
|
||
color: str = "#1d74f5",
|
||
title: str = "",
|
||
number_buttons: bool = True,
|
||
button_style: str = "primary",
|
||
) -> List[dict]:
|
||
"""Baut Attachments nach Andreas' 2-Reihen-Button-Pattern.
|
||
|
||
Regeln:
|
||
- Nummerierte Buttons: max 3 pro Attachment, mit 1️⃣ 2️⃣ 3️⃣...
|
||
(Nummerierung kann via number_buttons=False deaktiviert werden)
|
||
- Sonderschaltflächen (↩️ Zurück, ⏭️ Skip, ➕ Neu): KEINE Nummer,
|
||
IMMER in separatem (letztem) Attachment
|
||
- Jedes Attachment hat max 3 actions
|
||
|
||
P0-Fix: msg-Payload mit '::' Suffix für Button-Klick-Erkennung.
|
||
num_buttons: (label, prefix) oder (label, prefix, suffix)
|
||
special_buttons: (label, prefix, style) oder (label, prefix, suffix, style)
|
||
Wenn suffix vorhanden: msg = f'{prefix}::{suffix}'
|
||
Sonst (backward-compat): msg = prefix
|
||
|
||
Args:
|
||
num_buttons: Liste von (label, prefix[, suffix]) Tuples
|
||
special_buttons: Liste von (label, prefix, style[, suffix]) Tuples
|
||
color: Attachment-Farbe (default: "#1d74f5")
|
||
title: Attachment-Titel (nur für erstes Attachment)
|
||
number_buttons: Wenn False, werden Buttons NICHT nummeriert
|
||
(z.B. für X-Buttons in Keywords-Edit)
|
||
button_style: Rocket.Chat button style für num_buttons (default: "primary")
|
||
|
||
Returns:
|
||
Liste von Attachment-Dicts (jedes mit max 3 actions)
|
||
"""
|
||
attachments: List[dict] = []
|
||
|
||
# Nummerierte Buttons in 3er-Blöcke aufteilen
|
||
for i in range(0, len(num_buttons), 3):
|
||
chunk = num_buttons[i:i + 3]
|
||
actions = []
|
||
for j, tup in enumerate(chunk):
|
||
label = tup[0]
|
||
prefix = tup[1]
|
||
suffix = tup[2] if len(tup) >= 3 else None
|
||
msg_payload = f"{prefix}::{suffix}" if suffix else prefix
|
||
if number_buttons:
|
||
num_idx = i + j
|
||
emoji = EMOJI_NUMBERS[num_idx] if num_idx < len(EMOJI_NUMBERS) else str(num_idx + 1)
|
||
text = f"{emoji} {label}"
|
||
else:
|
||
text = label
|
||
actions.append({
|
||
"type": "button",
|
||
"text": text,
|
||
"msg": msg_payload,
|
||
"msg_in_chat_window": True,
|
||
"style": button_style,
|
||
"action_id": _make_action_id(msg_payload, i + j),
|
||
})
|
||
att = {
|
||
"color": color,
|
||
"actions": actions,
|
||
}
|
||
if i == 0 and title:
|
||
att["title"] = title
|
||
attachments.append(att)
|
||
|
||
# Sonderschaltflächen in separatem Attachment
|
||
if special_buttons:
|
||
special_actions = []
|
||
for s_idx, tup in enumerate(special_buttons):
|
||
label = tup[0]
|
||
prefix = tup[1]
|
||
style = tup[2]
|
||
suffix = tup[3] if len(tup) >= 4 else None
|
||
msg_payload = f"{prefix}::{suffix}" if suffix else prefix
|
||
special_actions.append({
|
||
"type": "button",
|
||
"text": label,
|
||
"msg": msg_payload,
|
||
"msg_in_chat_window": True,
|
||
"style": style,
|
||
"action_id": _make_action_id(msg_payload, s_idx),
|
||
})
|
||
attachments.append({
|
||
"color": color,
|
||
"actions": special_actions,
|
||
})
|
||
|
||
return attachments
|