"""Texts and inline keyboards for the post panel.""" from __future__ import annotations from telethon import Button from .buttons import count, grid_label, link_positions, missing_links from .richmsg import rich_of from .tgutil import esc, post_link, short CANCEL = [[Button.inline("โœ–๏ธ Cancel", "cx")]] KIND_ICON = {None: "๐Ÿ“", "photo": "๐Ÿ–ผ", "video": "๐ŸŽฌ", "animation": "๐ŸŽž", "audio": "๐ŸŽต", "document": "๐Ÿ“Ž"} KIND_NAME = {None: "text", "photo": "photo", "video": "video", "animation": "GIF", "audio": "audio", "document": "file"} def post_title(p) -> str: for row in p.buttons or []: for b in row: if b.get("t"): return b["t"] text = (p.text or "").strip() return text.splitlines()[0] if text else (KIND_NAME.get(p.media_kind) or "post") def post_label(p) -> str: icon = "๐Ÿ“" if p.status == "draft" else "โœ…" return f"{icon} #{p.id} {short(post_title(p), 34)}" def panel_text(p, ch, note=None) -> str: state = "draft (not published yet)" if p.status == "draft" else "published" lines = [f"Post #{p.id} ยท {state}", f"Channel: {esc(ch.title)}"] if p.status == "sent" and p.message_id: lines.append(f'Open in the channel') rich = rich_of(p) if rich is not None: origin = {"html": "typed as HTML", "markdown": "typed as Markdown"}.get(rich.fmt, "made in Telegram") lines.append(f"Type: ๐Ÿงฉ rich message ({origin})") else: lines.append(f"Type: {KIND_ICON.get(p.media_kind, '๐Ÿ“Ž')} {KIND_NAME.get(p.media_kind, p.media_kind)}") n, miss = count(p.buttons), missing_links(p.buttons) lines.append(f"Buttons: {n}" + (f" (โš ๏ธ {miss} without a link)" if miss else "")) if p.source == "adopted": lines.append("Picked up from the channel - it was not made with this bot.") snippet = short(p.text or "", 220) or "(no text)" lines += ["", f"{esc(snippet)}"] if note: lines += ["", note] return "\n".join(lines) def panel_keyboard(p) -> list: pid = p.id kb = [] for r, row in enumerate(p.buttons or []): kb.append([Button.inline(grid_label(b), f"bs:{pid}:{r}:{c}") for c, b in enumerate(row)]) rich = rich_of(p) is not None kb.append( [Button.inline("โœ๏ธ Caption" if p.media_kind else "โœ๏ธ Text", f"et:{pid}")] + ([] if rich else [Button.inline("๐Ÿ–ผ Media", f"em:{pid}")]) # a rich message has no media slot ) row3 = [Button.inline("โž• Button", f"ba:{pid}"), Button.inline("๐Ÿ“‹ Set buttons", f"bp:{pid}")] if p.buttons: row3.append(Button.inline("๐Ÿงน Clear", f"bc:{pid}")) kb.append(row3) extra = [] if not p.media_kind and not rich: extra.append(Button.inline(f"๐Ÿ”— Link preview: {'on' if p.link_preview else 'off'}", f"lp:{pid}")) if p.media_kind and p.status == "draft": extra.append(Button.inline("๐Ÿšซ Remove media", f"rm:{pid}")) if extra: kb.append(extra) if p.status == "sent" and p.message_id and link_positions(p.buttons): kb.append([Button.inline("๐Ÿ”— Mass replace links", f"mlp:{pid}")]) if p.status == "draft": kb.append([Button.inline("๐Ÿ‘ Preview", f"pv:{pid}"), Button.inline("๐Ÿš€ Publish", f"pb:{pid}")]) kb.append([Button.inline("๐Ÿ—‘ Discard", f"dl:{pid}"), Button.inline("๐Ÿ“š My posts", "pc:d:0")]) else: kb.append([Button.inline("๐Ÿ‘ Preview", f"pv:{pid}"), Button.inline("๐Ÿ—‘ Delete", f"dl:{pid}")]) kb.append([Button.inline("๐Ÿ“š My posts", f"pc:{p.channel_id}:0")]) return kb def button_text(p, r: int, c: int) -> str: b = p.buttons[r][c] if b.get("raw"): return ( f"๐Ÿ”’ Button from another bot (row {r + 1}, position {c + 1})\n" f"Text: {esc(b.get('t') or '')}\n" "Buttons like reactions only work with the bot that made them, so you can move or delete it, " "but not edit it." ) return ( f"๐Ÿ”˜ Button (row {r + 1}, position {c + 1})\n" f"Text: {esc(b.get('t') or '')}\n" f"Link: {esc(b.get('u') or '- none yet -')}" ) def button_keyboard(p, r: int, c: int) -> list: pid, at = p.id, f"{p.id}:{r}:{c}" b = p.buttons[r][c] kb = [] if not b.get("raw"): kb.append([Button.inline("โœ๏ธ Change text", f"bt:{at}"), Button.inline("๐Ÿ”— Change link", f"bu:{at}")]) kb.append( [ Button.inline("โฌ…๏ธ", f"bm:{at}:l"), Button.inline("โฌ†๏ธ", f"bm:{at}:u"), Button.inline("โฌ‡๏ธ", f"bm:{at}:d"), Button.inline("โžก๏ธ", f"bm:{at}:r"), ] ) kb.append([Button.inline("๐Ÿ—‘ Delete", f"bd:{at}"), Button.inline("๐Ÿ”™ Back", f"po:{pid}")]) return kb