23 lines
650 B
Python
23 lines
650 B
Python
SEPARATOR = ":"
|
|
TELEGRAM_CALLBACK_MAX_BYTES = 64
|
|
|
|
ACTION_MUTE = "mute"
|
|
ACTION_DEEP = "deep"
|
|
ACTION_DOWNTIME = "dt"
|
|
ACTION_CONFIRM = "dty"
|
|
ACTION_CANCEL = "dtn"
|
|
|
|
|
|
def encode(action: str, *parts: object) -> str:
|
|
data = SEPARATOR.join([action, *(str(part) for part in parts)])
|
|
if len(data.encode()) > TELEGRAM_CALLBACK_MAX_BYTES:
|
|
raise ValueError("callback_data zu lang")
|
|
return data
|
|
|
|
|
|
def decode(data: str) -> tuple[str, tuple[str, ...]] | None:
|
|
if not data or len(data.encode()) > TELEGRAM_CALLBACK_MAX_BYTES:
|
|
return None
|
|
action, *parts = data.split(SEPARATOR)
|
|
return (action, tuple(parts)) if action else None
|