Spaces:
Paused
Paused
File size: 4,858 Bytes
61d6e2a 2e79f82 53e3df0 61d6e2a 53e3df0 61d6e2a 2e79f82 61d6e2a 53e3df0 61d6e2a 53e3df0 61d6e2a 53e3df0 61d6e2a 2e79f82 61d6e2a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | """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"<b>Post #{p.id}</b> Β· {state}", f"Channel: <b>{esc(ch.title)}</b>"]
if p.status == "sent" and p.message_id:
lines.append(f'<a href="{post_link(ch, p.message_id)}">Open in the channel</a>')
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("<i>Picked up from the channel - it was not made with this bot.</i>")
snippet = short(p.text or "", 220) or "(no text)"
lines += ["", f"<i>{esc(snippet)}</i>"]
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"π <b>Button from another bot</b> (row {r + 1}, position {c + 1})\n"
f"Text: <code>{esc(b.get('t') or '')}</code>\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"π <b>Button</b> (row {r + 1}, position {c + 1})\n"
f"Text: <code>{esc(b.get('t') or '')}</code>\n"
f"Link: <code>{esc(b.get('u') or '- none yet -')}</code>"
)
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
|