File size: 1,055 Bytes
cb2264a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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