
    `gj,                    p    d Z ddlmZ ddlZddlmZmZ  ej                  d      Zd
dZ	ddZ
ddZdd	Zy)u  Helpers for Telegram Bot API chat identifiers.

Telegram's Bot API accepts a ``chat_id`` in two forms: a numeric ID (an int,
e.g. ``123456789`` for a DM or ``-1001234567890`` for a channel/supergroup) or
an ``@username`` string for public channels and groups. Hermes historically
coerced every ``chat_id`` with ``int()``, which crashes on the username form
(``ValueError: invalid literal for int()``). Normalizing here lets numeric IDs
pass through as ints while usernames pass through unchanged — both are valid
values for the Bot API.
    )annotationsN)AnyUnionz@[A-Za-z0-9_]{4,32}c                z    t        |       j                         }	 t        |      S # t        t        f$ r |cY S w xY w)a:  Return a Bot API-compatible chat_id.

    Numeric values (incl. negative channel IDs) are returned as ``int``; any
    non-numeric value (e.g. an ``@username``) is returned as a stripped string.
    Telegram's Bot API accepts both, so this never raises on a username the way
    a bare ``int(chat_id)`` would.
    )strstripint	TypeError
ValueError)chat_idchat_id_strs     Z/root/.hermes/venv/lib/python3.12/site-packages/plugins/platforms/telegram/telegram_ids.pynormalize_telegram_chat_idr      s@     g,$$&K;z" s   
& ::c                *    t        t        |             S )zBStable string key for a chat_id (for dict keys / persisted state).)r   r   r   s    r   telegram_chat_id_keyr   &   s    )'233    c                l    t        t        j                  t        |       j	                                     S )zHTrue when the value is an ``@username``-format Telegram chat identifier.)bool_TELEGRAM_USERNAME_RE	fullmatchr   r   r   s    r   looks_like_telegram_usernamer   +   s&    %//G0B0B0DEFFr   c                R    t        |       j                         }t        |      r|S dS )zCReturn the value when it is an ``@username`` target, else ``None``.N)r   r   r   )
target_refvalues     r   parse_telegram_username_targetr   0   s&    
O!!#E075ATAr   )r   r   returnzUnion[int, str])r   r   r   r   )r   r   r   r   )r   r   r   zUnion[str, None])__doc__
__future__r   retypingr   r   compiler   r   r   r   r    r   r   <module>r$      s?   	 # 	 
 #

#9: 4
G
Br   