"""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