
    (Gj,                    n    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dS )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}chat_idr   returnUnion[int, str]c                    t          |                                           }	 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)r   chat_id_strs     M/home/rurouni/.hermes/hermes-agent/plugins/platforms/telegram/telegram_ids.pynormalize_telegram_chat_idr      sZ     g,,$$&&K;z"   s   2 AAr
   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2333    boolc                    t          t                              t          |                                                               S )zHTrue when the value is an ``@username``-format Telegram chat identifier.)r   _TELEGRAM_USERNAME_RE	fullmatchr
   r   r   s    r   looks_like_telegram_usernamer   +   s1    %//G0B0B0D0DEEFFFr   
target_refUnion[str, None]c                j    t          |                                           }t          |          r|ndS )zCReturn the value when it is an ``@username`` target, else ``None``.N)r
   r   r   )r   values     r   parse_telegram_username_targetr   0   s0    
OO!!##E077A55TAr   )r   r   r   r   )r   r   r   r
   )r   r   r   r   )r   r   r   r   )__doc__
__future__r   retypingr   r   compiler   r   r   r   r    r   r   <module>r&      s   	 	 # " " " " " 				        
 #
#9::    4 4 4 4
G G G G
B B B B B Br   