Spaces:
Paused
Paused
File size: 18,187 Bytes
61d6e2a 848377f 61d6e2a 65651b1 61d6e2a 2e79f82 61d6e2a 1dfae7a 848377f 1dfae7a 848377f 1dfae7a 848377f 1dfae7a 848377f 1dfae7a 61d6e2a 848377f 53e3df0 848377f 61d6e2a 53e3df0 848377f 53e3df0 61d6e2a 848377f 53e3df0 61d6e2a de342dc 65651b1 61d6e2a 65651b1 61d6e2a de342dc 5bcd8c9 de342dc 61d6e2a 848377f 61d6e2a 314215f 61d6e2a 314215f | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | """Helpers around Telethon types: entity/button (de)serialisation, media info, raw edits, rights."""
from __future__ import annotations
import asyncio
import base64
import copy
import html
import logging
import os
from dataclasses import dataclass, field
from types import SimpleNamespace
from typing import Any, Awaitable, Callable, Optional
from telethon import errors, functions, types
from telethon.extensions import BinaryReader
log = logging.getLogger(__name__)
esc = html.escape
# Telegram refusals that do not go away with the next message: the bot has no access (or no right) in that channel
FATAL = {
"ChatAdminRequiredError",
"ChatWriteForbiddenError",
"ChannelPrivateError",
"UserBannedInChannelError",
"ChannelInvalidError",
"ChatRestrictedError",
}
# ----------------------------------------------------------------------------- entities
_SIMPLE = {
"bold": types.MessageEntityBold,
"italic": types.MessageEntityItalic,
"underline": types.MessageEntityUnderline,
"strike": types.MessageEntityStrike,
"code": types.MessageEntityCode,
}
if hasattr(types, "MessageEntitySpoiler"):
_SIMPLE["spoiler"] = types.MessageEntitySpoiler
# entities Telegram works out by itself from the text - never stored
_AUTO = tuple(
getattr(types, n)
for n in (
"MessageEntityUrl",
"MessageEntityMention",
"MessageEntityHashtag",
"MessageEntityCashtag",
"MessageEntityBotCommand",
"MessageEntityEmail",
"MessageEntityPhone",
"MessageEntityBankCard",
)
if hasattr(types, n)
)
def _b64(obj: Any) -> str:
return base64.b64encode(bytes(obj)).decode()
def _unb64(data: str) -> Any:
return BinaryReader(base64.b64decode(data)).tgread_object()
def ser_entities(entities) -> list:
"""Message entities -> JSON-friendly list (UTF-16 offsets, as Telegram uses)."""
out: list = []
for e in entities or []:
if isinstance(e, _AUTO):
continue
done = False
for key, cls in _SIMPLE.items():
if isinstance(e, cls):
out.append({"k": key, "o": e.offset, "l": e.length})
done = True
break
if done:
continue
if isinstance(e, types.MessageEntityPre):
out.append({"k": "pre", "o": e.offset, "l": e.length, "g": e.language or ""})
elif isinstance(e, types.MessageEntityTextUrl):
out.append({"k": "url", "o": e.offset, "l": e.length, "u": e.url})
elif isinstance(e, types.MessageEntityBlockquote):
out.append({"k": "quote", "o": e.offset, "l": e.length, "c": bool(getattr(e, "collapsed", False))})
elif isinstance(e, types.MessageEntityCustomEmoji):
out.append({"k": "emoji", "o": e.offset, "l": e.length, "d": e.document_id})
else:
try: # anything else is kept byte-for-byte
out.append({"k": "raw", "b": _b64(e)})
except Exception: # pragma: no cover
log.warning("cannot store entity %r", e)
return out
def de_entities(data) -> list:
out: list = []
for d in data or []:
k = d.get("k")
try:
if k in _SIMPLE:
out.append(_SIMPLE[k](d["o"], d["l"]))
elif k == "pre":
out.append(types.MessageEntityPre(d["o"], d["l"], d.get("g", "")))
elif k == "url":
out.append(types.MessageEntityTextUrl(d["o"], d["l"], d["u"]))
elif k == "quote":
out.append(types.MessageEntityBlockquote(d["o"], d["l"], True if d.get("c") else None))
elif k == "emoji":
out.append(types.MessageEntityCustomEmoji(d["o"], d["l"], d["d"]))
elif k == "raw":
out.append(_unb64(d["b"]))
except Exception: # pragma: no cover
log.warning("bad stored entity %r", d)
return out
# ------------------------------------------------------------------------------ buttons
_LEGACY = hasattr(types, "KeyboardButtonUrl") # Telethon < 1.45 used one class per button kind
def button_url(b) -> Optional[str]:
"""The link of a URL button, else None."""
if _LEGACY:
return b.url if isinstance(b, types.KeyboardButtonUrl) else None
t = getattr(b, "type", None)
return t.url if isinstance(t, types.InlineButtonTypeUrl) else None
def is_callback_button(b) -> bool:
if _LEGACY:
return isinstance(b, types.KeyboardButtonCallback)
return isinstance(getattr(b, "type", None), types.InlineButtonTypeCallback)
def make_url_button(text: str, url: str, style=None):
if _LEGACY:
return types.KeyboardButtonUrl(text, url)
return types.KeyboardButton(text=text, type=types.InlineButtonTypeUrl(url), style=style)
def with_url(b, new_url: str):
"""Copy of URL button `b` pointing to `new_url`; every other attribute (colour, ...) is kept."""
nb = copy.copy(b)
if _LEGACY:
nb.url = new_url
else:
nb.type = copy.copy(b.type)
nb.type.url = new_url
return nb
def with_text(b, new_text: str):
"""Copy of button `b` with another name; the link, colour ... are kept."""
nb = copy.copy(b)
nb.text = new_text
return nb
def _style_dump(b) -> Optional[str]:
st = getattr(b, "style", None)
if st is None:
return None
if not any(getattr(st, f, None) for f in ("bg_primary", "bg_danger", "bg_success", "icon")):
return None
try:
return _b64(st)
except Exception: # pragma: no cover
return None
def ser_markup(markup) -> list:
"""Inline keyboard -> rows of {'t': text, 'u': url} (URL buttons) or {'t', 'raw'} (any other kind)."""
rows: list = []
if isinstance(markup, types.ReplyInlineMarkup):
for row in markup.rows:
r = []
for b in row.buttons:
url = button_url(b)
if url is not None:
item = {"t": b.text, "u": url}
s = _style_dump(b)
if s:
item["s"] = s
r.append(item)
else:
try:
r.append({"t": getattr(b, "text", ""), "raw": _b64(b)})
except Exception: # pragma: no cover
log.warning("cannot store button %r", b)
if r:
rows.append(r)
return rows
def build_markup(rows) -> Optional[types.ReplyInlineMarkup]:
out = []
for row in rows or []:
btns = []
for b in row:
if b.get("raw"):
try:
btns.append(_unb64(b["raw"]))
except Exception: # pragma: no cover
continue
elif b.get("u"):
style = None
if b.get("s"):
try:
style = _unb64(b["s"])
except Exception: # pragma: no cover
style = None
btns.append(make_url_button(b["t"], b["u"], style))
if btns:
out.append(types.KeyboardButtonRow(btns))
return types.ReplyInlineMarkup(out) if out else None
def state_of(text, entities, markup, preview) -> dict:
"""Snapshot used for the undo log."""
return {
"text": text or "",
"entities": ser_entities(entities),
"buttons": ser_markup(markup),
"preview": preview, # True/False for text messages, None for media messages
}
# --------------------------------------------------------------------------------- media
def classify_media(m) -> Optional[str]:
"""photo | video | animation | audio | document, or None (text / unsupported)."""
media = getattr(m, "media", None)
if isinstance(media, types.MessageMediaPhoto) and getattr(media, "photo", None):
return "photo"
if isinstance(media, types.MessageMediaDocument) and getattr(media, "document", None):
if m.gif:
return "animation"
if m.video:
return "video"
if m.audio:
return "audio"
if m.voice or m.video_note or m.sticker:
return None
return "document"
return None
# Media is stored as the serialised Telegram media object ("tl:<base64>"). Telethon's Bot-API style
# file_id helper cannot pack photos with the current Telegram schema, so we don't rely on it.
_REF_PREFIX = "tl:"
def media_ref(m) -> Optional[str]:
"""A storable reference to the photo / document attached to message `m` (None if it has none)."""
media = getattr(m, "media", None)
if isinstance(media, (types.MessageMediaPhoto, types.MessageMediaDocument)):
try:
return _REF_PREFIX + _b64(media)
except Exception: # pragma: no cover
log.warning("cannot store media reference", exc_info=True)
return None
pack_file_id = media_ref # older name, kept for the callers
def media_from_ref(ref: str, *, blank_reference: bool = False):
"""Rebuild the media object; `blank_reference` drops the (possibly expired) file reference."""
media = _unb64(ref[len(_REF_PREFIX):]) if ref.startswith(_REF_PREFIX) else None
if media is None:
raise ValueError("unknown media reference")
if blank_reference:
media = copy.copy(media)
inner = copy.copy(media.photo if isinstance(media, types.MessageMediaPhoto) else media.document)
inner.file_reference = b""
if isinstance(media, types.MessageMediaPhoto):
media.photo = inner
else:
media.document = inner
return media
async def with_media(ref: str, call):
"""call(media): first with the stored file reference, then (if Telegram says it expired) without it."""
try:
return await call(media_from_ref(ref))
except errors.RPCError as e:
if "FileReference" not in type(e).__name__ and type(e).__name__ != "MediaEmptyError":
raise
log.info("stored file reference rejected (%s) - retrying without it", type(e).__name__)
return await call(media_from_ref(ref, blank_reference=True))
def media_info(m) -> tuple:
kind = classify_media(m)
return kind, (media_ref(m) if kind else None)
def media_key(media) -> Optional[tuple]:
"""("photo" | "doc", id) of the photo / file in a message's media; the id is the same on every account."""
if isinstance(media, types.MessageMediaPhoto) and getattr(media, "photo", None):
return "photo", media.photo.id
if isinstance(media, types.MessageMediaDocument) and getattr(media, "document", None):
return "doc", media.document.id
return None
@dataclass
class MediaFile:
"""A photo or file downloaded from Telegram, with what is needed to upload it again as the same kind of post."""
path: str
kind: str # photo | video | animation | audio | document
name: str
mime_type: Optional[str] = None
attributes: list = field(default_factory=list) # the file's name, video size and length, GIF mark ... as it had them
thumb_path: Optional[str] = None # the picture Telegram shows for the file (JPEG); None: it has none
async def fetch_media(client, ref: str, kind: str, folder: str) -> MediaFile:
"""Download the photo / file behind the stored media reference `ref` (as `client` sees it) into `folder`.
A file's thumbnail comes along: without it the file would be uploaded again with a plain file icon."""
path = await with_media(ref, lambda media: client.download_media(media, file=folder + os.sep))
if not path:
raise ValueError("Telegram gave me nothing to download for that media")
doc = getattr(media_from_ref(ref), "document", None)
return MediaFile(
path=str(path),
kind=kind,
name=os.path.basename(str(path)),
mime_type=getattr(doc, "mime_type", None),
attributes=list(getattr(doc, "attributes", None) or []),
thumb_path=await fetch_thumb(client, ref, folder) if doc is not None else None,
)
async def fetch_thumb(client, ref: str, folder: str) -> Optional[str]:
"""The thumbnail of the file behind `ref` (the largest one Telegram has) as a JPEG in `folder`, or None.
Best effort: a file that goes out without its thumbnail is better than an edit that fails."""
where = os.path.join(folder, "thumb")
try:
os.makedirs(where, exist_ok=True)
path = await with_media(ref, lambda media: client.download_media(media, file=where + os.sep, thumb=-1))
except (errors.RPCError, OSError, ValueError, TypeError) as e:
log.info("could not download the thumbnail: %s", type(e).__name__)
return None
return str(path) if path and os.path.isfile(str(path)) else None
def utf16_len(s: str) -> int:
return len(s.encode("utf-16-le")) // 2
# --------------------------------------------------------------------------------- misc
def peer_of(ch) -> types.InputPeerChannel:
return types.InputPeerChannel(ch.id, ch.access_hash)
def post_link(ch, message_id: int) -> str:
if ch.username:
return f"https://t.me/{ch.username}/{message_id}"
return f"https://t.me/c/{ch.id}/{message_id}"
def short(s: str, n: int) -> str:
s = " ".join((s or "").split())
return s if len(s) <= n else s[: n - 1] + "…"
async def flood_retry(factory: Callable[[], Awaitable[Any]], tries: int = 4) -> Any:
for _ in range(tries):
try:
return await factory()
except errors.FloodWaitError as e:
wait = int(getattr(e, "seconds", 5)) + 1
log.warning("flood wait %ss", wait)
await asyncio.sleep(min(wait, 900))
return await factory()
async def edit_raw(
client,
peer,
message_id: int,
*,
text: Optional[str] = None,
entities=None,
markup=None,
preview=None,
media=None,
rich_message=None,
):
"""messages.editMessage with every field explicit.
`markup=None` together with `text` (or `rich_message`) removes the keyboard (that is how Telegram behaves);
a markup-only edit (text=None) leaves the text alone. `media` (an InputMedia) replaces the photo / file.
`rich_message` (an InputRichMessage*) makes the message that rich message instead of text.
"""
kw: dict = {"peer": peer, "id": message_id}
if markup is not None:
kw["reply_markup"] = markup
if media is not None:
kw["media"] = media
if rich_message is not None:
kw["rich_message"] = rich_message
elif text is not None:
kw["message"] = text
kw["entities"] = list(entities or [])
if preview is not None:
kw["no_webpage"] = not preview
return await client(functions.messages.EditMessageRequest(**kw))
async def get_rights(client, ch):
"""The bot's own admin rights in a channel, or None if Telegram won't tell us."""
try:
res = await client(
functions.channels.GetParticipantRequest(channel=peer_of(ch), participant=types.InputPeerSelf())
)
except Exception as e:
log.info("could not read own rights in %s: %s", ch.id, e)
return None
p = res.participant
if isinstance(p, (types.ChannelParticipantCreator, types.ChannelParticipantAdmin)):
r = getattr(p, "admin_rights", None)
creator = isinstance(p, types.ChannelParticipantCreator)
def has(name: str) -> bool:
return True if (creator and r is None) else bool(getattr(r, name, False))
return SimpleNamespace(
admin=True,
post=has("post_messages"),
edit=has("edit_messages"),
delete=has("delete_messages"),
invite=has("invite_users"),
add_admins=has("add_admins"),
change_info=has("change_info"),
)
return SimpleNamespace(
admin=False, post=False, edit=False, delete=False, invite=False, add_admins=False, change_info=False
)
_HINTS = {
"ChatAdminRequiredError": "The bot needs admin rights in that channel (post / edit / delete messages).",
"MessageAuthorRequiredError": "Telegram wants the message author. Give the bot the 'Edit messages of others' right.",
"InlineBotRequiredError": "That post was sent through another bot's inline mode, so only that bot can edit it.",
"MessageEditTimeExpiredError": "Telegram no longer allows editing that message.",
"MessageIdInvalidError": (
"Telegram won't let the bot change that message (or it was deleted). If the post is still there, "
"the usual reason is that its buttons were added by another bot: only that bot may change them."
),
"ButtonUrlInvalidError": "Telegram rejected one of the button links.",
"ReplyMarkupInvalidError": "Telegram rejected the buttons.",
"ChatWriteForbiddenError": "The bot can't post in that channel - check its admin rights.",
"ChannelPrivateError": "The bot can't access that channel any more (removed or banned?).",
"UserBannedInChannelError": "The bot is banned in that channel.",
"FileReferenceExpiredError": "The saved media expired - send the media again.",
"MediaEmptyError": "Telegram could not use that media - send it again.",
"EntityBoundsInvalidError": "The text formatting doesn't fit the text.",
"EntitiesTooLongError": "The text has too many formatted pieces (bold, links ...) for Telegram - use less formatting.",
"MessageTooLongError": "The text is too long for Telegram.",
"MediaCaptionTooLongError": "The caption is too long for Telegram.",
}
# Telegram's own error codes (the name of the Python class is only "BadRequestError" for the ones Telethon doesn't know)
_CODE_HINTS = {
"RICH_MESSAGE_BLOCK_INVALID": "Telegram would not take one of the blocks of that rich message.",
}
def explain_rpc(e: BaseException) -> str:
name = type(e).__name__
code = getattr(e, "message", None) or ""
hint = _HINTS.get(name)
if hint is None and code in _CODE_HINTS:
hint = f"{_CODE_HINTS[code]} ({code})"
return f"Telegram refused ({name}). " + (hint if hint is not None else str(e)[:200])
|