Spaces:
Runtime error
Runtime error
| # PATH: bot/core/link_parser.py | |
| from __future__ import annotations | |
| import re | |
| from typing import Tuple, Union, Optional | |
| ChatRef = Union[int, str] | |
| _TME_RE = re.compile( | |
| r"^(?:https?://)?t\.me/(?:(?:c/)?(?P<cchat>\d+)|(?P<user>[A-Za-z0-9_]{4,}))/+(?P<msg>\d+)", | |
| re.IGNORECASE, | |
| ) | |
| def parse_link(link: str) -> Optional[Tuple[ChatRef, int]]: | |
| if not link: | |
| return None | |
| link = link.strip().split("?", 1)[0].rstrip("/") | |
| m = _TME_RE.match(link) | |
| if not m: | |
| parts = link.strip("/").split("/") | |
| if len(parts) >= 2: | |
| chat = parts[-2] | |
| msg = parts[-1] | |
| try: | |
| mid = int(msg) | |
| except Exception: | |
| return None | |
| try: | |
| cid = int(chat) | |
| return cid, mid | |
| except Exception: | |
| return chat, mid | |
| return None | |
| msg_id = int(m.group("msg")) | |
| if m.group("cchat"): | |
| internal = m.group("cchat") | |
| return int("-100" + internal), msg_id | |
| return m.group("user"), msg_id |