
    $*jS                       d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
m
Z
mZ ddlmZmZmZmZmZ  ej        e          Z	 ddlmamamamama 	 ddlma n# e$ r daY nw xY wddlmamam a m!a"m#a#m$a$ ddl%m&a&m'a' dd	l(m)a) d
a*n8# e$ r0 da*eaeaeaeaeadaeaeaea ea"ea)da$da&da' G d d          Z+e+a#Y nw xY wddl,Z,ddl-m.Z/ e,j0        1                    d e2 e/e3          4                                j5        d                              ddl6m7Z7m8Z8 ddl9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZG ddlHmIZImJZJmKZK ddlLmMZM h dZNddddddZOddddddZPdZQd eRfd!ZS e	jT        d"          ZUd#e2d e2fd$ZVd#e2d e2fd%ZW e	jT        d&          ZXd'e2d eRfd(ZYd'e2d eZe2         fd)Z[d*eZe2         d e2fd+Z\d#e2d e2fd,Z] G d- d.e:          Z^dS )/z
Telegram platform adapter.

Uses python-telegram-bot library for:
- Receiving messages from users/groups
- Sending responses back
- Handling media and commands
    N)datetimetimezone)DictListOptionalSetAny)UpdateBotMessageInlineKeyboardButtonInlineKeyboardMarkupLinkPreviewOptionsApplicationCommandHandlerCallbackQueryHandlerMessageHandlerContextTypesfilters	ParseModeChatTypeHTTPXRequestTFc                       e Zd ZeZdS )_MockContextTypesN)__name__
__module____qualname__r	   DEFAULT_TYPE     ?/home/ubuntu/.hermes/hermes-agent/gateway/platforms/telegram.pyr   r   ;   s        r$   r   )Path   )PlatformPlatformConfig)BasePlatformAdapterMessageEventMessageTypeProcessingOutcome
SendResultcache_image_from_bytescache_audio_from_bytescache_video_from_bytescache_document_from_bytesresolve_proxy_urlSUPPORTED_VIDEO_TYPESSUPPORTED_DOCUMENT_TYPESSUPPORTED_IMAGE_DOCUMENT_TYPES	utf16_len)TelegramFallbackTransportdiscover_fallback_ipsparse_fallback_ip_env)atomic_replace>   .gif.jpg.png.jpeg.webpr>   r=   r@   r<   )	image/png
image/jpegz	image/jpg
image/webp	image/gifrA   rB   rC   rD   )r>   r=   r?   r@   r<      returnc                  ^   t           rdS 	 ddlm}   | dd           n# t          $ r Y dS w xY w	 ddlm}m}m} ddlm}m	} 	 dd	lm
} n# t          $ r d
}Y nw xY wddlm}m}m}	m}
m}m} ddlm}m} ddlm} n# t          $ r Y dS w xY w|a|a|a|a|a	|a
|a|a|	a|
a|a|a|a|a|ada dS )aF  Check if Telegram dependencies are available.

    If python-telegram-bot is missing, attempts to lazy-install it via
    ``tools.lazy_deps.ensure("platform.telegram")``. After a successful
    install, re-imports the SDK and flips ``TELEGRAM_AVAILABLE`` to True
    so the adapter's class-level type aliases get rebound.
    Tr   )ensurezplatform.telegramF)prompt)r
   r   r   )r   r   r   Nr   r   r   )TELEGRAM_AVAILABLEtools.lazy_depsrH   	Exceptiontelegramr
   r   r   r   r   r   ImportErrortelegram.extr   r   r   r   r   r   telegram.constantsr   r   telegram.requestr   TelegramMessageHandler)_lazy_ensure_Update_Bot_Message_IKB_IKM_LPO_App_CH_CQH_MH_CT_filters_PM_CtT_HRs                   r%   check_telegram_requirementsrc   o   s     t::::::(77777   uuPPPPPPPPPPWWWWWWWW	;;;;;;; 	 	 	DDD		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	JIIIIIII8888888   uuF
CGKN LGIHL4sA    
--A< A 
A< AA< A!A< <
B
	B
z([_*\[\]()~`>#\+\-=|{}.!\\])textc                 8    t                               d|           S )zIEscape Telegram MarkdownV2 special characters with a preceding backslash.z\\\1)_MDV2_ESCAPE_REsubrd   s    r%   _escape_mdv2ri      s    w---r$   c                    t          j        dd|           }t          j        dd|          }t          j        dd|          }t          j        dd|          }t          j        dd|          }t          j        dd|          }|S )zStrip MarkdownV2 escape backslashes to produce clean plain text.

    Also removes MarkdownV2 formatting markers so the fallback
    doesn't show stray syntax characters from format_message conversion.
    z\\([_*\[\]()~`>#\+\-=|{}.!\\])\1z\*\*([^*]+)\*\*z\*([^*]+)\*z(?<!\w)_([^_]+)_(?!\w)z	~([^~]+)~z\|\|([^|]+)\|\|rerg   )rd   cleaneds     r%   _strip_mdv2ro      s     f6tDDGf'88Gf^UG44G f.w??Gf\5'22Gf'88GNr$   z0^\s*\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*){1,}\|?\s*$linec                 P    |                                  }t          |          od|v S )z:Return True if *line* could plausibly be a table data row.|)stripboolrp   strippeds     r%   _is_table_rowrw      s$    zz||H>>-cXo-r$   c                     |                                  }|                    d          r
|dd         }|                    d          r
|dd         }d |                    d          D             S )z7Split a simple GFM table row into stripped cell values.rr      Nc                 6    g | ]}|                                 S r#   rs   .0cells     r%   
<listcomp>z-_split_markdown_table_row.<locals>.<listcomp>   s     999TDJJLL999r$   )rs   
startswithendswithsplitru   s     r%   _split_markdown_table_rowr      su    zz||H3  ABB< !CRC=99X^^C%8%89999r$   table_blockc                 p   t          |           dk     rd                    |           S t          | d                   }t          |          dk     rd                    |           S t          |           dk    rt          | d                   ng }t          |          t          |          dz   k    }g }t          | dd         d          D ]S\  }}t          |          }|r"|r|d         r|d         nd| }|dd         }	nt	          d	 |D             d|           }|}	t          |	          t          |          k     r7|	                    d
gt          |          t          |	          z
  z             n7t          |	          t          |          k    r|	dt          |                   }	g }
t          ||	          D ])\  }}|s||k    r|
                    d| d|            *d| dg|
}|                    d                    |                     Ud                    |          S )z<Render a detected GFM table as Telegram-friendly row groups.   
r   r'   ry   N)startzRow c              3      K   | ]}||V  	d S Nr#   r}   s     r%   	<genexpr>z3_render_table_block_for_telegram.<locals>.<genexpr>   s'      ;;Td;D;;;;;;r$    u   • z: **

)lenjoinr   	enumeratenextextendzipappend)r   headersfirst_data_rowhas_row_label_colrendered_groupsindexrowcellsheading
data_cellsbulletsheadervaluegroup_liness                 r%    _render_table_block_for_telegramr      sh   
;!yy%%%'A77G
7||ayy%%% CFkBRBRUVBVBV.{1~>>>\^NN++s7||a/??!#OABBq999 7 7
s)#.. 	"'HE!HHeAhh...GqrrJJ ;;U;;;^E^^LLGJ z??S\\))rdc'llS__&DEFFFF__s7||++#Nc'llN3J   *55 	5 	5MFE$ ')9)9NN3&33E334444
 (G'''2'2tyy556666 ;;'''r$   c                    d| vsd| vr| S |                      d          }g }d}d}|t          |          k     r||         }|                                }|                    d          r| }|                    |           |dz  }c|r|                    |           |dz  }d|v r|dz   t          |          k     rt
                              ||dz                      r|||dz            g}|dz   }|t          |          k     r]t          ||                   rH|                    ||                    |dz  }|t          |          k     rt          ||                   H|                    t          |                     |}e|                    |           |dz  }|t          |          k     d	                    |          S )	ay  Rewrite GFM-style pipe tables into Telegram-friendly bullet groups.

    Detected by a row containing '|' immediately followed by a delimiter
    row matching :data:`_TABLE_SEPARATOR_RE`.  Subsequent pipe-containing
    non-blank lines are consumed as the table body and rewritten as
    per-row bullet groups. Tables inside existing fenced code blocks are left
    alone.
    rr   -r   Fr   ```ry   r'   )
r   r   lstripr   r   _TABLE_SEPARATOR_REmatchrw   r   r   )	rd   linesoutin_fenceirp   rv   r   js	            r%   _wrap_markdown_tablesr     s    $#T//JJtECH	A
c%jj..Qx;;== u%% 	#|HJJtFA 	JJtFA
 4KKAE

""#))%A,77 #  q1u.KAAc%jj..]58%<%<.""58,,,Q c%jj..]58%<%<. JJ7DDEEEA

4	QA c%jj..D 99S>>r$   c                       e Zd ZU dZdZdZdZdZdZdZ	e
ed<   dZd	Zd
ZdZeddddededee         dee         def
d            Zed             Zdef fdZdeeeef                  deeef         fdZddddddedee         dee         dee         dee         de
fdZedeeeef                  dee         fd             Zedeeeef                  dee         fd!            Zedeeeef                  dee          fd"            Z!edede
fd#            Z"ededee         deeeef                  de
fd$            Z#edefd%            Z$e	 	 dd&ee         deeeef                  d'ee         dee          fd(            Z%e	 	 	 ddedee         deeeef                  d)ee          d'ee         deeef         fd*            Z&edee         dee          fd+            Z'edee         dee          fd,            Z(ed-e)de
fd.            Z*ed-e)de
fd/            Z+ed-e)deeeef                  d)ee          de
fd0            Z,	 dd1ed2eeef         deeeef                  d)ee          d3ed4ee         defd5Z-de.e         fd6Z/ed-e)de
fd7            Z0ed-e)de
fd8            Z1ed-e)de
fd9            Z2ed-e)de
fd:            Z3dd<ede
de
fd=Z4deeef         fd>Z5dd?Z6d-e)ddfd@Z7ddAZ8d-e)ddfdBZ9	 	 dde dedCee          dDee         dee          f
dEZ:dFededee         fdGZ;ddedHedIe
dee         fdJZ<de de deddfdKZ=	 dde dHede dLe
ddf
dMZ>ddNZ?de
fdOZ@ddPZAd&ee         dQe de
fdRZB	 	 ddedSed&ee         deeeef                  deCf
dTZDddUdedVedSedeeeef                  deCf
dWZEd;ddXdedYedSedZe
deeeef                  deCfd[ZFddUdedYedSedZe
deeeef                  deCfd\ZGdedYede
fd]ZH	 	 ddee         deeeef                  de
fd^ZI	 dded_e dSedeeeef                  deCf
d`ZJda ZK	 	 	 ddedcededdedeeeef                  deCfdeZL	 	 ddedgeddedhedeeeef                  deCfdiZM	 ddedjedkeddedledeeeef                  deCfdmZN	 ddednedoee.         dpeddedeeeef                  deCfdqZO	 ddedre.dsedteddedeeeef                  deCfduZPdvZQdre.fdwZRdxe.dye deSfdzZTd{ededdfd|ZU	 	 	 	 	 	 ddZVdg dd;fdg dd;fdg dd;fdg dd;fddgddfddgddfddgddfddgddfddgddfddgddfd
ZWd{eddfdZXdededefdZYdedede defdZZdededeSe
ee         f         fdZ[	 	 	 ddededee         d&ee         deeeef                  deCf fdZ\	 	 d dede]eS         deeeef                  deddf
 fdZ^	 	 	 ddededee         d&ee         deeeef                  deCf fdZ_	 	 	 	 ddededee         dee         d&ee         deeeef                  deCf fdZ`	 	 	 ddededee         d&ee         deeeef                  deCf fdZa	 	 	 ddededee         d&ee         deeeef                  deCf fdZb	 	 	 ddededee         d&ee         deeeef                  deCfdZcddedeeeef                  ddfdZddedeeef         fdZedSedefdZfde
fdZgde
fdZhde
fdZide
fdZjdeke         fdZldeke         fdZmdeke         fdZndeke         fdZodeke         fdZpdeke          fdZqde]erjs                 fdZtdkeude
fdZvdkeude
fdZwedkeudeke         fd            Zxdkeude
fdZydkeude
fdÄZzdkeude
fdĄZ{dkeude
fdńZ|dee         dee         fdǄZ}dkeude
fdȄZ~dɄ Zdedefd˄Zdefd̄Zdedefd̈́ZdeudefdτZdeudeddfdЄZdeufdфZedee         dedefdԄ            Z	 	 ddkeudedee          dee         ddf
dׄZd;d؜dkeude
de
fdڄZddۄZd}edeeu         fd܄Zd}edej        ddfd݄Zd}edej        ddfdބZd}edej        ddfd߄ZdedefdZdeddfdZd<eddfdZdedeudefdZdeddfdZdededdfdZd}edej        ddfdZdededdfdZdeddfdZdeuddddfdZddZdedee         deeeef                  fdZdededHeddfdZ	 ddkeudedee          defdZde
fdZdedYedede
fdZdedYede
fdZdeddfdZdededdfdZ xZS (  TelegramAdapterz
    Telegram bot adapter.

    Handles:
    - Receiving messages from users and groups
    - Sending responses with Telegram markdown
    - Forum topics (thread_id support)
    - Media messages
    i   Ti  g?1REQUIRES_EDIT_FINALIZEi@  g
ףp=
?   gQ?N	min_value	max_valuenamedefaultr   r   rF   c                V   ddl }t          j        |           }	 |t          |          nt          |          }n&# t          t
          f$ r t          |          }Y nw xY w|                    |          st          |          }|t          ||          }|t          ||          }|S )zRead a float env var, reject non-finite values, and clamp to bounds.

        Guarantees the returned value is a finite number usable directly in
        ``asyncio.sleep()`` and similar APIs that reject NaN / Inf.
        r   N)	mathosgetenvfloat	TypeError
ValueErrorisfinitemaxmin)r   r   r   r   r   rawr   s          r%   _env_float_clampedz"TelegramAdapter._env_float_clampedv  s     	ioo	#"%/E#JJJuW~~EE:& 	# 	# 	#'NNEEE	#}}U## 	#'NNE y))E y))Es    ;  AAc                     t           S )z6Telegram measures message length in UTF-16 code units.)r7   selfs    r%   message_len_fnzTelegramAdapter.message_len_fn  s
     r$   configc                 
   t                                          |t          j                   d | _        d | _        d| _        |                                 | _        t          |dd          pd| _
        |                     dd          | _        t          t          j        dd                    | _        i | _        i | _        i | _        i | _        |                     ddd	d
          | _        |                     dd| j        d          | _        i | _        i | _        d | _        d| _        d| _        d | _        d| _        i | _        tA                      | _!        tE          j#                    | _$        | j%        j&        '                    dg           | _(        d | j(        D             | _)        | j%        j&        '                    d          rdnd| _*        i | _+        i | _,        i | _-        i | _.        d| _/        i | _0        d S )NFreply_to_modefirstdisable_link_previews)HERMES_TELEGRAM_MEDIA_BATCH_DELAY_SECONDSz0.8(HERMES_TELEGRAM_TEXT_BATCH_DELAY_SECONDSg333333?g{Gz?g       @r   .HERMES_TELEGRAM_TEXT_BATCH_SPLIT_DELAY_SECONDS      ?g      @r   	dm_topicsc                 @    h | ]}d |v t          |d                    S chat_idstr)r~   es     r%   	<setcomp>z+TelegramAdapter.__init__.<locals>.<setcomp>  s0     -
 -
 -
"#aC)r$   base_urll          @	important)1super__init__r(   TELEGRAM_app_bot_webhook_mode_compile_mention_patterns_mention_patternsgetattr_reply_to_mode_coerce_bool_extra_disable_link_previewsr   r   r   _media_batch_delay_seconds_pending_photo_batches_pending_photo_batch_tasks_media_group_events_media_group_tasksr   _text_batch_delay_seconds_text_batch_split_delay_seconds_pending_text_batches_pending_text_batch_tasks_polling_error_task_polling_conflict_count_polling_network_error_count_polling_error_callback_ref_send_path_degraded
_dm_topicsset_forum_command_registeredasyncioLock_forum_lockr   extraget_dm_topics_config_dm_topic_chat_ids_max_doc_bytes_model_picker_state_approval_state_slash_confirm_state_clarify_state_notifications_mode_status_message_ids)r   r   	__class__s     r%   r   zTelegramAdapter.__init__  s'   !2333+/	#'	#(!%!?!?!A!A#*6?G#L#L#WPW,0,C,CD[]b,c,c# +0	:egl0m0m*n*n'?A#CE'<> ;= *.)@)@6	 *A *
 *
& 04/F/F<4	 0G 0
 0
, ?A"BD&;? ,-$12)+/( */ *,3655&"<>>7;{7H7L7L[Z\7]7]-
 -
'+'=-
 -
 -
 { $$Z00"""! 	 57 /1 57! /1 )4  68   r$   metadatac                 j    t          | dd          dk    ri S |pi                     d          ri S ddiS )a!  Return disable_notification kwargs when the adapter is in silent mode.

        In "important" mode, all message sends are silently delivered
        (disable_notification=True) unless the caller explicitly requests a
        notification by setting ``metadata["notify"] = True``.
        r  r   notifydisable_notificationT)r   r   )r   r  s     r%   _notification_kwargsz$TelegramAdapter._notification_kwargs  sK     4.<<KKIN)) 	I&--r$   r   	chat_type	thread_id	user_nameuser_idr   r  r  r  c          
         t          |pd                                          }|sdS t          t          | dd          dd          }t          |dd          }t          |          r	 ddlm}	 t          |pd	                                                                          pd	}
|
d
k    rd	}
n|
dk    r|dnd}
 |	t          j        t          |p|          |
||r!t          |                                          nd|t          |          nd          }t           ||                    S # t          $ r  t                              d|d           Y nw xY wt          j        dd                                          }|s)t          j        dd                                          dv S d |                    d          D             }d|v p||v S )zIReturn whether a Telegram inline-button caller may perform gated actions.r   F_message_handlerN__self___is_user_authorizedr   )SessionSourcedmprivate
supergroupforumgroup)platformr   r  r  r  r  z=[Telegram] Falling back to env-only callback auth for user %sTexc_infoTELEGRAM_ALLOWED_USERSGATEWAY_ALLOW_ALL_USERS>   r   yestruec                 ^    h | ]*}|                                 |                                 +S r#   r|   )r~   uids     r%   r   z?TelegramAdapter._is_callback_user_authorized.<locals>.<setcomp>2  s-    TTTs		Tsyy{{TTTr$   ,*)r   rs   r   callablegateway.sessionr  lowerr(   r   rt   rL   loggerdebugr   r   r   )r   r  r   r  r  r  normalized_user_idrunnerauth_fnr  normalized_chat_typesourceallowed_csvallowed_idss                 r%   _is_callback_user_authorizedz,TelegramAdapter._is_callback_user_authorized  s2    !B//5577! 	5'94@@*dSS&"7>>G 	999999'*9+<'='='C'C'E'E'K'K'M'M'UQU$'944+/(()\996?6K77QX(&%. =+=>>2.8AKc)nn22444t090Ec)nnn4   GGFOO,,,   S&!       i 8"==CCEE 	\
 96;;AACCG[[[TTk.?.?.D.DTTTk!F%7;%FFs   )CD1 1'EEc                     |sd S |                     d          p|                     d          }|t          |          nd S )Nr  message_thread_idr   r   )clsr  r  s      r%   _metadata_thread_idz#TelegramAdapter._metadata_thread_id5  sG     	4LL--R>Q1R1R	!*!6s9~~~D@r$   c                     |sd S |                     d          p|                     d          }|t          |          nd S )Ndirect_messages_topic_id!telegram_direct_messages_topic_idr9  )r:  r  topic_ids      r%   "_metadata_direct_messages_topic_idz2TelegramAdapter._metadata_direct_messages_topic_id<  sH     	4<< :;;px||Lo?p?p ( 4s8}}}$>r$   c                 Z    |sd S |                     d          }|t          |          nd S )Ntelegram_reply_to_message_id)r   int)r:  r  reply_tos      r%   _metadata_reply_to_message_idz-TelegramAdapter._metadata_reply_to_message_idC  s6     	4<< >?? ( 4s8}}}$>r$   c                 Z    	 t          |           dk    S # t          t          f$ r Y dS w xY w)Nr   F)rC  r   r   r   s    r%   _looks_like_private_chat_idz+TelegramAdapter._looks_like_private_chat_idJ  s?    	w<<!##:& 	 	 	55	s    **c                 J   |                      |          ;t          |o+|                    d          o|                     |          d u          S |r|                    d          rdS t          |o+|r|                    d          p|                     |                    S )N telegram_dm_topic_reply_fallback"telegram_dm_topic_created_for_sendF)r@  rt   r   rE  rG  )r:  r   r  r  s       r%   _is_private_dm_topic_sendz)TelegramAdapter._is_private_dm_topic_sendQ  s     11(;;G LLL!CDDL55h??tK  
  	%IJJ 	5 MX\\*LMM <227;;	
 
 	
r$   c                      dS )Nz`Telegram DM topic delivery requires a reply anchor; refusing to send outside the requested topicr#   r#   r$   r%   _dm_topic_missing_anchor_errorz.TelegramAdapter._dm_topic_missing_anchor_errorh  s    qqr$   rD  r   c                     |rt          |          S |r2|                    d          r|dk    rd S |                     |          S d S )NrI  off)rC  r   rE  )r:  rD  r  r   s       r%   _reply_to_message_id_for_sendz-TelegramAdapter._reply_to_message_id_for_sendl  s]      	!x==  	?%GHH 	?%%t44X>>>tr$   reply_to_message_idc                    |r|                     d          rx|dk    rd|                     |          iS ||                     |          }|+|                     |          }|dt	          |          dS i S d|                     |          iS |                     |          }|dt	          |          dS d|                     |          iS )aA  Return Telegram send kwargs for forum and direct-message topic routing.

        Supergroup/forum topics use ``message_thread_id``. True Bot API Direct
        Messages topics can opt in with explicit ``direct_messages_topic_id``
        metadata. Hermes-created private-chat topic lanes are marked with
        ``telegram_dm_topic_reply_fallback``. Live replies send the private
        topic thread id together with a reply anchor; synthetic/resumed sends
        without an anchor use ``direct_messages_topic_id`` when metadata has it.
        ``message_thread_id`` alone can render outside the visible lane.

        When ``reply_to_mode`` is ``"off"``, the reply anchor is suppressed for
        DM topic fallback sends while preserving the ``message_thread_id`` so
        the message still lands in the correct topic.
        rI  rO  r8  N)r8  r=  )r   _message_thread_id_for_sendrE  r@  rC  )r:  r   r  r  rQ  r   direct_topic_ids          r%   _thread_kwargs_for_sendz'TelegramAdapter._thread_kwargs_for_send{  s   .  	U%GHH 	U%%+S-L-LY-W-WXX"*&)&G&G&Q&Q#"*"%"H"H"R"R".-1474H4H   	')H)H)S)STT@@JJ&%),/,@,@   $S%D%DY%O%OPPr$   c                 X    |rt          |          | j        k    rd S t          |          S r   )r   _GENERAL_TOPIC_THREAD_IDrC  r:  r  s     r%   rS  z+TelegramAdapter._message_thread_id_for_send  s.     	C	NNc.JJJ49~~r$   c                 (    |sd S t          |          S r   )rC  rX  s     r%   _message_thread_id_for_typingz-TelegramAdapter._message_thread_id_for_typing  s      	49~~r$   errorc                 H    dt          |                                           v S )Nthread not found)r   r,  )r[  s    r%   _is_thread_not_found_errorz*TelegramAdapter._is_thread_not_found_error  s    !SZZ%5%5%7%777r$   c                     | j         j                                        }|dk    s|                    d          rdS 	 ddlm} t          | |          S # t          $ r Y dS w xY w)N
badrequestTr   
BadRequestF)r
  r   r,  r   telegram.errorrb  
isinstancerN   )r[  r   rb  s      r%   _is_bad_request_errorz%TelegramAdapter._is_bad_request_error  s    '--//<4==#>#>4	111111eZ000 	 	 	55	s   A 
A! A!c                 "   |r|                     d          sdS |                     |          sdS t          |                                          |dv rdS |                     d          rd}t	          fd|D                       rdS dS )	u  True when a DM-topic send should be retried with routing stripped.

        Two cases trigger the retry:

        1. The original anchor-stale case — the reply target was deleted, so
           Bot API returns "message to be replied not found". The retry drops
           the reply anchor and the topic id together.

        2. The synthetic-event case (added when #27937 introduced
           ``direct_messages_topic_id`` fallback for sends without an anchor):
           if Bot API rejects the topic id itself with any BadRequest that
           mentions topic/thread routing, we retry without routing rather
           than dropping the message.
        rI  FNmessage to be replied not foundTr=  )direct_messages_topiczmessage thread not foundr]  topic_closedtopic_deletedztopic not foundc              3       K   | ]}|v V  	d S r   r#   )r~   marker	err_lowers     r%   r   zNTelegramAdapter._should_retry_without_dm_topic_reply_anchor.<locals>.<genexpr>  s(      CC66Y&CCCCCCr$   )r   re  r   r,  any)r:  r[  r  rQ  topic_markersrm  s        @r%   +_should_retry_without_dm_topic_reply_anchorz;TelegramAdapter._should_retry_without_dm_topic_reply_anchor  s    *  	X\\*LMM 	5((// 	5JJ$$&&	*/PT]/]/]4 <<233 
	M CCCC]CCCCC tur$   send_fnsend_kwargsmedia_labelreset_mediac                 z  K   	  |di | d{V S # t           $ r}|                     |||          s t                              d| j        ||           |
 |             t          |          }d|d<   |                    dd           |                    dd            |di | d{V cY d}~S d}~ww xY w)zFRetry stale private-topic media replies once without the topic anchor.NzR[%s] Reply target deleted for Telegram %s, retrying without reply/topic anchor: %srQ  r8  r=  r#   )rL   rp  r-  warningr   dictpop)	r   rq  rr  r  rQ  rs  rt  send_errretry_kwargss	            r%   &_send_with_dm_topic_reply_anchor_retryz6TelegramAdapter._send_with_dm_topic_reply_anchor_retry  s.     	1 //;///////// 	1 	1 	1CC#  
 NN:	   &,,L26L./0$7777>>> 00<00000000000000)	1s    
B:BB5/B:5B:c                 &   t          | j        dd          r | j        j                            dg           ng }t	          |t
                    r|                    d          }t          |rd                    d |D                       nd          S )zNReturn validated fallback IPs from config (populated by _apply_env_overrides).r   Nfallback_ipsr(  c              3   4   K   | ]}t          |          V  d S r   r   )r~   vs     r%   r   z0TelegramAdapter._fallback_ips.<locals>.<genexpr>  s(      -I-Ic!ff-I-I-I-I-I-Ir$   )	r   r   r   r   rd  r   r   r:   r   r   
configureds     r%   _fallback_ipszTelegramAdapter._fallback_ips  s    BI$+W^`dBeBemT[&**>2>>>km
j#&& 	/#))#..J$Z%aSXX-I-Ij-I-I-I%I%I%I]abbbr$   c                     t          |                                           }| j        j                                        dk    pd|v pd|v S )Nconflictz&terminated by other getupdates requestzanother bot instance is running)r   r,  r
  r   )r[  rd   s     r%   _looks_like_polling_conflictz,TelegramAdapter._looks_like_polling_conflict  sQ    5zz!!O$**,,
: 974?90D8	
r$   c                     | j         j                                        }|dv rdS 	 ddlm}m} t          | ||f          rdS n# t          $ r Y nw xY wt          | t                    S )zJReturn True for transient network errors that warrant a reconnect attempt.>   timedoutnetworkerrorconnectionerrorTr   NetworkErrorTimedOut)	r
  r   r,  rc  r  r  rd  rN   OSError)r[  r   r  r  s       r%   _looks_like_network_errorz)TelegramAdapter._looks_like_network_error(  s     '--//BBB4	========%,!9:: t 	 	 	D	%)))s   A 
AAc                    t                      }| g}|r|                                }t          |          }||v r*|                    |           |j        j                                        }t          |                                          }d|v sd|v sd|v rdS t          |dd          }t          |dd          }||	                    |           ||	                    |           |dS )	a*  Return True when a Telegram TimedOut wraps a connect-timeout.

        A plain Telegram TimedOut may mean the request reached Telegram and
        should not be re-sent. A ConnectTimeout means the TCP connection was
        never established, so retrying is safe and prevents silent drops.
        connecttimeoutzconnect timeoutzconnect timed outT	__cause__N__context__F
r   rx  idaddr
  r   r,  r   r   r   	r[  seenstackcuridentr   rd   causecontexts	            r%   _looks_like_connect_timeoutz+TelegramAdapter._looks_like_connect_timeout6  s    &+W 	&))++CsGGE}}HHUOOO=)//11Ds88>>##D4''+<+D+DH[_cHcHctCd33Ec=$77G U###"W%%%  	&  ur$   c                    t                      }| g}|r|                                }t          |          }||v r*|                    |           |j        j                                        }t          |                                          }d|v sd|v sd|v rd|v rdS t          |dd          }t          |dd          }||	                    |           ||	                    |           |d	S )
a  Return True when a Telegram TimedOut wraps an httpx pool timeout.

        PTB converts ``httpx.PoolTimeout`` into ``telegram.error.TimedOut`` with
        a message that explicitly states the request was *not* sent
        (``"Pool timeout: All connections in the connection pool are occupied.
        Request was *not* sent to Telegram."``). Because the request never left
        the process, re-sending is safe and cannot duplicate -- the opposite of
        a generic TimedOut, which may have reached Telegram. We match the
        wrapped ``httpx.PoolTimeout`` class as well as the message string so the
        check survives PTB message-wording changes.
        pooltimeoutzpool timeoutzconnection pooloccupiedTr  Nr  Fr  r  s	            r%   _looks_like_pool_timeoutz(TelegramAdapter._looks_like_pool_timeoutR  s    &+W 	&))++CsGGE}}HHUOOO=)//11Ds88>>##D$$$(>(>!T))jD.@.@tCd33Ec=$77G U###"W%%%#  	&$ ur$   Fkeyc                 (   t          | j        dd           r| j        j                            |          nd }||S t	          |t
                    r4|                                                                }|dv rdS |dv rdS |S t          |          S )Nr   >   r   onr$  r%  T>   0norO  falseF)	r   r   r   r   rd  r   rs   r,  rt   )r   r  r   r   lowereds        r%   r   z"TelegramAdapter._coerce_bool_extrau  s    .5dk7D.Q.Q[!%%c***W[=NeS!! 	kkmm))++G444t555uNE{{r$   c                 b    t          | dd          si S t          dt          d          iS ddiS )Nr   Flink_preview_optionsT)is_disableddisable_web_page_preview)r   r   r   s    r%   _link_preview_kwargsz$TelegramAdapter._link_preview_kwargs  sD    t5u== 	I)*,>4,P,P,PQQ*D11r$   c                    K   | j         r| j         j        sdS 	 | j         j        j        d         }n# t          $ r Y dS w xY w	 |                                 d{V  n2# t          $ r% t
                              d| j        d           Y nw xY w	 |                                 d{V  t
                              d| j                   dS # t          $ r& t
                              d| j        d           Y dS w xY w)ur  Reset the httpx connection pool used for getUpdates polling.

        Network errors (especially through proxies like sing-box) can leave
        httpx connections in a half-closed state that still occupy pool slots.
        After enough reconnect cycles the pool fills up entirely, causing
        ``Pool timeout: All connections in the connection pool are occupied.``

        We reset ONLY ``_request[0]`` (the getUpdates request) — the general
        request (``_request[1]``) is left untouched so concurrent
        ``send_message`` / ``edit_message`` calls are never interrupted.

        Implementation note: accesses ``Bot._request[0]`` which is the
        get-updates ``BaseRequest`` in the PTB 22.x internal tuple
        ``(get_updates_request, general_request)``.  There is no public
        accessor for the polling request; review if upgrading to PTB 23+.
        Nr   z0[%s] Polling request shutdown failed (non-fatal)Tr   z2[%s] Polling request pool drained before reconnectz5[%s] Polling request re-initialize failed (non-fatal))	r   bot_requestrL   shutdownr-  r.  r   
initialize)r   polling_reqs     r%   _drain_polling_connectionsz*TelegramAdapter._drain_polling_connections  su     " 	 	dim 	F	 )-03KK 	 	 	FF		&&(((((((((( 	 	 	LLB	D      	
		((*********LLDdi      	 	 	LLG	D       	s0   1 
??A ,BB:C ,C=<C=c                   K   | j         rdS d}d}d}| xj        dz  c_        d| _        | j        }||k    r[d|z  }t                              d| j        ||           |                     d	|d
           |                                  d{V  dS t          |d|dz
  z  z  |          }t          	                    d| j        ||||           t          j        |           d{V  	 | j        rA| j        j        r5| j        j        j        r$| j        j                                         d{V  n# t           $ r Y nw xY w|                                  d{V  	 | j        j                            t&          j        d| j                   d{V  t                              d| j        |           d| _        | j         sat          j        |                                           }| j                            |           |                    | j        j                   dS dS # t           $ r}	t          	                    d| j        |	           | j         sft          j        |                     |	                    }
| j                            |
           |
                    | j        j                   Y d}	~	dS Y d}	~	dS d}	~	ww xY w)aK  Reconnect polling after a transient network interruption.

        Triggered by NetworkError/TimedOut in the polling error callback, which
        happen when the host loses connectivity (Mac sleep, WiFi switch, VPN
        reconnect, etc.).  The gateway process stays alive but the long-poll
        connection silently dies; without this handler the bot never recovers.

        Strategy: exponential back-off (5s, 10s, 20s, 40s, 60s cap) up to
        MAX_NETWORK_RETRIES attempts, then mark the adapter retryable-fatal so
        the supervisor restarts the gateway process.
        N
      <   ry   TzXTelegram polling could not reconnect after %d network error retries. Restarting gateway.z[%s] %s Last error: %stelegram_network_error	retryabler'   zK[%s] Telegram network error (attempt %d/%d), reconnecting in %ds. Error: %sFallowed_updatesdrop_pending_updateserror_callbackz>[%s] Telegram polling resumed after network error (attempt %d)r   z*[%s] Telegram polling reconnect failed: %s)has_fatal_errorr   r   r-  r[  r   _set_fatal_error_notify_fatal_errorr   rv  r   sleepr   updaterrunningstoprL   r  start_pollingr
   	ALL_TYPESr   infoensure_future_verify_polling_after_reconnect_background_tasksr  add_done_callbackdiscard_handle_polling_network_error)r   r[  MAX_NETWORK_RETRIES
BASE_DELAY	MAX_DELAYattemptmessagedelayprobe	retry_errtasks              r%   r  z-TelegramAdapter._handle_polling_network_error  s       	F 
	))Q.))#' 3(((&(;<  LL149guMMM!!":Gt!TTT**,,,,,,,,,FJ!!"45yAAYIw 3UE	
 	
 	
 mE"""""""""	y /TY. /493D3L /i',,......... 	 	 	D	 --/////////	G)#11 & 0%*#? 2         
 KKP	7   12D- ' H-d.R.R.T.TUU&**5111''(>(FGGGGGH H  		G 		G 		GNNGT]^^^ ' G,66yAA  &**4000&&t'='EFFFFFFFFFG G G G G G			Gs-   )AD2 2
D?>D?CH& &
K	0BKK	c                 ^  K   d}d}t          j        |           d{V  | j        rdS | j        r| j        j        r| j        j        j        sKt                              d| j        |           | 	                    t          d                     d{V  dS 	 t          j        | j        j                                        |           d{V  d| _        dS # t          $ rH}t                              d| j        ||           | 	                    |           d{V  Y d}~dS d}~ww xY w)a(  Heartbeat probe scheduled after a successful reconnect.

        PTB's Updater can survive a botched stop()+start_polling() cycle
        with `running=True` but a wedged consumer task. No error callback
        fires, so the reconnect ladder doesn't advance on its own. This
        probe detects the wedge by:

        1. Sleeping HEARTBEAT_PROBE_DELAY so a healthy long-poll has time
           to complete at least one cycle.
        2. Verifying `Updater.running` is still True.
        3. Probing the bot endpoint with a tight asyncio timeout. A
           wedged httpx pool fails this probe; a healthy one returns
           well under the timeout.

        On any failure, re-enter the reconnect ladder so the existing
        MAX_NETWORK_RETRIES path can ultimately escalate to fatal-error.
        r  r  NuC   [%s] Updater not running %ds after reconnect — treating as wedgedz-Updater not running after reconnect heartbeatFz;[%s] Polling heartbeat probe failed %ds after reconnect: %s)r   r  r  r   r  r  r-  rv  r   r  RuntimeErrorwait_forr  get_mer   rL   )r   HEARTBEAT_PROBE_DELAYPROBE_TIMEOUT	probe_errs       r%   r  z/TelegramAdapter._verify_polling_after_reconnect  s     $ !#m1222222222 	F	 	di/ 	DI4E4M 	NNU	0   44LMM         F	@"49=#7#7#9#9=IIIIIIIII',D$$$ 	@ 	@ 	@NNM	0)   44Y???????????????	@s   >C 
D,$=D''D,c           
        K   | j         r| j        dk    rd S | xj        dz  c_        d}d| j        dz  z   }| j        |k    rt                              d| j        | j        |||           	 | j        rA| j        j        r5| j        j        j        r$| j        j        	                                 d {V  n# t          $ r Y nw xY wt          j        |           d {V  |                                  d {V  	 | j        j                            t          j        d| j                   d {V  t                              d| j        | j        |           d	| _        d S # t          $ r}t                              d
| j        | j        ||           | j        |k     rFt          j                    }|                    |                     |                    | _        Y d }~d S Y d }~nd }~ww xY wd|t/          d t1          d|dz             D                       fz  }t                              d| j        ||           |                     d|d           	 | j        r0| j        j        r$| j        j        	                                 d {V  n:# t          $ r-}t                              d| j        |d           Y d }~nd }~ww xY w|                                  d {V  d S )Ntelegram_polling_conflictry   r  r  u   [%s] Telegram polling conflict (%d/%d) — previous session still held open on Telegram's servers. Waiting %ds for it to expire. Error: %sFr  z8[%s] Telegram polling resumed after conflict retry %d/%dr   zF[%s] Telegram polling retry %d/%d failed: %s. Scheduling next attempt.aH  Telegram polling could not recover after %d retries (%ds total wait). The previous gateway session is still held open on Telegram's servers, or another process is using the same bot token. To recover: ensure no other Hermes or OpenClaw instance is running with this token, then restart the gateway with 'hermes gateway restart'.c              3   &   K   | ]}d |d z  z   V  dS )r  Nr#   )r~   r   s     r%   r   z;TelegramAdapter._handle_polling_conflict.<locals>.<genexpr>  s*      (a(aa"f(a(a(a(a(a(ar$   z[%s] %s Original error: %sr  zK[%s] Failed stopping Telegram updater after exhausting conflict retries: %sTr   )r  fatal_error_coder   r-  rv  r   r   r  r  r  rL   r   r  r  r  r
   r  r   r  get_running_loopcreate_task_handle_polling_conflictr   sumranger[  r  r  )r   r[  MAX_CONFLICT_RETRIESRETRY_DELAYr  loopr  
stop_errors           r%   r  z(TelegramAdapter._handle_polling_conflict2  s      	D$9=X$X$XF$ 	$$)$$ 
 D82=>'+???NN 	479MU  9 3!2 3ty7H7P 3)+00222222222    -,,,,,,,,,11333333333#i'55$*$4).#'#C 6         
 NIt;=Q   01,   /It;=Q	   /2FFF #355D/3/?/?55i@@0 0D, FFFFF GFFFF:W
 $S(a(aeAG[^_G_>`>`(a(a(a%a%abc 	 	(Iw	
 	
 	
 	97eTTT	y /TY. /i',,......... 	 	 	NN]	:         	
 &&(((((((((((sE   ,AB5 5
CC:A%E! !
G.+A3G))G.7J 
K
#KK

icon_coloricon_custom_emoji_idc                 ,  K   | j         sdS 	 ||d}|||d<   |r||d<    | j         j        di | d{V }|j        }t                              d| j        |||           |S # t          $ r}t          |                                          }	d|	v sd|	v r#t                              d| j        ||           nNd	|	v sd
|	v r#t          	                    d| j        ||           n#t          	                    d| j        |||           Y d}~dS d}~ww xY w)zCreate a forum topic in a private (DM) chat.

        Uses Bot API 9.4's createForumTopic which now works for 1-on-1 chats.
        Returns the message_thread_id on success, None on failure.
        N)r   r   r  r  z5[%s] Created DM topic '%s' in chat %s -> thread_id=%stopic_name_duplicatealreadyzT[%s] DM topic '%s' already exists in chat %s (will be mapped from incoming messages)znot a forumforums_disabledz[%s] Cannot create DM topic '%s' in chat %s: Topics mode is not enabled. The user must open the DM with this bot in Telegram, tap the bot name at the top, and enable 'Topics' in chat settings before topics can be created.z2[%s] Failed to create DM topic '%s' in chat %s: %sr#   )
r   create_forum_topicr8  r-  r  r   rL   r   r,  rv  )
r   r   r   r  r  kwargstopicr  r   
error_texts
             r%   _create_dm_topicz TelegramAdapter._create_dm_topic  s      y 	4#	18$%G%GF%'1|$# F1E-.6$)6@@@@@@@@@@E/IKKG	4)    	 	 	QJ &33yJ7N7NjItW    *,,0AZ0O0Oe ItW	    HItWa   44444+	s   AA$ $
D.BDDparent_chat_idc                    K   	 t          |          }n# t          t          f$ r Y dS w xY w|                     ||           d{V }|rt	          |          ndS )a	  Create a forum topic for a session handoff.

        Works for DM topics (Bot API 9.4+, requires user to enable Topics
        in their chat with the bot) and forum supergroups. Returns the
        ``message_thread_id`` as a string, or ``None`` on failure.
        N)r   )rC  r   r   r  r   )r   r  r   chat_id_intr  s        r%   create_handoff_threadz%TelegramAdapter.create_handoff_thread  s      	n--KK:& 	 	 	44	//$/GGGGGGGG	!*4s9~~~4s    ))
topic_nameforce_createc                 d  K   t          |pd                                          }|sdS 	 t          |          }n# t          t          f$ r Y dS w xY w| d| }| j                            |          }|r|st          |          S d}d}	| j        D ]n}
t          |
                    d                    t          |          k    r6|
}	|
                    dg           D ]}|                    d          |k    r|} n  |rE|                    d          r0|s.t          |d                   }|| j        |<   t          |          S |	|g d}	| j                            |	           |-d|i}|		                    dg                               |           | 
                    |||                    d	          |                    d
                     d{V }|sdS ||d<   t          |          | j        |<   |                     ||t          |          |           t          |          S )zJReturn a private DM topic thread id, creating and persisting it if needed.r   N:r   topicsr   r  r   r   r  r  )r   r  r  )replace_existing)r   rs   rC  r   r   r   r   r  r   
setdefaultr  _persist_dm_topic_thread_id)r   r   r  r  r   r  	cache_keycached
topic_conf
chat_entryentry	candidater  s                r%   ensure_dm_topiczTelegramAdapter.ensure_dm_topic  s     :#$$**,, 	4	g,,KK:& 	 	 	44	 #++T++	$$Y// 	, 	v;;/3
/3
+ 	 	E599Y''((C,<,<<<J"YYx44  	==((D00!*JE 1  	"*..55 	"l 	"J{344I)2DOI&y>>!%0B??J"))*555 $J!!(B//66zBBB//!~~l33!+0F!G!G	 0 
 
 
 
 
 
 
 
	  	4"+
;%(^^	"((dC	NN]i(jjj9~~s   ; AAc                   K   | j         sdS 	 t          |          }n# t          t          f$ r |}Y nw xY w| j                             |t          |          |           d{V  t
                              d| j        |||           dS )z,Rename a forum topic in a private (DM) chat.N)r   r8  r   z5[%s] Renamed DM topic in chat %s thread_id=%s -> '%s')r   rC  r   r   edit_forum_topicr-  r  r   )r   r   r  r   chat_id_args        r%   rename_dm_topiczTelegramAdapter.rename_dm_topic  s       y 	F	"g,,KK:& 	" 	" 	"!KKK	"i((!)nn ) 
 
 	
 	
 	
 	
 	
 	
 	

 	CIw	4	
 	
 	
 	
 	
s    33r  c                    	 ddl m}  |            dz  }|                                s#t                              d| j        |           dS ddl}t          |dd          5 }|                    |          pi }	ddd           n# 1 swxY w Y   |		                    d	i           }
|
	                    d
i           }|	                    di           }|	                    dg           }d}d}|D ]}	 t          |                    dd                    t          |          k    }n# t          t          f$ r d}Y nw xY w|sT|}|	                    dg           D ]T}|                    d          |k    r9|s|                    d          s |                    d          |k    r||d<   d} n/U|	                    dg                               ||d           d} ||                    |||dgd           d}|rt          j        t#          |j                  dd          \  }}	 t'          j        |dd          5 }|                    |	|dd           |                                 t'          j        |                                           ddd           n# 1 swxY w Y   t3          ||           n5# t4          $ r( 	 t'          j        |           n# t8          $ r Y nw xY w w xY wt                              d| j        ||           dS dS # t<          $ r.}t                              d| j        |d           Y d}~dS d}~ww xY w)zTSave a newly created thread_id back into config.yaml so it persists across restarts.r   get_hermes_homeconfig.yamlz:[%s] Config file not found at %s, cannot persist thread_idNrutf-8encoding	platformsrM   r   r   Fr   r   r   r  T)r   r  r  .tmpz.config_)dirsuffixprefixw)default_flow_style	sort_keysz9[%s] Persisted thread_id=%s for topic '%s' in config.yamlz.[%s] Failed to persist thread_id to config: %sr   )hermes_constantsr  existsr-  rv  r   yamlopen	safe_loadr  rC  r   r   r   r   tempfilemkstempr   parentr   fdopendumpflushfsyncfilenor;   BaseExceptionunlinkr  r  rL   )r   r   r  r  r  r  config_path_yamlfr   r  telegram_configr   r   changedmatching_chat_entryr  chat_matchestfdtmp_pathr   s                         r%   r  z+TelegramAdapter._persist_dm_topic_thread_id3  s   I	j888888)/++m;K%%'' []a]fhsttt    k3999 2Q++1r2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ))+r::I'22:rBBO#..w;;E((b99IG"&'  
)#&z~~i'C'C#D#DG#TLL!:. ) ) )#(LLL)# &0##..x<< # #AuuV}}
22+ /1553E3E / uu[11Y>>1:+*. 3 ))(B77>>!+)DD   #G"*  &(2KKL" "     '/K.//!%     H
2sW=== -

61RW
XXX			,,,- - - - - - - - - - - - - - - #8[9999$   	(++++"    OIy*    % ,  	j 	j 	jNNKTYXYdhNiiiiiiiii	js   AL L "B:L B

L B
A#L 24D'&L 'D=:L <D==C1L /K AJ&K &J**K -J*.K L 
K4K"!K4"
K/,K4.K//K44%L 
M'#MMc           	        K   | j         sdS | j         D ]}|                    d          }|                    dg           }|r|s3t                              d| j        t          |          |           |D ]}|                    d          }|s| d| }|                    d          }|r:t          |          | j        |<   t                              d| j        ||           s|                    d	          }|                    d
          }	|                     t          |          |||	           d{V }
|
r|
| j        |<   t                              d| j        ||
           | 	                    t          |          ||
           	 | j
                            t          |          |
d|            d{V  Q# t          $ r-}t                              d| j        ||           Y d}~d}~ww xY wdS )u  Load or create configured DM topics for specified chats.

        Reads config.extra['dm_topics'] — a list of dicts:
        [
            {
                "chat_id": 123456789,
                "topics": [
                    {"name": "General", "icon_color": 7322096, "thread_id": 100},
                    {"name": "Accessibility Auditor", "icon_color": 9367192, "skill": "accessibility-auditor"}
                ]
            }
        ]

        If a topic already has a thread_id in the config (persisted from a previous
        creation), it is loaded into the cache without calling createForumTopic.
        Only topics without a thread_id are created via the API, and their thread_id
        is then saved back to config.yaml for future restarts.
        Nr   r   z*[%s] Setting up %d DM topic(s) for chat %sr   r  r  z4[%s] DM topic loaded from config: %s -> thread_id=%sr  r  )r   r   r  r  z([%s] DM topic cached: %s -> thread_id=%su   📌 )r   r8  rd   z2[%s] Could not send seed message to topic '%s': %s)r  r   r-  r  r   r   rC  r   r  r  r   send_messagerL   r.  )r   r  r   r   r  r  r  existing_thread_idr  
icon_emojir  seed_errs               r%   _setup_dm_topicsz TelegramAdapter._setup_dm_topics  s     & % 	F0 <	 <	J nnY//G^^Hb11F & KK<	3v;;  
 % 1 1
'^^F33
! &5555	 &0^^K%@%@"% 145G1H1HDOI.KKN	9.@    (^^L99
'^^,BCC
"&"7"7LL#))3	 #8 # #      	  1:DOI.KKB	9i  
 44S\\:yYYY
"i44$'LL.7!;z!;!; 5          
 %   P Iz8       #91<	 <	s   3G
G?"G::G?c           
      v   '(K   t           s"t                              d j                   dS  j        j        s"t                              d j                   dS 	                      d j        j        d          sdS t          j                                         j        j                  } j        j	        
                    d          }|ri|                    |          }|                     j        j	        
                    d|                    }t                              d j        |            j        j	        
                    d	          r5|                    d
          }t                              d j                   dt          dt           dt           fd}dt          dt"          dt"          fd} |dd           |dd           |dd           |dd           |dd          d}t%          j        dd                                                                          dv }                                 }|sHt/                       d{V }t                              d j        d                     |                     d!g|}t3          d"|#          }	|rw|	su|sst                              d$ j        d                     |                     t5          dTi |d%d&t7          |          ii}
t5          dTi |d%d&t7          |          ii}n~|	rBt                              d' j        |	           t5          dTi |d(|	i}
t5          dTi |d(|	i}n:|r t                              d) j                   t5          dTi |}
t5          dTi |}|                    |
                              |          }|                                 _         j        j          _!         j        "                    tG          tH          j%        tH          j&         z   j'                              j        "                    tG          tH          j&         j(                              j        "                    tG          tH          j)        tU          tH          d*tH          j)                  z   j+                              j        "                    tG          tH          j,        tH          j-        z  tH          j.        z  tH          j/        z  tH          j0        j1        z  tH          j2        j1        z   j3                              j        "                    ti           j5                             	 d+d,l6m7}m8} n# tr          $ r tt          x}}Y nw xY wd-}tw          |          D ]}	  j        <                                 d{V   ny# ||tt          f$ ri}||d.z
  k     rUt{          d/|z  d0          }t          >                    d1 j        |d.z   |||           t          j@        |           d{V  n Y d}~d}~ww xY w j        A                                 d{V  t%          j        d2d                                          }|rt!          t%          j        d3d4                    }t%          j        d5d                                          }|st          d6          d+d7lCmD}  ||          jE        pd8} j        jF        G                    d9||||t          jI        d
:           d{V  d
 _J        t                              d; j        ||           ntU           j!        d<d          }t          |          r |d=           d{V  t          jL                    (d>t          ddf( fd?}| _N         j        jF        O                    t          jI        d
|@           d{V  	 d+dAlPmQ'mR}mS}mT} d+dBlUmV}  |t          C          \  }}'fdD|D             }|||fD ]} | jX        }!	  j!        Y                    | |             E           d{V  t                              dF j        |!t          |                     d# t          $ r,}"t          >                    dG j        |!|"           Y d}"~"d}"~"ww xY w|r0t                              dH j        t          |          |dI           n:# t          $ r-}#t          >                    dJ j        |#d
K           Y d}#~#nd}#~#ww xY w [                                  jJ        rdLndM}$t                              dN j        |$           	  \                                 d{V  n:# t          $ r-}%t          >                    dO j        |%d
K           Y d}%~%nd}%~%ww xY wd
S # t          $ r_}# ]                                 dP|# }& ^                    dQ|&d
R           t                              dS j        |#d
K           Y d}#~#dS d}#~#ww xY w)Ua_  Connect to Telegram via polling or webhook.

        By default, uses long polling (outbound connection to Telegram).
        If ``TELEGRAM_WEBHOOK_URL`` is set, starts an HTTP webhook server
        instead.  Webhook mode is useful for cloud deployments (Fly.io,
        Railway) where inbound HTTP can wake a suspended machine.

        Env vars for webhook mode::

            TELEGRAM_WEBHOOK_URL    Public HTTPS URL (e.g. https://app.fly.dev/telegram)
            TELEGRAM_WEBHOOK_PORT   Local listen port (default 8443)
            TELEGRAM_WEBHOOK_SECRET Secret token for update verification
        zL[%s] python-telegram-bot not installed. Run: pip install python-telegram-botFz[%s] No bot token configuredztelegram-bot-tokenzTelegram bot tokenr   base_file_urlz'[%s] Using custom Telegram base_url: %s
local_modeTz5[%s] Using Telegram local_mode (read files from disk)r   r   rF   c                     	 t          t          j        | t          |                              S # t          t
          f$ r |cY S w xY wr   )rC  r   r   r   r   r   r   r   s     r%   _env_intz)TelegramAdapter.connect.<locals>._env_int  sP    #rys7||<<===!:. # # #"NNN#   .1 AAc                     	 t          t          j        | t          |                              S # t          t
          f$ r |cY S w xY wr   )r   r   r   r   r   r   rC  s     r%   
_env_floatz+TelegramAdapter.connect.<locals>._env_float  sP    # 4W!>!>???!:. # # #"NNN#rE  HERMES_TELEGRAM_HTTP_POOL_SIZEi   !HERMES_TELEGRAM_HTTP_POOL_TIMEOUTg       @$HERMES_TELEGRAM_HTTP_CONNECT_TIMEOUTg      $@!HERMES_TELEGRAM_HTTP_READ_TIMEOUTg      4@"HERMES_TELEGRAM_HTTP_WRITE_TIMEOUT)connection_pool_sizepool_timeoutconnect_timeoutread_timeoutwrite_timeout$HERMES_TELEGRAM_DISABLE_FALLBACK_IPSr   >   r   r  r$  r%  Nz.[%s] Auto-discovered Telegram fallback IPs: %s, zapi.telegram.orgTELEGRAM_PROXY)target_hostsz%[%s] Telegram fallback IPs active: %shttpx_kwargs	transportz;[%s] Proxy detected; passing explicitly to HTTPXRequest: %sproxyz4[%s] Telegram fallback-IP transport disabled via envVENUEr   r     ry   r'      u9   [%s] Connect attempt %d/%d failed: %s — retrying in %dsTELEGRAM_WEBHOOK_URLTELEGRAM_WEBHOOK_PORT8443TELEGRAM_WEBHOOK_SECRETu  TELEGRAM_WEBHOOK_SECRET is required when TELEGRAM_WEBHOOK_URL is set. Without it, the webhook endpoint accepts forged updates from anyone who can reach it — see https://github.com/NousResearch/hermes-agent/security/advisories/GHSA-3vpc-7q5r-276h.

Generate a secret and set it in your .env:
  export TELEGRAM_WEBHOOK_SECRET="$(openssl rand -hex 32)"

Then register it with Telegram when setting the webhook via setWebhook's secret_token parameter.)urlparsez	/telegramz0.0.0.0)listenporturl_pathwebhook_urlsecret_tokenr  r  z-[%s] Webhook server listening on 0.0.0.0:%d%sdelete_webhook)r  r[  c                    j         rj                                         sd S                     |           r/                                        |                     _         d S                     |           rPt                              dj        |                                	                    |                     _         d S t          
                    dj        | d           d S )Nz5[%s] Telegram network error, scheduling reconnect: %sz[%s] Telegram polling error: %sTr   )r   doner  r  r  r  r-  rv  r   r  r[  )r[  r  r   s    r%   _polling_error_callbackz8TelegramAdapter.connect.<locals>._polling_error_callback  s    / 8P8U8U8W8W 88?? i373C3CDDaDabgDhDh3i3i00077>> i'^`d`ikpqqq373C3CDDfDfglDmDm3n3n000%F	SXcghhhhhr$   r  )
BotCommandBotCommandScopeAllPrivateChatsBotCommandScopeAllGroupChatsBotCommandScopeDefaulttelegram_menu_commandsmax_commandsc                 .    g | ]\  }} ||          S r#   r#   r~   r   descrj  s      r%   r   z+TelegramAdapter.connect.<locals>.<listcomp>  )    WWW:4

4 6 6WWWr$   scopez.[%s] set_my_commands OK for scope %s (%d cmds)z,[%s] set_my_commands FAILED for scope %s: %szc[%s] Telegram menu: %d commands registered, %d hidden (over %d limit). Use /commands for full list.rE   z1[%s] Could not register Telegram command menu: %sr   webhookpollingz$[%s] Connected to Telegram (%s mode)z+[%s] DM topics setup failed (non-fatal): %szTelegram startup failed: telegram_connect_errorr  z&[%s] Failed to connect to Telegram: %sr#   )_rJ   r-  r[  r   r   token_acquire_platform_lockr   builderr   r   r   r@  r  rA  r   rC  r   r   r   rs   r,  r  r9   r   r3   r   r8   requestget_updates_requestbuildr   r  r   add_handlerrR   r   TEXTCOMMAND_handle_text_message_handle_commandLOCATIONr   _handle_location_messagePHOTOVIDEOAUDIOVOICEDocumentALLSticker_handle_media_messager   _handle_callback_queryrc  r  r  rN   r  r  r  r   rv  r   r  r   r  urllib.parser`  pathr  start_webhookr
   r  r   r*  r  rL   r   r  rM   rj  rk  rl  rm  hermes_cli.commandsro  MAX_COMMANDS_PER_SCOPEr   set_my_commandsr   _mark_connectedr>  _release_platform_lockr  ))r   r}  custom_base_urlrD  rG  request_kwargsdisable_fallbackr}  proxy_targets	proxy_urlr~  r  r  r  _max_connect_attemptinit_errwaitrd  webhook_portwebhook_secretr`  webhook_pathrf  ri  rk  rl  rm  ro  menu_commandshidden_countbot_commands	scope_cls
scope_name	scope_errr   mode
topics_errr  rj  r  s)   `                                      @@r%   connectzTelegramAdapter.connect  s      " 	LL^	   5{  	LL7CCC5R	../CT[EVXlmm u ")++11$+2CDDG"k/33J??O !**?;;!//K%))/?KK  =I   { $$\22 `!,,T22SUYU^___
#s #S #S # # # ## #u # # # # # )11QSV(W(W *
+NPS T T#-:.TVZ#[#[ *
+NPT U U!+,PRV!W!W N !#	*PRT U U [ [ ] ] c c e e  jD  !D--//L %:%<%<<<<<<<DIIIl++   0?,?M)*:WWWI EI E6F E;IIIl++   '  $ "-/H/V/V!W   '3 ' '$' '"-/H/V/V!W' ' '##  EY[_[dfoppp&IIIIyIII&2&U&U^&U&U9&U&U&U### cKK VX\Xabbb&8888&2&D&D^&D&D#oog..BBCVWWGDI	DI I!!"8//)# #    I!!"8$# #    I!!"8 77GW=M#N#NN-# #    I!!"8-=MPWP`Pddgngvgzz*# #   
 I!!"6t7R"S"STTT2AAAAAAAAA 2 2 2*11xxx2L!,//  )..000000000E$h8 	 	 	,"222"1="55W Ix!|\8T   &mD1111111111 21111	 )//######### )$:B??EEGGK H  #29-Df#M#MNN!#+Db!I!I!O!O!Q!Q% &	K   211111'x449H[i'55$%) +!/$*$4)- 6          &*"CI|\    ")4Dd!K!KN++ E(.eDDDDDDDDDD/11	i9 	i 	i 	i 	i 	i 	i 	i 	i 4K0i'55$*$4)-#: 6         &            GFFFFF /E.DRh.i.i.i+|WWWWWWW #9:XZv!w y yI!*!3Jy"i77IIKK7XXXXXXXXX$TVZV_akmpq}m~m~$ y y y'UW[W`blnwxxxxxxxxy   KK}	3}#5#5|R      GI!	            """ $ 2A99	DKK>	4PPP
++----------   AIzD          4 	 	 	'')))5!55G!!":Gt!TTTLLA49aZ^L___55555	s   !g S4g 6U? >g ?Vg Vg ,Wg YAX>9g >YF0g 4Ac= 7Abc= 
c"c=c= c5c= <g =
d4#d/*g /d44Ag 8f g 
g
#g g g

g 
h8Ah33h8c                 V  K   t          | j                                                  }|D ]}|                                 |rt	          j        |ddi d{V  | j                                         | j                                         | j        r	 | j        j	        r5| j        j	        j
        r$| j        j	                                         d{V  | j        j
        r| j                                         d{V  | j                                         d{V  n:# t          $ r-}t                              d| j        |d           Y d}~nd}~ww xY w|                                  | j                                        D ],}|r(|                                s|                                 -| j                                         | j                                         |                                  d| _        d| _        t                              d| j                   dS )zCStop polling/webhook, cancel pending album flushes, and disconnect.return_exceptionsTNz)[%s] Error during Telegram disconnect: %sr   z[%s] Disconnected from Telegram)listr   valuescancelr   gatherclearr   r   r  r  r  r  rL   r-  rv  r   r  r   rh  r   _mark_disconnectedr   r  )r   pending_media_group_tasksr  r   s       r%   
disconnectzTelegramAdapter.disconnect  sY     $()@)G)G)I)I$J$J!- 	 	DKKMMMM$ 	U.";TtTTTTTTTTT%%''' &&(((9 		ii9$ 3):)B 3)+002222222229$ +)..*********i((********** i i iJDIWXcghhhhhhhhi##%%%3::<< 	 	D DIIKK '--///#))+++!!!		5tyAAAAAs   BD! !
E+#EEchunk_indexc                 D    |sdS | j         }|dk    rdS |dk    rdS |dk    S )a3  Determine if this message chunk should thread to the original message.

        Args:
            reply_to: The original message ID to reply to
            chunk_index: Index of this chunk (0 = first chunk)

        Returns:
            True if this chunk should be threaded to the original message
        FrO  allTr   )r   )r   rD  r  r  s       r%   _should_thread_replyz$TelegramAdapter._should_thread_reply(  s@      	5"5==5U]]4!##r$   contentc                   K   | j         st          dd          S t          | dd          rt          ddd          S |r|                                st          dd	          S 	 |                     |          }|                     || j        t          
          }t          |          dk    rd |D             }g }| 	                    |          }| 
                    |          }	d}
	 ddlm} n# t          $ r
 t          }Y nw xY w	 ddlm} n# t          $ r d}Y nw xY w	 ddlm} n# t          t"          f$ r d}Y nw xY wt%          |          D ]\  }}d}|                     |          }|                     |||          }|o.| j        dk    o#t-          |o|                    d                    }|p|r|t1          |          nd}|r|duo
| j        dk    }n|                     ||          }|r|rt5          |          nd}|r*|(|s&t          d|                                 d          c S |                     ||||| j                  }|
r)|                    d          t;          |          }d|d<   |                    d          }d}t=          d          D ]}	 	  | j         j        d/t5          |          |t@          j!        |d|| "                                | #                    |           d{V }n# tH          $ r}dt1          |          %                                v s#dt1          |          %                                v rtL          '                    d| j(        |           tS          |          } | j         j        d/t5          |          |d|d|| "                                | #                    |           d{V }n Y d}~nd}~ww xY w n# |$ r&}|rtU          ||          r}| +                    |          r||s|r=|                    d          r(t          dt1          |          d          cY d}~c c S |s)d}tL          '                    d| j(        |           Y d}~tL          '                    d| j(        |           d}
d}ddi}Y d}~t1          |          %                                }d|v r||r(t          dt1          |          d          cY d}~c c S tL          '                    d| j(        |           d}|r|                    d          ri }d}n4|                     ||||| j                  }|                    d          }Y d}~ނ |r;tU          ||          r+| ,                    |          s| -                    |          s |dk     rFd|z  }tL          '                    d | j(        |dz   ||           t]          j/        |           d{V  n Y d}~od}~wtH          $ r}t          |d!d          } | #d"t1          |          %                                v r_|dk     rY| ta          |           nd#}tL          '                    d$| j(        |dz   ||           t]          j/        |           d{V  Y d}~ d}~ww xY w|1                    t1          |j2                             	 | 3                    ||%           d{V  n# tH          $ r Y nw xY wt          d|r|d         nd||	|
d&'          S # tH          $ r}!tL          4                    d(| j(        |!d)           t1          |!          %                                }"d*|"v sd+|"v r6tL          5                    d,| j(                   t          dd*          cY d}!~!S tm                                          d-          }#|#rtU          |!|#          pd.|"v }$| ,                    |!          }%| -                    |!          }&t          dt1          |!          |%p|&p|$           cY d}!~!S d}!~!ww xY w)0z"Send a message to a Telegram chat.FNot connectedsuccessr[  r   send_path_degradedTr  r[  r  Nr  
message_idlen_fnry   c                 :    g | ]}t          j        d d|          S )z \((\d+)/(\d+)\)$z \\(\1/\2\\)rl   )r~   chunks     r%   r   z(TelegramAdapter.send.<locals>.<listcomp>Y  s7        F/%HH  r$   r   )r  ra  )r  rO  rI  rQ  r   r8  r   r   rd   
parse_moderQ  parsemarkdownz<[%s] MarkdownV2 parse failed, falling back to plain text: %srJ  z;[%s] Thread %s not found, retrying once with same thread_idz<[%s] Thread %s not found, retrying without message_thread_idrg  z8[%s] Reply target deleted, retrying without reply_to: %sr'   z>[%s] Network error on send (attempt %d/3), retrying in %ds: %sretry_afterretry afterr   zI[%s] Telegram flood control on send (attempt %d/3), retrying in %.1fs: %sr  )message_idsrequested_thread_idthread_fallback)r  r  raw_responsez([%s] Failed to send Telegram message: %sr   message_too_longtoo longzF[%s] send() content too long, falling back to new-message continuation	_TimedOut	timed outr#   )7r   r.   r   rs   format_messagetruncate_messageMAX_MESSAGE_LENGTHr7   r   r;  rS  rc  r  rN   r  rb  r  AttributeErrorr   rE  rK  r   rt   r   r   r  rC  rM  rU  rw  r  r:  r   MARKDOWN_V2r  r  rL   r,  r-  rv  r   ro   rd  r^  r  r  r   r  r   r   r  send_typingr[  r.  locals)'r   r   r  rD  r  	formattedchunksr  r  r  used_thread_fallback_NetErr_BadReqr  r   r  retried_thread_not_foundmetadata_reply_toprivate_dm_topic_senddm_topic_reply_to_offreply_to_sourceshould_threadreply_to_idthread_kwargseffective_thread_idmsg_send_attemptmd_errorplain_chunkry  rm  r  r  r   err_str_to
is_timeoutis_connect_timeoutis_pool_timeouts'                                          r%   sendzTelegramAdapter.send<  s      y 	De?CCCC 4.66 	Ye3GSWXXXX  	=gmmoo 	=dt<<<<@	@++G44I**429 +  F 6{{Q !'  
 K00::I"&"B"B9"M"M#( "BBBBBBB " " "!"@@@@@@@   !@@@@@@@0 ! ! ! 			! &f-- s8 s85+0($($F$Fx$P$P!(,(F(FwPY[c(d(d% * \+u4\XZ(,,7Y*Z*Z[[ &
 #+ #.CoHYHeC)***ko   ) R't3 9 /58 "M
 %)$=$=oq$Q$QM6Caac/222]a( [-@I^-@% %"AACC"'     
 !% < <(3"&"5 != ! ! ( >M,=,=>Q,R,R,^$($7$7M9=M"56&3&7&78K&L&L#%*1XX F FME&(>	(> )(+G%*+4+@4?	) )
 #0) #'";";"="=) #'";";H"E"E) ) # # # # # #CC  ) & & &&#h--*=*=*?*???:QTU]Q^Q^QdQdQfQfCfCf &/mosox  {C  !D  !D  !D.9%.@.@,BDI,B -",/LL)4/38C	-" -"
 '4-" '+&?&?&A&A-" '+&?&?&I&I-" -" '" '" '" '" '" '" !& !$&  " Y" Y" Y"
 # ?"z(G'D'D ?"#>>xHH )M`Ml#8 !&X !&(,,W{J|J| !&+505.1(mm27,& ,& ,& %& %& %& %& %& %& %& %& %& %& (@ !-?C$<$*NN(e(,	3F%& %& %& %-HHHH !'$b$(I/B!" !" !" 8< 46: 31Dd0K ((+H(;(;(=(=I@IMMR]Ri#8 !&+505.1(mm27,& ,& ,& %& %& %& %& %& %& %& %& %& %& !'$^$(Ix!" !" !" /3#+ !a=_0`0` !a46M:>$7$7484P4P(/(1(0<G6:6I 5Q 5& 5&M ;H:K:KL_:`:`$7 (! &" *8Y ? ?" %)$D$DX$N$N" %)$A$A($K$K	" "(1,,#$#5D"NN+k+/9ma6GxY Y Y")-"5"555555555! 65555 %   &-ht&L&L&2ms8}}GZGZG\G\6\6\,q00=H=Tu['9'9'9Z] &$o$(I$1A$5$($,!" !" !" '.mD&9&9 9 9 9 9 9 9 9 ( ""3s~#6#67777&&w&BBBBBBBBBB    -8B;q>>d#.+>';      	@ 	@ 	@LLCTYPQ\`Laaa!ffllnnG "W,,
g0E0E\I   "%7IJJJJJJJJJ ((,,{++C4*Q"4"4O9OJ!%!A!A!!D!D";;A>>Oe3q66FXF}\kF}s}o})	@sz  &B\: +C2 1\: 2D\: D\: 
D \: D \: D  \: $D+ *\: +E>\:  EC6\: 8A3\: -AL
O0
O*CO% O0%O**O0-\: 0Z?6A"XZ?\: "%X\: )X6\: <AXZ?\: A3X \: BX\: Z?$BZ:3\: 9Z::Z??,\: ,\
 	\: 

\\: \"\: :aA<aaBaaar  
status_keyc                  K   t          |          t          |          f}| j                            |          }|g|                     |||d|           d{V }|j        r%|j        rt          |j                  | j        |<   |S | j                            |d           |                     |||           d{V }|j        r#|j        rt          |j                  | j        |<   |S )a  Send a status message, or edit the previous one with the same key.

        Issue #30045: progress/status callbacks (context-pressure, lifecycle,
        compression, etc.) used to append a fresh bubble on every call. With
        this method, the first call sends and the message id is remembered;
        subsequent calls with the same (chat_id, status_key) edit that same
        message in place. If the edit fails (message deleted, too old, etc.)
        we drop the cached id and send fresh.
        NTfinalizer  r  )r   r	  r   edit_messager  r  rx  r  )r   r   r  r  r  r  	cached_idresults           r%   send_or_update_statusz%TelegramAdapter.send_or_update_statusQ  s'     " 7||S__-,0055	 ,,GdX -        F ~ $ K478I4J4JD,S1$((d333yy'HyEEEEEEEE> 	Cf/ 	C,/0A,B,BD$S)r$   r  r  r  c                  K   | j         st          dd          S t          |          | j        k    r |                     |||||           d{V S 	 |sN| j                             t          |          t          |          |           d{V  t          d|          S |                     |          }	 | j                             t          |          t          |          |t          j	        	           d{V  n# t          $ r}d
t          |                                          v rt          d|          cY d}~S t                              d| j        |           |rt!          |          n|}| j                             t          |          t          |          |           d{V  Y d}~nd}~ww xY wt          d|          S # t          $ rt}	t          |	                                          d
v rt          d|          cY d}	~	S dv sdv rYt                              d| j        t          |          | j                   |                     |||||           d{V cY d}	~	S t%          |	dd          }
|
dv r|
r|
nd}t                              d| j        |           |dk    rt          dd|           cY d}	~	S t'          j        |           d{V  	 | j                             t          |          t          |          |           d{V  t          d|          cY d}	~	S # t          $ rN}t                              d| j        |           t          dt          |                    cY d}~cY d}	~	S d}~ww xY wd}t-          fd|D                       }|rFt                              d| j        ||	           t          dt          |	          d          cY d}	~	S t                              d| j        ||	d           t          dt          |	                    cY d}	~	S d}	~	ww xY w)a"  Edit a previously sent Telegram message.

        Telegram caps single-message text at 4096 UTF-16 codeunits.  Streaming
        replies that grow past this limit must NOT be silently truncated and
        must NOT return failure (the consumer would re-send and create a
        duplicate).  Instead this method split-and-delivers: edit the
        existing message with the first chunk and send the rest as
        continuation messages, returning the final chunk's id so subsequent
        edits target the most recent visible message.
        Fr  r  r  Nr   r  rd   Tr  r   r  rd   r  not modifiedz;[%s] MarkdownV2 edit failed, falling back to plain text: %sr  r  z6[%s] edit_message overflow (%d UTF-16 > %d), splittingr  r  r   z*[%s] Telegram flood control, waiting %.1fsg      @zflood_control:z+[%s] Edit retry failed after flood wait: %s)connecterrorzconnect errorzconnection errorr  znetwork errorr  readtimeoutwritetimeoutzserver disconnectedztemporarily unavailableztemporary failurehttpxc              3       K   | ]}|v V  	d S r   r#   )r~   mr  s     r%   r   z/TelegramAdapter.edit_message.<locals>.<genexpr>  s'      IIWIIIIIIr$   z@[%s] Transient network error editing message %s (will retry): %sr  z+[%s] Failed to edit Telegram message %s: %sr   )r   r.   r7   r  _edit_overflow_splitedit_message_textrC  r  r   r  rL   r   r,  r-  rv  r   ro   r.  r   r   r  r[  rn  )r   r   r  r  r  r  r  fmt_err_plainr   r  r  r  _transient_markers_is_transientr  s                  @r%   r  zTelegramAdapter.edit_messages  s     & y 	De?CCCC W 77722Wx( 3         n	; Gi11LL":  2         
 "$:FFFF++G44Ii11LL":"(4	 2              !S\\%7%7%9%999%dzJJJJJJJJJQI  
 29EW---gi11LL": 2                dzBBBB L	; L	; L	;!ffllnnG((!$:FFFFFFFFF "W,,
g0E0ELIy1143J   "66Z8h 7               "!]D99K&-7*B*B&1:{{s@It   #::%e;RD;R;RSSSSSSSSSmD)))))))))K)55 #G#&z??$ 6         
 &dzJJJJJJJJJ  K K KLLE	9   &e3y>>JJJJJJJJJJJJJJK"  IIII6HIIIIIM OVI	   "%s1vvNNNNNNNNNLL=	     e3q66:::::::::YL	;s   AG %G ;AD G 
G3F=GG A1F=8G =GG Q!5QQAQ7Q=AQQQ6AM	Q	
N!>NN!QQN!!A"QQ	AQ
QQc                :  K   |                      || j        t                    }t          |          dk    r|g}|d         }	 |r|                     |          }	 | j                            t          |          t          |          |t          j	                   d{V  n# t          $ r}	dt          |	                                          vrkt                              d| j        |	           | j                            t          |          t          |          t!          |                     d{V  Y d}	~	nEd}	~	ww xY w| j                            t          |          t          |          |           d{V  n# t          $ rv}
t          |
                                          }d|v rnFt                              d	| j        |
d
           t%          dt          |
                    cY d}
~
S Y d}
~
nd}
~
ww xY wg }|g}|}|                     |          }|dd         D ]}d}|rt          |          nd}|                     ||||          }|rdndD ]}	 |r|                     |          }n|rt!          |          n|} | j        j        d!t          |          ||rt          j	        nd|d||                                 |                     |           d{V } nA# t          $ r3}dt          |                                          v r|r|                    d          ri n|                     |||d          }	  | j        j        d!t          |          |rt!          |          n|d||                                 |                     |           d{V }Y d}~ nx# t          $ r2}t                              d| j        |           d}Y d}~Y d}~ nAd}~ww xY w|rY d}~t                              d| j        |           d}Y d}~ nd}~ww xY w|t                              d| j        dt          |          z   t          |                     d                    d |D                       }t%          d|dd
d
dt          |          z   t          |          ||t5          |          dt5          |                    c S t          t7          |dd                    p|}|                    |           |                    |           |}|r|d         n|}t                              d| j        dt          |          z   |           t%          d
|t5          |                     S )"u  Split an oversized edit across the existing message + continuations.

        Edit the original ``message_id`` with chunk 1 (with the platform's
        usual ``(1/N)`` suffix preserved), then send the remaining chunks as
        new messages threaded as replies to the previous chunk so the user
        sees them grouped.  Returns ``SendResult(success=True,
        message_id=<last-chunk-id>, continuation_message_ids=(...))`` so the
        stream consumer can keep editing the most recent visible message
        and the gateway has full visibility into every message id we put on
        screen.

        Falls back to ``SendResult(success=False)`` only if even the first-
        chunk edit fails — that's a real adapter problem, not an overflow.
        r  ry   r   r  Nr  zW[%s] Overflow split: MarkdownV2 first-chunk edit failed, falling back to plain text: %sr  z0[%s] Overflow split: first-chunk edit failed: %sTr   Fr  rQ  TFFr  zreply message not foundrI  )r   rd   z4[%s] Overflow continuation no-reply retry failed: %sz*[%s] Overflow continuation send failed: %sz6[%s] Overflow split: stopped at %d/%d chunks deliveredr   c              3   B   K   | ]}t          j        d d|          V  dS )z \(\d+/\d+\)$r   Nrl   )r~   	delivereds     r%   r   z7TelegramAdapter._edit_overflow_split.<locals>.<genexpr>	  sE       + +! F+R;;+ + + + + +r$   overflow_continuation_failed)partial_overflowdelivered_chunkstotal_chunkslast_message_iddelivered_prefixcontinuation_message_ids)r  r  r[  r  r  r  r  rz   z3[%s] Overflow split delivered %d chunks; last_id=%s)r  r  r  r#   )r  r  r7   r   r  r   r  rC  r   r  rL   r   r,  r-  rv  r   ro   r[  r.   r;  rU  r:  r  r  r   r   tupler   r   r.  )r   r   r  r  r  r  r  first_chunkr  r  r   r  continuation_idsr  prev_idr  r  sent_msgr  r  use_markdownrd   ry  retry_thread_kwargs
_retry_errr  new_idlast_ids                               r%   r
  z$TelegramAdapter._edit_overflow_split 	  s}     . &&T,Y ' 
 
 v;;! YF Qi)	?  !//<<	)55 #G#&z??&#,#8	 6           !   %S\\-?-?-A-AAAE Iw  
 #i99$'LL'*:!,[!9!9 :          i11LL":$ 2         
  	? 	? 	?!ffllnnG(( FIq4     "%s1vv>>>>>>>>> 	?& ')'=,,X66	ABBZ c	 c	EH*1;#g,,,tK 88$/	 9  M 2: Gx 8 87# I#22599 6>H{51115%;TY%; & #G!<H#R9#8#8d,7	& &
 (& 3355& 33H==& &            H E  $ $ $0CMM4G4G4I4III  (,4LL9[,\,\BB!%!=!= 'HRV "> " " ,"-CTY-C .(+G;C%N[%7%7%7. . #6. #'";";"="=	.
 #'";";H"E"E. . ( ( ( ( ( (H "EEEEE( " " ""NN V $	:   (,H!EEEEEEEEE" $ ! NND	8    $HEEEEEI$J  LIq3'7#8#88#f++   $&77 + +%5+ + + $ $  "!&8",0,-4D0E0E,E(+F+2,<49:J4K4K" " .33C-D-D      <<<==HF##F+++##E***GG*:J"2&&
AIq3/000'	
 	
 	
 %*+;%<%<
 
 
 	
s   F	 AB& %F	 &
E0BE>F	 EA F	 	
H	A&H9H	H	-BK<<P:AP5A"O
P	#O<	2P5<P	P5#P55P:c                 
  K   | j         sdS 	 | j                             t          |          t          |                     d{V  dS # t          $ r-}t                              d| j        ||           Y d}~dS d}~ww xY w)u  Delete a previously sent Telegram message.

        Used by the stream consumer's fresh-final cleanup path (ported
        from openclaw/openclaw#72038) to remove long-lived preview
        messages after sending the completed reply as a fresh message.
        Telegram's Bot API ``deleteMessage`` works for bot-posted
        messages in the last 48 hours.  Failures are non-fatal — the
        caller leaves the preview in place and logs at debug level.
        F)r   r  NTz-[%s] Failed to delete Telegram message %s: %s)r   delete_messagerC  rL   r-  r.  r   r   r   r  r   s       r%   r(  zTelegramAdapter.delete_message	  s       y 	5	)**Gz?? +          4 	 	 	LL?	:q   55555	s   <A 
B"A==Bc                 n    | j         rt          | j         d          sdS |pd                                dv S )a  Telegram supports sendMessageDraft for private chats only.

        Bot API 9.5 (March 2026) opened ``sendMessageDraft`` to all bots
        unconditionally for private (DM) chats.  Groups, supergroups, and
        channels still rely on the edit-based path.

        We additionally require ``self._bot`` to expose ``send_message_draft``
        (added to python-telegram-bot in 22.6); older PTB installs gracefully
        fall back to the edit path even on DMs.
        send_message_draftFr   >   r  r  )r   hasattrr,  )r   r  r  s      r%   supports_draft_streamingz(TelegramAdapter.supports_draft_streaming	  sC     y 		3G H H 	5R&&((,===r$   draft_idc           	        K   | j         st          dd          S t          | j         d          st          dd          S t          |          | j        k    r|n'|                     || j        t                    d         }|                     |          }dD ]*}t          |          t          |          |r| 	                    |          n|d	}|rt          j        |d
<   |||d<   	  | j         j        di | d{V }	|	rt          dd          c S t          dd          c S # t          $ r}
|r=|                     |
          r(t                              d| j        |||
           Y d}
~
t                              d| j        |||
           t          dt%          |
                    cY d}
~
c S d}
~
ww xY wt          dd          S )u  Stream a partial message via Telegram's native sendMessageDraft.

        The Bot API animates the preview when the same ``draft_id`` is reused
        across consecutive calls in the same chat.  When the response
        finishes, the caller sends the final text via the normal ``send``
        path; the draft preview clears naturally on the client (Telegram has
        no Bot API to "promote" a draft to a real message — the final
        ``sendMessage`` is what the user receives in their history).
        Fnot_connectedr  r+  api_unavailabler  r   r  )r   r.  rd   r  Nr8  Tr  draft_rejectedz[[%s] sendMessageDraft MarkdownV2 rejected, retrying as plain text (chat=%s draft_id=%s): %sz6[%s] sendMessageDraft failed (chat=%s draft_id=%s): %sr#   )r   r.   r,  r   r  r  r7   r;  rC  r  r   r  r+  rL   re  r-  r.  r   r   )r   r   r.  r  r  rd   r  r"  r  okr   s              r%   
send_draftzTelegramAdapter.send_draft	  sb       y 	De?CCCCty"677 	Fe3DEEEE g,,$*AAAww!!'4+B9!UUVWX 	 ,,X66	 * #	? #	?Lw<<MM5AK++D111t& &F
  ='0'<|$$.7*+?7497AA&AAAAAAAA E &dtDDDDDD!%7GHHHHHH ? ? ?   D$>$>q$A$A LLB	7Ha  
 HHHHLIw!   "%s1vv>>>>>>>>>>>#?& %/?@@@@s+   -*D-D--
G7:F>6A F>6G>Gc                   K   | j         st          d          |                    d          }	  | j         j        di | d{V S # t          $ r}||                     |          rx|                     |          rct                              d| j	        |           t          |          }|                    dd            | j         j        di | d{V cY d}~S  d}~ww xY w)a  Send a Telegram message, retrying once without message_thread_id
        if Telegram returns 'Message thread not found'.

        Used for control-style sends (approval prompts, model picker,
        update prompts) that can carry a stale thread_id from a DM
        reply chain.  The streaming send loop has its own equivalent
        (PR #3390) at the body of ``send``; this helper applies the
        same retry pattern to the non-streaming control paths.
        r  r8  NzP[%s] Thread %s not found for control message, retrying without message_thread_idr#   )r   r  r   r:  rL   re  r^  r-  rv  r   rw  rx  )r   r  r8  ry  rz  s        r%   "_send_message_with_thread_fallbackz2TelegramAdapter._send_message_with_thread_fallback>
  s<      y 	0///"JJ':;;	//99&999999999 	 	 	!-..x88 .33H== . fI%  
  $F||  !4d;;;3TY3CClCCCCCCCCCCCCCC	s$   A 
C&B	C!C& C!!C&r   rI   session_keyc                   K   | j         st          dd          S 	 |rd| dnd}|                     d| |           }t          t	          dd	
          t	          dd
          gg          }|                     |          }	|                     d|| j                  }
 | j        dt          |          |t          j        ||
d|                     ||	||
| j                  |                                  d{V }t          dt          |j                            S # t           $ rI}t"                              d| j        |           t          dt          |                    cY d}~S d}~ww xY w)zSend an inline-keyboard update prompt (Yes / No buttons).

        Used by the gateway ``/update`` watcher when ``hermes update --gateway``
        needs user input (stash restore, config migration).
        Fr  r  z (default: )r   u    ⚕ *Update needs your input:*

u   ✓ Yeszupdate_prompt:ycallback_datau   ✗ Nozupdate_prompt:nNr   r   rd   r  reply_markuprQ  r  Tr  z"[%s] send_update_prompt failed: %sr#   )r   r.   r  r   r   r;  rP  r   r6  rC  r   r  rU  r  r   r  rL   r-  rv  r   )r   r   rI   r   r7  r  default_hintrd   keyboardr  r  r  r   s                r%   send_update_promptz"TelegramAdapter.send_update_prompt^
  s      y 	De?CCCC	;7>F33333BL&&'bF'bT`'b'bccD+(BSTTT(ARSSS-  H 00::I<<T8[_[n<ooK?? G$0%$/  ..(3"&"5 /   ++--       C ds3>7J7JKKKK 	; 	; 	;NN?ANNNe3q66:::::::::	;s   D	D& &
E90>E4.E94E9dangerous commandcommanddescriptionc           	      t  K   | j         st          dd          S 	 t          |          dk    r|dd         dz   n|}dt          j        |           dt          j        |           }|                     |          }d	dl}	t          | d
          s|	                    d          | _	        t          | j	                  }
t          t          dd|
           t          dd|
           gt          dd|
           t          dd|
           gg          }t          |          |t          j        |d|                                 }|                     d|| j                  }||d<   |                    |                     ||||| j                              | j        di | d{V }|| j        |
<   t          dt/          |j                            S # t2          $ rI}t4                              d| j        |           t          dt/          |                    cY d}~S d}~ww xY w)u   Send an inline-keyboard approval prompt with interactive buttons.

        The buttons call ``resolve_gateway_approval()`` to unblock the waiting
        agent thread — same mechanism as the text ``/approve`` flow.
        Fr  r    N...u.   ⚠️ <b>Command Approval Required</b>

<pre>z</pre>

Reason: r   _approval_counterry   u   ✅ Allow Oncezea:once:r:  u   ✅ Sessionzea:session:u
   ✅ Alwaysz
ea:always:u   ❌ Denyzea:deny:r   rd   r  r>  r<  rQ  r  Tr  z"[%s] send_exec_approval failed: %sr#   )r   r.   r   _htmlescaper;  	itertoolsr,  countrH  r   r   r   rC  r   HTMLr  rP  r   updaterU  r6  r  r   r  rL   r-  rv  r   )r   r   rC  r7  rD  r  cmd_previewrd   r  rL  approval_idr@  r  r  r  r   s                   r%   send_exec_approvalz"TelegramAdapter.send_exec_approval
  s      y 	De?CCCC9	;47LL44G4G'%4%.500WK7[117 7 <447 7  00::I
 4!455 <)2););&t566K+()9IaT_IaIabbb(FaT_FaFabbb
 )E_R]E_E_```(C[kC[C[\\\	- 	 	H w<<'n (	& &
 ++--&F <<T8[_[n<ooK,7F()MM,,(3"&"5 -     @?II&IIIIIIIIC 1<D -ds3>7J7JKKKK 	; 	; 	;NN?ANNNe3q66:::::::::	;s   GG$ $
H7.>H2,H72H7titler  
confirm_idc           	        K   | j         st          dd          S 	 |                     t          |          dk    r|n|dd         dz             }t	          t          dd| 	          t          d
d| 	          gt          dd| 	          gg          }|                     |          }	t          |          |t          j	        |d| 
                                }
|                     d|| j                  }||
d<   |
                    |                     ||	||| j                              | j        di |
 d{V }|| j        |<   t          dt#          |j                            S # t&          $ rI}t(                              d| j        |           t          dt#          |                    cY d}~S d}~ww xY w)z8Render a three-button slash-command confirmation prompt.Fr  r  rF  NrG  u   ✅ Approve Oncezsc:once:r:  u   🔒 Always Approvez
sc:always:u
   ❌ Cancelz
sc:cancel:rI  r<  rQ  r  Tr  z"[%s] send_slash_confirm failed: %sr#   )r   r.   r  r   r   r   r;  rC  r   r  r  rP  r   rO  rU  r6  r  r   r  rL   r-  rv  r   )r   r   rS  r  r7  rT  r  previewr@  r  r  r  r  r   s                 r%   send_slash_confirmz"TelegramAdapter.send_slash_confirm
  sA     
 y 	De?CCCC&	;))S\\T5I5I''wW\X\W\~`eOeffG+();KbV`KbKbccc()>Ng[eNgNghhh
 )E^R\E^E^___-  H 00::Iw<<'3 (	& &
 ++--&F <<T8[_[n<ooK,7F()MM,,(3"&"5 -     @?II&IIIIIIIIC4?D%j1ds3>7J7JKKKK 	; 	; 	;NN?ANNNe3q66:::::::::	;s   EE6 6
G	 >G>G	G	questionchoices
clarify_idc           
      J  K   | j         st          dd          S 	 dt          j        |           }|                     |          }|r4d                    d t          |          D                       }	|d|	 z  }t          |          |t          j	        d| 
                                }
|rg }t          t          |                    D ]=}|                    t          t          |d	z             d
| d|           g           >|                    t          dd
| d          g           t!          |          |
d<   |                     d|          }||
d<   |
                    |                     ||||                      | j        di |
 d{V }|| j        |<   t          dt          |j                            S # t.          $ rI}t0                              d| j        |           t          dt          |                    cY d}~S d}~ww xY w)u$  Render a clarify prompt with one inline button per choice.

        Multi-choice mode (``choices`` non-empty): renders one button per
        option plus a final "✏️ Other (type answer)" button.  Picking the
        "Other" button flips the entry into text-capture mode so the next
        message becomes the response.

        Open-ended mode (``choices`` empty): renders the question as plain
        text — no buttons.  The next message in the session is captured by
        the gateway's text-intercept and resolves the clarify.
        Fr  r     ❓ r   c              3   n   K   | ]0\  }}|d z    dt          j        t          |                     V  1dS )ry   z. N)rJ  rK  r   )r~   r   cs      r%   r   z/TelegramAdapter.send_clarify.<locals>.<genexpr>!  s\       ) )1 1u66SVV 4 466) ) ) ) ) )r$   r   r   rd   r  ry   cl:r  r:  u   ✏️ Other (type answer)z:otherr>  NrQ  r  Tr  z[%s] send_clarify failed: %sr#   )r   r.   rJ  rK  r;  r   r   rC  r   rN  r  r  r   r   r   r   r   rP  rO  rU  r6  r  r  rL   r-  rv  r   )r   r   rX  rY  rZ  r7  r  rd   r  option_linesr  rowsidxr  r  r   s                   r%   send_clarifyzTelegramAdapter.send_clarify  s     ( y 	De?CCCC9	;2%,x0022D00::I 	.
  $yy ) ) )' 2 2) ) )     -|--- w<<'n& & ++--	&F  D  W..  CKK,aLL*B
*B*BS*B*B  !     (4&>J&>&>&>      *>d)C)C~&<<T8LLK,7F()MM,,(3	 -     @?II&IIIIIIIIC.9D
+ds3>7J7JKKKK 	; 	; 	;NN949aHHHe3q66:::::::::	;s   F2G 
H">HH"H"	providerscurrent_modelcurrent_providerc                 P  K   | j         st          dd          S 	 ddlm} n# t          $ r d }Y nw xY w	 |                     |          }	 ||          }
|                     d|pd d	|
 d
          }|r|                    d          nd}|                     d|| j	                  } | j
        dt          |          |t          j        |	|d|                     ||||| j	                  |                                  d{V }|j        |||||d| j        t%          |          <   t          dt%          |j                            S # t&          $ rI}t(                              d| j        |           t          dt%          |                    cY d}~S d}~ww xY w)u   Send an interactive inline-keyboard model picker.

        Two-step drill-down: provider selection → model selection.
        Edits the same message in-place as the user navigates.
        Fr  r  r   	get_labelc                     | S r   r#   slugs    r%   rj  z4TelegramAdapter.send_model_picker.<locals>.get_labelh      r$   +   ⚙ *Model Configuration*

Current model: `unknown`
Provider: 

Select a provider:r  Nr<  r=  r  )msg_idre  r7  on_model_selectedrf  rg  Tr  z![%s] send_model_picker failed: %sr#   )r   r.   hermes_cli.providersrj  rN   _build_provider_keyboardr  r   rP  r   r6  rC  r   r  rU  r  r  r  r   rL   r-  rv  r   )r   r   re  rf  rg  r7  rt  r  rj  r@  provider_labelrd   r  r  r  r   s                   r%   send_model_pickerz!TelegramAdapter.send_model_pickerS  s_      y 	De?CCCC	6666666 	 	 	    	-	;44Y??H&Y'788N&&*'4'A	* *!/* * * D 6>G[1114I<<T8[_[n<ooK?? G$0%$/  ..(3"&"5 /   ++--       C$ .&*%6!.$46 6D$S\\2 ds3>7J7JKKKK 	; 	; 	;NN>	1MMMe3q66:::::::::	;s*   # 33DE 
F%>F F% F%rZ  c           	      0  
 	 ddl m} n# t          $ r d}Y nw xY wd |D             d }g 
| |d |D                       D ]}|d         dk    rfd	|d
         D             }t          d |D                       }|d          d| d}t	          d |D                       rd| }
                    t          |d|d                                                   |d                   }|
                     ||                     n#|D ] }
                     ||                     !
fdt          dt          
          d          D             }	|	                    t          dd          g           t          |	          S )a  Build the top-level provider keyboard, folding provider groups.

        Provider families (Kimi/Moonshot, MiniMax, xAI Grok, ...) collapse to
        a single ``mpg:<gid>`` button; tapping it drills into a member
        sub-keyboard. Single providers (and groups with only one authenticated
        member) render as direct ``mp:<slug>`` buttons. Grouping mirrors the
        CLI ``hermes model`` picker via the shared ``group_providers`` fold,
        so all surfaces stay consistent.
        r   )group_providersNc                 :    i | ]}|                     d           |S rl  r   r~   ps     r%   
<dictcomp>z<TelegramAdapter._build_provider_keyboard.<locals>.<dictcomp>  s$    777155==!777r$   c           	          |                      dt          |                      dg                               }| d          d| d}|                      d          rd| }t          |d| d	          
          S )Ntotal_modelsmodelsr    (r9  
is_current   ✓ mp:rm  r:  )r   r   r   )r~  rM  labels      r%   _provider_buttonzBTelegramAdapter._build_provider_keyboard.<locals>._provider_button  s    EE.#aeeHb.A.A*B*BCCEy,,E,,,Euu\"" '&u'=N1V9=N=NOOOOr$   c                 8    g | ]}|                     d           S rl  r|  r}  s     r%   r   z<TelegramAdapter._build_provider_keyboard.<locals>.<listcomp>  s"    'I'I'I!f'I'I'Ir$   kindr  c                 (    g | ]}|v |         S r#   r#   r~   r	  by_slugs     r%   r   z<TelegramAdapter._build_provider_keyboard.<locals>.<listcomp>  s"    RRRaQ'\\wqz\\\r$   membersc           
   3      K   | ];}|                     d t          |                     dg                               V  <dS )r  r  N)r   r   r~   r	  s     r%   r   z;TelegramAdapter._build_provider_keyboard.<locals>.<genexpr>  sW          LMnc!%%"2E2E.F.FGG           r$   r  u    ▸ (r9  c              3   @   K   | ]}|                     d           V  dS )r  Nr|  r  s     r%   r   z;TelegramAdapter._build_provider_keyboard.<locals>.<genexpr>  s.      @@1155..@@@@@@r$   r  mpg:group_idr:  rm  c                 *    g | ]}||d z            S r'   r#   r~   r   buttonss     r%   r   z<TelegramAdapter._build_provider_keyboard.<locals>.<listcomp>  &    FFFqAE	"FFFr$   r'   
   ✗ Cancelmx)hermes_cli.modelsrz  rL   r  rn  r   r   r   r  r   r   )r   re  rz  r  r   r  rM  r  r~  rb  r  r  s             @@r%   rv  z(TelegramAdapter._build_provider_keyboard  sA   	#9999999 	# 	# 	#"OOO	# 87Y777	P 	P 	P &&'I'Iy'I'I'IJJ < <v;'))RRRR3y>RRRG    QX       E  #7|;;5;;;E@@@@@@@ / .uNN,UBZZBZBZ[[[     CK00A}'7'7':':;;;<"  4 4//223333FFFFE!S\\1,E,EFFF),dKKKLMMM#D)))s    r  pagec                 V   | j         }t          |          }t          d||z   dz
  |z            }t          dt          ||dz
                      }||z  }t          ||z   |          }|||         }g t	          |          D ]r\  }	}
||	z   }d|
v r|
                    d          d         n|
}t          |          dk    r|dd         dz   }                    t          |d	| 
                     sfdt          dt                    d          D             }|dk    rg }|dk    r*|                    t          dd|dz
   
                     |                    t          |dz    d| d
                     ||dz
  k     r*|                    t          dd|dz    
                     |                    |           |                    t          dd
          t          dd
          g           |dk    rd|dz    d| d| dnd}t          |          |fS )zBBuild paginated model buttons. Returns (keyboard, page_info_text).ry   r   /rz   &   N#   rG  mm:r:  c                 *    g | ]}||d z            S r  r#   r  s     r%   r   z9TelegramAdapter._build_model_keyboard.<locals>.<listcomp>  r  r$   r'   u   ◀ Prevmg:zmx:noopu   Next ▶   ◀ Backmbr  r  r  u   –z of r9  r   )
_MODEL_PAGE_SIZEr   r   r   r   r   r   r   r  r   )r   r  r  	page_sizetotaltotal_pagesr   endpage_modelsr   model_idabs_idxshortrb  nav	page_infor  s                   @r%   _build_model_keyboardz%TelegramAdapter._build_model_keyboard  s   )	F!ei/!3	ABB1c$a0011y %)#U++U3Y'$[11 	 	KAxaiG/2hHNN3''++HE5zzBcrc
U*NN$U///JJJ    GFFFE!S\\1,E,EFFF ??Caxx

/
JZPTWXPXJZJZ[[[\\\JJ+tax,G,G+,G,GW`aaabbbkAo%%

/
JZPTWXPXJZJZ[[[\\\KK 4@@@ TBBB
 	 	 	
 =H!OO888s888888QS	#D))944r$   datac           
        #$%K   | j                             |          }|s|                    d           d{V  dS 	 ddlm} n# t
          $ r d }Y nw xY w|                    d          r\|dd         %t          %fd	|d
         D             d          }|s|                    d           d{V  dS |                    dg           }%|d<   |                    d%          |d<   ||d<   d|d<   |                     |d          \  }}	|                    d%          }
|                    dt          |                    }t          |          }||k    r	d||z
   dnd}|
                    |                     d|
 d|	 d|           t          j        |           d{V  |                                 d{V  dS |                    d          r|	 t          |dd                   }n-# t          $ r  |                    d           d{V  Y dS w xY w|                    dg           }||d<   |                     ||          \  }}	|                    dd          }
|                    dd          %t          %fd|d
         D             d          }|r#|                    dt          |                    nt          |          }t          |          }||k    r	d||z
   dnd}|
                    |                     d|
 d|	 d|           t          j        |           d{V  |                                 d{V  dS |                    d          r	 t          |dd                   }n-# t          $ r  |                    d           d{V  Y dS w xY w|                    dg           }|dk     s|t          |          k    r|                    d           d{V  dS ||         }|                    dd          %|                    d           }|s|                    d!           d{V  dS d"}	  |||%           d{V }n9# t           $ r,}t"                              d#|           d$| }d%}Y d}~nd}~ww xY w	 |
                    |                     |          t          j        d           d{V  n@# t           $ r3 	 |
                    |dd           d{V  n# t           $ r Y nw xY wY nw xY w|                    |rd&nd'           d{V  | j                             |d           dS |                    d(          r	 t          |dd                   }n-# t          $ r  |                    d           d{V  Y dS w xY w|                    dg           }|dk     s|t          |          k    r|                    d           d{V  dS ||         }|                    dd          %|                    d           }|s|                    d!           d{V  dS 	 dd)lm} t-          j        ||%*           d{V }n# t           $ r d}Y nw xY w|t1          t3          d+d| ,          gt3          d-d.,          t3          d/d0,          gg          }|
                    |                     d1|j                   t          j        |           d{V  |                    d2           d{V  dS d"}	  |||%           d{V }n9# t           $ r,}t"                              d#|           d$| }d%}Y d}~nd}~ww xY w	 |
                    |                     |          t          j        d           d{V  n@# t           $ r3 	 |
                    |dd           d{V  n# t           $ r Y nw xY wY nw xY w|                    |rd&nd'           d{V  | j                             |d           dS |                    d3          r|d4d         }	 dd5lm} |                    |ddg f          \  }}}n# t           $ r dg }}Y nw xY wd6 |d
         D             $$fd7|D             }|s|                    d8           d{V  dS g #|D ]}|                    dt          |                    dg                               }|d          d9| d:} |                    d;          rd<|  } #                    t3          | d|d=          ,                     #fd>t=          dt          #          d?          D             }!|!                    t3          d-d.,          t3          d/d0,          g           t1          |!          }|
                    |                     d@|p| dA          t          j        |           d{V  |                                 d{V  dS |d.k    r|                     |d
                   }	  ||dB                   }"n# t           $ r |dB         }"Y nw xY w|
                    |                     dC|dD         pdE dF|" dG          t          j        |           d{V  |                                 d{V  dS |d0k    rT| j                             |d           |
                    dHdI           d{V  |                                 d{V  dS |                                 d{V  dS )JzHHandle model picker inline keyboard callbacks (mp:/mm:/mc:/mb:/mx:/mg:).u$   Picker expired — use /model again.rh   Nr   ri  c                     | S r   r#   rl  s    r%   rj  z@TelegramAdapter._handle_model_picker_callback.<locals>.get_label  rn  r$   r  r   c              3   4   K   | ]}|d          k    |V  dS rm  Nr#   r~   r~  provider_slugs     r%   r   z@TelegramAdapter._handle_model_picker_callback.<locals>.<genexpr>
  1      MMq!F)}2L2L2L2L2L2LMMr$   re  zProvider not found.r  selected_providerr   selected_provider_name
model_list
model_pager  z
_u2    more available — type `/model <name>` directly_r   u&   ⚙ *Model Configuration*

Provider: *r)  z
Select a model:rd   r  r>  r  zInvalid page.c              3   4   K   | ]}|d          k    |V  dS r  r#   r  s     r%   r   z@TelegramAdapter._handle_model_picker_callback.<locals>.<genexpr>;  r  r$   mc:zInvalid selection.zInvalid model index.rt  zPicker expired.FzModel picker switch failed: %szError switching model: TzSwitch failed.zModel switched!r  )expensive_model_warning)providerzSwitch anywayr:  r  r  r  r  u   ⚠ *Expensive Model Warning*

zConfirm expensive modelr     )PROVIDER_GROUPSc                      i | ]}|d          |S rl  r#   r}  s     r%   r  zATelegramAdapter._handle_model_picker_callback.<locals>.<dictcomp>  s    @@@qy!@@@r$   c                 (    g | ]}|v |         S r#   r#   r  s     r%   r   zATelegramAdapter._handle_model_picker_callback.<locals>.<listcomp>  s"    HHHa1<<wqz<<<r$   zGroup not found.r  r9  r  r  rm  c                 *    g | ]}||d z            S r  r#   r  s     r%   r   zATelegramAdapter._handle_model_picker_callback.<locals>.<listcomp>  s&    JJJ1GAAI&JJJr$   r'   u-   ⚙ *Model Configuration*

Provider family: *z*

Select a provider:rg  ro  rf  rp  rq  rr  zModel selection cancelled.rd   r>  ) r  r   answerru  rj  rN   r   r   r  r   r  r  r   r  rC  r   rL   r-  r[  rx  hermes_cli.model_cost_guardr  r   	to_threadr   r   r  r  r  r   r  rv  )&r   queryr  r   staterj  r  r  r@  r  pnamer  shownr   r  rc  r  r  callbackswitch_failedresult_textexcr  rv  r  r  _label_descmember_slugsr  r~  rM  r  rb  rw  r  r  r  s&                                      @@@r%   _handle_model_picker_callbackz-TelegramAdapter._handle_model_picker_callback  s      (,,W55 	,,$J,KKKKKKKKKF	6666666 	 	 	    	 ??5!! [	! HMMMMME+.MMM H  ll(=l>>>>>>>>>\\(B//F)6E%&.6ll6=.Q.QE*+"(E,"#E,"&"<"<VQ"G"GHiLL77ELLV==EKKE_dgl_l_l[%%-[[[[rtE))((2&+2 2.72 2*/2 2  %0% * 
 
 
 
 
 
 
 
 
 ,,..         __U## v	!48}}   lll888888888 YY|R00F"&E,"&"<"<VT"J"JHiII6;;E!II&92>>MMMMME+.MMM H BJZHLLV===sSY{{EKKE_dgl_l_l[%%-[[[[rtE))((2&+2 2.72 2*/2 2  %0% * 
 
 
 
 
 
 
 
 
 ,,..         __U## R	!$qrr(mm   ll(<l========= <44JQww#Z00ll(>l?????????!#H!II&92>>Myy!455H ll(9l:::::::::!M%$,HWh$N$NNNNNNN % % %=sCCC=== $%
--,,[99(4!% .          
    11(#'%) 2          
 !   D ,,)6M%%<M           $(($77777__U## a	!$qrr(mm   ll(<l========= <44JQww#Z00ll(>l?????????!#H!II&92>>Myy!455H ll(9l:::::::::OOOOOO !( 1+*! ! !      
    "/)/sUUUV,ZtLLL,\NNN1   --,,MGOMM   )4!) .          ll(AlBBBBBBBBB!M%$,HWh$N$NNNNNNN % % %=sCCC=== $%--,,[99(4!% .          
  	 	 	11(#'%) 2          
 !   D	 ,,)6M%%<M          
 $(($77777__V$$ N	!ABBxH.======.=.A.A(RQSUWL.Y.Y+|| . . .')2. A@U;-?@@@GHHHH<HHHG ll(:l;;;;;;;;;G  nc!%%"2E2E.F.FGGV90000055&& +*5NNE(>OAfI>O>OPPP    KJJJaWq0I0IJJJDKK$ZtDDD$\FFF    ,D11H))((.-3-?x. . .  %0% * 
 
 
 
 
 
 
 
 
 ,,..         T\\44U;5GHHH;!*51C+D!E!E ; ; ;!&'9!:; ))((.+0+A+NY. .%3. . .  %0% *          ,,..         T\\$(($777))1! *          ,,..          ,,..         s  A AA$G< <&H&%H&6N &N87N8Q2 2
R(<"R##R(,;S( (
T%3TT%
TT%TT%$T%<V &V>=V>"#Z ZZ] 
^ "^^;_ 
`	_65`	6
` `	``	`	*#b bb"i4 4j	j	rO  r
   r  ContextTypes.DEFAULT_TYPEc           
      x  K   |j         }|r|j        sdS |j        }t          |dd          }t          |dd          }t          |dd          }t          |dd          }t          |dd          }	t          |j        dd          }
|                    d          rC|j        rt          |j        j                  nd}|r|                     |||           d{V  dS |                    d	          r#| 	                    |||||	|

           d{V  dS |                    d          rd|
                    dd          }t          |          dk    r8|d         }	 t          |d                   }n4# t          t          f$ r  |                    d           d{V  Y dS w xY wt          t          |j        dd                    }|                     |||t          |          nd|	t          |	          nd|
          s|                    d           d{V  dS | j                            |d          }|s|                    d           d{V  dS ddddd}t          |j        dd          }|                    |d          }|                    |           d{V  	 |                    |                     | d|           t,          j        d           d{V  n# t0          $ r Y nw xY w	 d d!lm}  |||          }t6                              d"||||           n4# t0          $ r'}t6                              d#|           d }Y d}~nd}~ww xY w|r$|"|                     t          |                     dS |                    d$          r;|
                    dd          }t          |          dk    r|d         }|d         }t          t          |j        dd                    }|                     |||t          |          nd|	t          |	          nd|
          s|                    d%           d{V  dS | j                            |d          }|s|                    d&           d{V  dS dd'd(d)}t          |j        dd          }|                    |d          }|                    |           d{V  	 |                    |                     | d|           t,          j        d           d{V  n# t0          $ r Y nw xY w	 d d*l m!} |"                    |||           d{V }|rE|j        r=t          |j        dd          }t          |j        dd          }t          |dd          }t          |j        d+d          }t          |j        j                  |                     |          t,          j        d,| #                                }t          |d-|          }t          |          $                                d.t          tJ          j&                  $                                t          t          tJ          j&        d-tJ          j&                            $                                hv } || r}|{t          |          }!|!|d/<   |'                    | (                    t          |j        j                  t          |          t          |          d0d1|!| j)        2                     nf|d|'                    | (                    t          |j        j                  t          |          d3t          |          i| j)        4                      | j*        dZi | d{V  n:# t0          $ r-}t6                              d5| j+        |d06           Y d}~nd}~ww xY wdS |                    d7          r|
                    dd          }t          |          dk    r|d         }"|d         }#t          t          |j        dd                    }|                     |||t          |          nd|	t          |	          nd|
          s|                    d%           d{V  dS | j,                            |"          }|s|                    d&           d{V  dS t          |j        dd          }|#d8k    r	 d d9l-m.}$  |$|"           n8# t0          $ r+}t6          /                    d:| j+        |           Y d}~nd}~ww xY w|                    d;           d{V  	 |                    d<|j        j0        pd d=tc          j2        |           d>t,          j3        d           d{V  n# t0          $ r Y nw xY wdS 	 t          |#          }%n4# t          th          f$ r  |                    d?           d{V  Y dS w xY wd}&	 d d@l-m5}' |'                    |"          }(|(r6|(j6        r/d |%cxk    rt          |(j6                  k     rn n|(j6        |%         }&n# t0          $ r d}&Y nw xY w|&dA|%dz    }&| j,                            |"d           	 d dBl-m7})  |)|"|&          }*n:# t0          $ r-}t6                              dC| j+        |           dD}*Y d}~nd}~ww xY w|                    dE|&ddF                     d{V  	 |                    d<tc          j2        |j        j0        pd           dGtc          j2        |           dHtc          j2        |&           t,          j3        d           d{V  n# t0          $ r Y nw xY w|*rt6                              dI|"|&|           nt6          /                    dJ|"           dS |                    dK          sdS |
                    dd          d         }+t          t          |j        dd                    }|                     |||t          |          nd|	t          |	          nd|
          s|                    dL           d{V  dS |                    dM|+ dN           d{V  |+dOk    rdPndQ}	 |                    |                     dR| dS          t,          j        d           d{V  n# t0          $ r Y nw xY w	 d dTl8m9},  |,            }-|-dUz  }.|.:                    dV          }/|/;                    |+           |/<                    |.           t6                              dW|+t          |j        ddX                     dS # t0          $ r&}t6                              dY|           Y d}~dS d}~ww xY w)[z%Handle inline keyboard button clicks.Nr  r   chattyper8  
first_name)r  r  r  r  r  r  r  zgt:)query_chat_idquery_chat_typequery_thread_idquery_user_namezea:r  r'   r   ry   zInvalid approval data.rh   r  r   r  u/   ⛔ You are not authorized to approve commands.z(This approval has already been resolved.u   ✅ Approved onceu   ✅ Approved for sessionu   ✅ Approved permanentlyu
   ❌ Denied)oncesessionalwaysdenyUserResolved by r  r   )resolve_gateway_approvalzKTelegram button resolved %d approval(s) for session %s (choice=%s, user=%s)z;Failed to resolve gateway approval from Telegram button: %szsc:u1   ⛔ You are not authorized to answer this prompt.z&This prompt has already been resolved.u   🔒 Always approveu   ❌ Cancelled)r  r  r  )slash_confirmr  r_  r   r  rQ  T)r  rI  r  r  r<  z&[%s] slash-confirm callback failed: %sr   r`  other)mark_awaiting_textz"[%s] mark_awaiting_text failed: %su$   ✏️ Type your answer in the chat.r\  z"

<i>Awaiting typed response from u   …</i>zInvalid choice.)_entrieszchoice )resolve_gateway_clarifyz'[%s] resolve_gateway_clarify failed: %sFr  r  z

<b>z:</b> z<Telegram clarify button resolved (id=%s, choice=%r, user=%s)zGTelegram clarify button: resolve_gateway_clarify returned False (id=%s)zupdate_prompt:u4   ⛔ You are not authorized to answer update prompts.zSent 'z' to the update process.yYesNou   ⚕ Update prompt answered: *r)  r  z.update_responser  z/Telegram update prompt answered '%s' by user %srp  z1Failed to write update response from callback: %sr#   )=callback_queryr  r   	from_userr   r  r   r   r  _handle_gmail_triage_callbackr   r   rC  r   
IndexErrorr  r6  r  rx  r   r  r  r   r  rL   tools.approvalr  r-  r  r[  resume_typing_for_chatr  toolsr  resolver  r,  r   PRIVATErO  rU  r   r6  r   r  tools.clarify_gatewayr  rv  rd   rJ  rK  rN  r   r  rY  r  r   r  with_suffix
write_textreplace)0r   rO  r  r  r  query_messager  
query_chatr  r  r  r   partschoicerQ  	caller_idr7  	label_mapuser_displayr  r  rM  r  rT  _slash_confirm_modr  r  r  r  prompt_message_idrr  chat_type_valueis_private_chatr  rZ  choice_tokenr  rc  resolved_text_clarify_entriesr	  r  resolvedr  r  homeresponse_pathtmps0                                                   r%   r  z&TelegramAdapter._handle_callback_query#  s*      % 	EJ 	Fzy$77y$??]FD99
!*fd;;!-1DdKK!%/<FF ??KLL 	49MKc%-/000tG O88gNNNNNNNNNF ??5!! 		44+ / / / 5          F ??5!! D	JJsA&&E5zzQq"%eAh--KK"J/   ,,,D,EEEEEEEEEFF
  r B BCC	88)6E6Qc/222W[6E6Qc/222W[- 9     ,,,],^^^^^^^^^F"266{DII" ,,,V,WWWWWWWWWF 098(	 	  'ufMM!fj99lll.........11!00E1M1M|1M1MNN#,#8%) 2          
 !   D	GGGGGG44[&IIEKKe{FL    !   LL!^`cdddEEEEEE  D]6//M0B0BCCCF ??5!! a	JJsA&&E5zzQq"1X
r B BCC	88)6E6Qc/222W[6E6Qc/222W[- 9     ,,,_,`````````F"7;;JMM" ,,,T,UUUUUUUUUF 03- 	
  'ufMM!fj99lll.........11!00E1M1M|1M1MNN#,#8%) 2          
 !   D3jIIIIII(:(B(B#Z) ) # # # # # #K # ,Uu} ,U %,EM;NPT$U$U	&u}fdCC$+D&$$?$?	,3EM<QU,V,V)'*5=+@'A'A$($7$7$D$D*3*?7 7 #7799	7 +2)Wi*P*P*-o*>*>*D*D*F*F% 0117799(8'8CS T TUU[[]]K +
 %0_0IZIf*-.?*@*@KALK(=>'.. $ < <$'(=$>$>$'	NN58^^LP%& %& 9D262E != 	!" 	!"    '2'.. $ < <$'(=$>$>$'	NN%0#i..$A262E	 != !" !"   FdETTTTTTTTTTT  j j jLL!I49VYdhLiiiiiiiijF ??5!! f	JJsA&&E5zzQ"1X
$Qxr B BCC	88)6E6Qc/222W[6E6Qc/222W[- 9     ,,,_,`````````F"155jAA" ,,,T,UUUUUUUUUF&ufMM7**]LLLLLL**:6666$ ] ] ]'KTYX[\\\\\\\\]  ,,,R,SSSSSSSSS#55 "J(:(@b  "J  "Jfkfrs  gA  gA  "J  "J  "J'0~)- 6          
 %   Fl++CC"I.   ,,,=,>>>>>>>>>FF 04)RRRRRR,00<<E ; ;13P3P3P3Pc%->P>P3P3P3P3P3P(-c(:  ) ) )$(MMM) !( %8cAg$7$7M #''
D999%MMMMMM66z=QQHH  % % %LL!JDIWZ[[[$HHHHHH% ll(C}SbS/A(C(ClDDDDDDDDD11 SEL1C1Ir$J$J  S  SSXS_`lSmSm  S  Suz  vB  CP  vQ  vQ  S  S#,>%) 2          
 !   D  	KKV"M<   
 NNa"   F /00 	FC##A&r::;;	00!.=.Ic/***t.=.Ic/***t% 1 
 
 	 ,,$Z,[[[[[[[[[Fll I I I IlJJJJJJJJJ3D	))(()Q)Q)Q)QRR$0! *          
  	 	 	D	
	S888888"?$$D #55M++F33CNN6"""KK&&&KKIy I IK K K K K 	S 	S 	SLLLcRRRRRRRRR	Ss   E6 6-F'&F'2A K3 3
L ?L 0L5 5
M&?M!!M&(A T) )
T65T6:I+^& &
_0#__d 
e"!ee-Af; ;
ggg -hhAi) )i87i8!j4 4
k+>#k&&k+A3n
 

nn2?s2 2
s?>s?Bv	 	
v9v44v9zsend-draft.shu   ✓ sent draftz
archive.shu   ✓ archivedzdraft-blank.shu   ✓ drafted replyzspam.shu   ✓ marked spamzmute-add.shemailu	   ✓ muteddomainu   ✓ muted domainztrusted-ops-add.shu   ✓ trustedu   ✓ trusted domainz
vip-add.shu   ✓ marked VIPu   ✓ marked VIP domain)
r  archivedraftspammutezmute-domaintrustztrust-domainvipz
vip-domainc          	        K   |                     dd          }t          |          dk    r|                    d           d{V  dS |d         |d         }	}t          t	          |j        dd	                    }
|                     |
||t          |          nd|t          |          nd|
          s|                    d           d{V  dS | j                            |          }|s!|                    d|            d{V  dS |\  }}}}t          j
                    dz  dz  dz  |z  }|                                sC|                    d| d           d{V  t                              d| j        |           dS t          |          |	g|}d}	 t          j        |t          j        j        t          j        j        d d{V }t          j        |                                d           d{V \  }}|j        dk    r'|}d}t                              d| j        ||	           n|                    dd                                          }|r|                                d         n	d|j         }d| d|dd           }t                              d!| j        ||	|j        |           n|# t          j        $ r+ d| d"}t                              d#| j        ||	           Y nCt6          $ r7}d| d$| }t                              d%| j        ||	|d&           Y d}~nd}~ww xY w|                    |           d{V  |sdS t	          |j        d'd(          }|j        r|j        j        pd	nd	}| d)| d*| }	 |r|                    |           d{V  dS |                    |d+           d{V  dS # t6          $ r Y dS w xY w),z=Dispatch a gmail-triage inline-button callback (gt:verb:arg).r  r'   r   zInvalid gmail-triage data.rh   Nry   r  r   r  u0   ⛔ You are not authorized to act on this email.zUnknown verb: z.hermesscriptszgmail-triageu   ❌ z missingz$[%s] gmail-triage script missing: %sF)stdoutstderrr  timeoutr   Tz-[%s] gmail-triage callback ok: verb=%s arg=%sr  r  )errorsrz   zexit z	 failed: P   zA[%s] gmail-triage callback failed: verb=%s arg=%s rc=%s stderr=%sz
 timed outz4[%s] gmail-triage callback timed out: verb=%s arg=%sz error: z;[%s] gmail-triage callback exception: verb=%s arg=%s err=%sr   r  r  u   
— r  r  )r   r   r  r   r   r  r6  _GT_VERB_DISPATCHr   _Pathr  r!  r-  r[  r   r   create_subprocess_exec
subprocessPIPEr  communicate
returncoder  decoders   
splitlinesTimeoutErrorrL   r  rd   r  )r   r  r  r  r  r  r  r   verbargr  r	  script_name
extra_argssuccess_labelis_state_verbscript_pathcmdr  proc_stdout_bytesstderr_bytesr  stderr_text	last_liner  r  original_textappendeds                                r%   r  z-TelegramAdapter._handle_gmail_triage_callback  sK      

3""u::??,,$@,AAAAAAAAAF!HeAhcr::;;	00!.=.Ic/***t.=.Ic/***t% 1 
 
 	 ,,$V,WWWWWWWWWF&**400 	,,$;T$;$;,<<<<<<<<<F@E=ZjllY.:^KkY!!## 	,,$@;$@$@$@,AAAAAAAAALL?KXXXF;2z2 	 7).).        D
 180@  ""B1 1 1 + + + + + +'M< !##%CItS   
 +11')1LLRRTT<GfK2244R88MfUYUdMfMf	>t>>in>>WItS$/;   # 	g 	g 	g+4+++ELLOQUQZ\`befffff 	 	 	.4....ELLM	4cD         	 lll&&&&&&&&& 	FufEE6;mK+1r#DD5DDlDD		 P --8-<<<<<<<<<<< --8$-OOOOOOOOOOO 	 	 	DD	s7   'D!K	 	7M	M-L==MO =O 
O*)O*r  r  c                 H    | d| }|                     d          r|dz  }|S )zBuild an actionable file-not-found error for gateway MEDIA delivery.

        Paths like /workspace/... or /output/... often only exist inside the
        Docker sandbox, while the gateway process runs on the host.
        z file not found: )z/workspace/z/output/z	/outputs/z (path may only exist inside the Docker sandbox. Bind-mount a host directory and emit the host-visible path in MEDIA: for gateway file delivery.))r   )r   r  r  r[  s       r%   _missing_media_path_errorz)TelegramAdapter._missing_media_path_error  sB     11411??CDD 	=E
 r$   	file_size	max_bytesc                     t          d|dz            }	 t          |pd          dz  }|dd}n# t          t          f$ r d}Y nw xY wd| d| d	| d
S )Nry      r   z.1fz MBzunknown sizez
[Telegram z skipped: file size z exceeds the zN MB limit. Ask the user to send a shorter voice note or a smaller audio file.])r   rC  r   r   )r   r  r;  r<  limit_mbsize_mb	size_texts          r%   _telegram_media_too_large_notez.TelegramAdapter._telegram_media_too_large_note   s    q)455	').q))[9G"++++II:& 	' 	' 	'&III	'( ( (I ( (( ( (	
s   0 AAr3  c                    t          t          | dd          pd          }t          |dd          }	 t          |pd          }n# t          t          f$ r d}Y nw xY w|dk    rdS ||k    rdS d|                     |||          fS )z<Validate Telegram media size before downloading into memory.r  r   r;  Nr   )TNF)rC  r   r   r   rB  )r   r3  r  r<  r;  sizes         r%   _telegram_media_size_allowedz,TelegramAdapter._telegram_media_size_allowed  s    &68HII]M]^^	FK66		y~A&&DD:& 	 	 	DDD	199:9:d99%yQQQQs   A AA
audio_pathcaptionc           
        K   | j         st          dd          S 	 t          j                            |          s%t          d|                     d|                    S t          |d          5 t          j                            |          d                                         }|dv r| 	                    |          }| 
                    ||| j                  }	|                     ||||	| j        	          }
|                     | j         j        t          |          |r
|d
d         nd
|	d|
|                     |          ||	dfd           d
{V }n|dv r| 	                    |          }| 
                    ||| j                  }	|                     ||||	| j        	          }|                     | j         j        t          |          |r
|d
d         nd
|	d||                     |          ||	dfd           d
{V }n,|                     |||||           d
{V cd
d
d
           S d
d
d
           n# 1 swxY w Y   t          dt'          |j                            S # t*          $ rY}t,                              d| j        |d           t3                                          |||||           d
{V cY d
}~S d
}~ww xY w)z<Send audio as a native Telegram voice message or audio file.Fr  r  Audiorbry   >   .opus.oggr<  r  Nr   )r   voicerG  rQ  rM  c                  .                          d          S Nr   seek
audio_files   r%   <lambda>z,TelegramAdapter.send_voice.<locals>.<lambda>F      JOOA,>,> r$   rt  >   .m4a.mp3)r   audiorG  rQ  rY  c                  .                          d          S rO  rP  rR  s   r%   rT  z,TelegramAdapter.send_voice.<locals>.<lambda>`  rU  r$   )r   	file_pathrG  rD  r  Tr  zJ[%s] Failed to send Telegram voice/audio, falling back to base adapter: %sr   r  )r   r.   r   r  r!  r:  r#  splitextr,  r;  rP  r   rU  r{  
send_voicerC  r  
send_audiosend_documentr   r  rL   r-  r[  r   r   )r   r   rF  rG  rD  r  r  ext_voice_threadr  voice_thread_kwargsr  _audio_threadaudio_thread_kwargsr   rS  r
  s                  @r%   r]  zTelegramAdapter.send_voice  sD      y 	De?CCCCL	g7>>*-- l!%t7U7UV]_i7j7jkkkkj$'' ?:g&&z2215;;==+++$($<$<X$F$FM"&"D"DXxgkgz"D"{"{K*.*F*F% ,7&*&9 +G + +' !% K K	,'*7||%/9@'Jwuu~~d3>	 
 2 #77AA !#$>$>$>$> !L ! !      CC ,,,$($<$<X$F$FM"&"D"DXxgkgz"D"{"{K*.*F*F% ,7&*&9 +G + +' !% K K	,'*7||%/9@'Jwuu~~d3>	 
 2 #77AA !#$>$>$>$> !L ! !      CC$ "&!3!3 '", '!)!) "4 " "      s? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@ ds3>7J7JKKKK 	g 	g 	gLL\		     ++GZ(]e+ffffffffffffff	gsP   AJ "J 2G	I;J J IJ I%J 
K%AK K% K%        imageshuman_delayc           
        K   | j         sdS |sdS 	 ddlm} nc# t          $ rV}t                              d| j        |           t                                          ||||           d{V  Y d}~dS d}~ww xY wg }g |D ]^\  }}	|	                    d          s-| 
                    |          r|                    ||	f           G                    ||	f           _|r+t                                          ||||           d{V  sdS ddlm}
 |                     |          }dfd	t          dt!                              D             }t#          |          D ]\  }}|dk    r |dk    rt%          j        |           d{V  g }g 	 |D ]\  }}	|	r
|	dd
         nd}|	                    d          r |
|dd                   }t(          j                            |          s"t                              d| j        |           |t/          |d          }                    |           |                     |||                     |                     |||                     |s-	 D ]'}	 |                                 # t          $ r Y $w xY wEt                              d| j        t!          |          |dz   t!          |                     |                     d|| j                  }|                     ||||| j                  }dfd}|                     | j         j        t?          |          ||d||                      |          ||d|           d{V  nw# t          $ rj}t                              d| j        |dz   t!          |          |d           t                                          ||||           d{V  Y d}~nd}~ww xY wD ]'}	 |                                 # t          $ r Y $w xY w# D ]'}	 |                                 # t          $ r Y $w xY ww xY wdS )a)  Send a batch of images natively via Telegram's media group API.

        Telegram's ``send_media_group`` bundles up to 10 photos/videos into
        a single album. Larger batches are chunked. Animated GIFs cannot
        go into a media group (they require ``send_animation``), so they
        are peeled off and sent individually via the base default path.

        URL-based photos go into the group directly; local files are
        opened as byte streams. On failure the whole batch falls back to
        the base adapter's per-image loop.
        Nr   )InputMediaPhotozD[%s] InputMediaPhoto unavailable, falling back to per-image send: %szfile://)rg  )unquoter  c                 *    g | ]}||z            S r#   r#   )r~   r   CHUNKphotoss     r%   r   z8TelegramAdapter.send_multiple_images.<locals>.<listcomp>  s&    LLL!&1u9%LLLr$   r      z.[%s] Skipping missing image in media group: %srJ  )mediarG  z5[%s] Sending media group of %d photo(s) (chunk %d/%d)ry   r<  r  rF   c                  ^    D ](} 	 |                      d           # t          $ r Y %w xY wd S rO  )rQ  rL   )fhopened_filess    r%   _reset_opened_fileszATelegramAdapter.send_multiple_images.<locals>._reset_opened_files  sR    * ! !!GGAJJJJ( ! ! ! D!! !s   
**)r   ro  rQ  zmedia grouprV  zI[%s] send_media_group failed (chunk %d/%d), falling back to per-image: %sTr   rF   N)!r   rM   ri  rL   r-  rv  r   r   send_multiple_imagesr   _is_animation_urlr   r  rj  r;  r  r   r   r   r  r   r  r!  r#  closer  rP  r   rU  r{  send_media_grouprC  r  )r   r   rf  r  rg  ri  r  
animations	image_urlalt_text_unquote_threadr  	chunk_idxr  ro  rG  
local_pathrq  r  r  rs  r   rl  rr  rm  r
  s                          @@@r%   ru  z$TelegramAdapter.send_multiple_imagesv  sD     $ y 	F 	F	0000000 	 	 	NNV	3   ''..w+VVVVVVVVVFFFFF	 #%
 #) 	5 	5Ix''	22 5t7M7Mi7X7X 5!!9h"78888y(34444  	''..X; /           	F444444**844 LLLLLuQFU/K/KLLL )& 1 1 K	 K	IuQ9q==mK000000000!E&(LE+0 X X'Ix19ChuuootG ++I66 X%-Xim%<%<
!w~~j99 %"NN P $	:   %!*d33$++B///__2w%O%O%OPPPP__9g%V%V%VWWWW ^ '  B



$   [ KIs5zz9q=#f++   #@@x_c_r@ss $ < <(3"&"5 != ! !! ! ! ! ! ! AAI.#&w<<!&/:  (	
 33H== ! 3 B            	 	 	_Iy1}c&kk1!     gg22UH+ 3              	 '  B



$   ,  B



$   OK	 K	s    
A<AA77A<C'N
J
J,+J,1CNP*
O;A O61P*6O;;P*P
P%$P%*Q0QQ
QQQQ
image_pathc           
        K   | j         st          dd          S 	 t          j                            |          s%t          d|                     d|                    S |                     |          }|                     ||| j                  }| 	                    ||||| j                  }	t          |d          5 |                     | j         j        t          |          |r
|dd	         nd|d
|	|                     |          ||dfd           d{V }
ddd           n# 1 swxY w Y   t          dt          |
j                            S # t"          $ r}t          |          }d|v pd|v }|r"t$                              d| j        |           n#t$                              d| j        |d           	 |                     |||t          j                            |          ||           d{V cY d}~S # t"          $ r^}t$                              d| j        |d           t3                                          |||||           d{V cY d}~cY d}~S d}~ww xY wd}~ww xY w)z5Send a local image file natively as a Telegram photo.Fr  r  Imager<  r  rJ  Nr   r   photorG  rQ  r  c                  .                          d          S rO  rP  )
image_files   r%   rT  z1TelegramAdapter.send_image_file.<locals>.<lambda>%  s    
(:(: r$   rV  Tr  Photo_invalid_dimensionsPHOTO_INVALID_DIMENSIONSzK[%s] Image dimensions exceed Telegram photo limits, sending as document: %szO[%s] Failed to send Telegram local image as photo, trying document fallback: %sr   )r   r[  rG  	file_namerD  r  zV[%s] Failed to send Telegram local image as document, falling back to base adapter: %sr  )r   r.   r   r  r!  r:  r;  rP  r   rU  r#  r{  
send_photorC  r  r   r  rL   r-  r  r   rv  r_  basenamer[  r   send_image_file)r   r   r  rG  rD  r  r  r}  r  r  r  r   	error_stris_dim_errordoc_errr  r
  s                  @r%   r  zTelegramAdapter.send_image_file  s      y 	De?CCCCN	p7>>*-- l!%t7U7UV]_i7j7jkkkk..x88G<<Xx_c_r<ssK 88$/"1 9  M j$'' : GGI(#&w<<!+5<#F75D5>>$/:	 
 ( 33H==  : : : : H                        ds3>7J7JKKKK 0	p 0	p 0	pAI +i7 ;-:   .I	    3I!    p!//#(# g..z::%% 0                p p p7I!     #WW44Wj'S[fn4ooooooooooooooooooopQ0	ps|   AE! "A!E! A$D3'E! 3D77E! :D7;%E! !I>,AI9=HI>
I6AI1&I6'I9+I>1I66I99I>r[  r  c           
        K   | j         st          dd          S 	 t          j                            |          s%t          d|                     d|                    S |pt          j                            |          }|                     |          }	|                     ||| j	                  }
| 
                    ||	||
| j	                  }t          |d          5 |                     | j         j        t          |          ||r
|dd	         nd|
d
||                     |          ||
dfd           d{V }ddd           n# 1 swxY w Y   t          dt!          |j                            S # t$          $ rZ}t&                              d| j        |d           t-                                          ||||||           d{V cY d}~S d}~ww xY w)z<Send a document/file natively as a Telegram file attachment.Fr  r  Filer<  r  rJ  Nr   )r   documentfilenamerG  rQ  r  c                  .                          d          S rO  rP  r1  s   r%   rT  z/TelegramAdapter.send_document.<locals>.<lambda>      q		 r$   rV  Tr  z [%s] Failed to send document: %sr   r  )r   r.   r   r  r!  r:  r  r;  rP  r   rU  r#  r{  r_  rC  r  r   r  rL   r-  rv  r   r   )r   r   r[  rG  r  rD  r  r  display_namer}  r  r  r  r   r1  r
  s                 @r%   r_  zTelegramAdapter.send_documentZ  s      y 	De?CCCC#	t7>>),, j!%t7U7UV\^g7h7hiiii$C(8(8(C(CL..x88G<<Xx_c_r<ssK 88$/"1 9  M i&& ! GGI+#&w<<$%$05<#F75D5>>$/:  ( 33H==  1 1 1 1 H                      " ds3>7J7JKKKK 	t 	t 	tNN=ty!VZN[[[..w	7IW_jr.ssssssssssssss	tsK   AF "BF $A%E	F EF E%F 
G'AG"G'"G'
video_pathc           
        K   | j         st          dd          S 	 t          j                            |          s%t          d|                     d|                    S |                     |          }|                     ||| j                  }| 	                    ||||| j                  }	t          |d          5 |                     | j         j        t          |          |r
|dd	         nd|d
|	|                     |          ||dfd           d{V }
ddd           n# 1 swxY w Y   t          dt          |
j                            S # t"          $ rY}t$                              d| j        |d           t+                                          |||||           d{V cY d}~S d}~ww xY w)z2Send a video natively as a Telegram video message.Fr  r  Videor<  r  rJ  Nr   )r   videorG  rQ  r  c                  .                          d          S rO  rP  r  s   r%   rT  z,TelegramAdapter.send_video.<locals>.<lambda>  r  r$   rV  Tr  z[%s] Failed to send video: %sr   r  )r   r.   r   r  r!  r:  r;  rP  r   rU  r#  r{  
send_videorC  r  r   r  rL   r-  rv  r   r   )r   r   r  rG  rD  r  r  r}  r  r  r  r   r1  r
  s               @r%   r  zTelegramAdapter.send_video  sw      y 	De?CCCC 	g7>>*-- l!%t7U7UV]_i7j7jkkkk..x88G<<Xx_c_r<ssK 88$/"1 9  M j$'' 1 GGI(#&w<<!"5<#F75D5>>$/:	 
 ( 33H==  1 1 1 1 H                        ds3>7J7JKKKK 	g 	g 	gNN:DIqSWNXXX++GZ(]e+ffffffffffffff	gsK   AE! "A!E! A$D3'E! 3D77E! :D7;%E! !
G+AF?9G?Grz  c           	        K   | j         st          dd          S ddlm}  ||          sLt                              d| j                   t                                          |||||           d{V S 	 | 	                    |          }| 
                    ||| j        	          }|                     ||||| j        
          }	|                     | j         j        t          |          ||r
|dd         nd|d|	|                     |          ||d           d{V }
t          dt#          |
j                            S # t&          $ r}t                              d| j        |d           	 ddl}|                    d          4 d{V }|                    |           d{V }|                                 |j        }ddd          d{V  n# 1 d{V swxY w Y   |                     ||||| j        
          }|                     | j         j        t          |          ||r
|dd         nd|d||                     |          ||d           d{V }
t          dt#          |
j                            cY d}~S # t&          $ r^}t                              d| j        |d           t                                          |||||           d{V cY d}~cY d}~S d}~ww xY wd}~ww xY w)zSend an image natively as a Telegram photo.
        
        Tries URL-based send first (fast, works for <5MB images).
        Falls back to downloading and uploading as file (supports up to 10MB).
        Fr  r  r   )is_safe_urlz/[%s] Blocked unsafe image URL (SSRF protection)r  Nr<  r  r   r  z	URL photoTr  z8[%s] URL-based send_photo failed, trying file upload: %sr   g      >@r  zuploaded photoz+[%s] File upload send_photo also failed: %s)r   r.   tools.url_safetyr  r-  rv  r   r   
send_imager;  rP  r   rU  r{  r  rC  r  r   r  rL   r  AsyncClientr   raise_for_statusr  r[  )r   r   rz  rG  rD  r  r  _photo_threadr  photo_thread_kwargsr  r   r  clientresp
image_dataupload_thread_kwargse2r
  s                     r%   r  zTelegramAdapter.send_image  s      y 	De?CCCC000000{9%% 	fNNLdiXXX++GY\d+eeeeeeeeeG	j 44X>>M<<Xx_c_r<ssK"&">">$/"1 #? # # CC	$"7||&18Bwuu~~d+6	 
 * //99        C ds3>7J7JKKKK -	j -	j -	jNNJ		    %j ,,T,:: . . . . . . .f!'I!6!6666666D))+++!%J. . . . . . . . . . . . . . . . . . . . . . . . . . .
 (,'C'C!(3"&"5 (D ( ($ !GGI(#&w<<!+5<#F75D5>>$/:	 
 / 33H== $        "$3s~;N;NOOOOOOOOO j j jAI!	     #WW//GX`h/iiiiiiiiiiiiiiiiiiijK-	jsu   :CE L
#L; J7G$J$
G.	.J1G.	2B"JL

L$AK=2L3L7L
=LLL
animation_urlc                   K   | j         st          dd          S 	 |                     |          }|                     ||| j                  }|                     ||||| j                  }|                     | j         j        t          |          ||r
|dd         nd|d|| 	                    |          ||d	           d{V }	t          d
t          |	j                            S # t          $ rM}
t                              d| j        |
d
           |                     |||||           d{V cY d}
~
S d}
~
ww xY w)zJSend an animated GIF natively as a Telegram animation (auto-plays inline).Fr  r  r<  r  Nr   )r   	animationrG  rQ  r  Tr  zA[%s] Failed to send Telegram animation, falling back to photo: %sr   r  )r   r.   r;  rP  r   rU  r{  send_animationrC  r  r   r  rL   r-  r[  r   r  )r   r   r  rG  rD  r  _anim_threadr  animation_thread_kwargsr  r   s              r%   r  zTelegramAdapter.send_animation  s      y 	De?CCCC!	g33H==L<<Xx_c_r<ssK&*&B&B$/"1 'C ' '# CC	("7||!.18Bwuu~~d+6	 
 . //99        C ds3>7J7JKKKK 	g 	g 	gLLS		     -(]effffffffffffff	gs   CC. .
E8AE :E Ec                 .  K   | j         r
d}d}	 |                     |          }t          |o|                    d                    }|                     |          }| j                             t          |          d|           d{V  dS # t          $ rx}|rH|F	 | j                             t          |          d           d{V  Y d}~dS # t          $ r Y nw xY wt          	                    d| j
        |d	           Y d}~dS d}~ww xY wdS )
zSend typing indicator.FNrI  typing)r   actionr8  )r   r  z1[%s] Failed to send Telegram typing indicator: %sTr   )r   r;  rt   r   rZ  send_chat_actionrC  rL   r-  r.  r   )r   r   r  _is_dm_topicr8  _typing_threadr   s          r%   r  zTelegramAdapter.send_typingI  s     9 	!&L/3!%!9!9(!C!C#H$a>`1a1abb$($F$F~$V$V!i00LL#&7 1           
       $5$A"i88$'LL#+ 9          $    GI!	          	 	s6   A>B 
DD/C
C!D C!!&DDc                 @  K   | j         sdddS 	 | j                             t          |                     d{V }d}|j        t          j        k    rd}n8|j        t          j        k    rd}|j        rd}n|j        t          j        k    rd}|j	        p|j
        pt          |          ||j        t          |dd	          d
S # t          $ rN}t                              d| j        ||d           t          |          dt          |          dcY d}~S d}~ww xY w)z&Get information about a Telegram chat.Unknownr  )r   r  Nr  r  channelis_forumF)r   r  usernamer  z0[%s] Failed to get Telegram chat info for %s: %sTr   )r   r  r[  )r   get_chatrC  r  r   GROUP
SUPERGROUPr  CHANNELrS  	full_namer   r  r   rL   r-  r[  r   )r   r   r  r  r   s        r%   get_chat_infozTelegramAdapter.get_chat_infol  sc     y 	5%t444	I++CLL99999999DIyHN**#		h111#	= ( 'Ih...%	 
DdnDG! M#D*e<<	    	I 	I 	ILLB	      LL$QHHHHHHHH	Is   B4C 
DADDDc                 @   |s|S i dgdt           dt           ffd|}t          |          }fd}t          j        d||          }t          j        dfd|          }fd	}t          j        d
||          }fd}t          j        d||t          j                  }t          j        dfd|          }t          j        dfd|          }t          j        dfd|          }t          j        dfd|          }fd}t          j        d||t          j                  }t          |          }t          t                                                              D ]}|	                    ||                   }t          j
        d|          }g }	t          |          D ]R\  }
}|
dz  dk    r|	                    |           $|fd}|	                    t          j        d||                     Sd                    |	          }|S )af  
        Convert standard markdown to Telegram MarkdownV2 format.

        Protected regions (code blocks, inline code) are extracted first so
        their contents are never modified.  Standard markdown constructs
        (headers, bold, italic, links) are translated to MarkdownV2 syntax,
        and all remaining special characters are escaped.
        r   r   rF   c                 J    dd          d}dxx         dz  cc<   | |<   |S )z@Stash *value* behind a placeholder token that survives escaping.z PHr    ry   r#   )r   r  counterplaceholderss     r%   _phz+TelegramAdapter.format_message.<locals>._ph  s;    +71:+++CAJJJ!OJJJ %LJr$   c                 ,   |                      d          }d|dd          v r|                    d          dz   nd}|d |         }||d          }|d d         }|                    dd                              dd	          } ||z   d
z             S )Nr   r   r   ry   \\\`z\`r   )r  r   r  )r	  r   open_endopeningbody_and_closebodyr  s         r%   _protect_fencedz7TelegramAdapter.format_message.<locals>._protect_fenced  s    ''!**C.2c!""goosyy**1H)8)nG ^N!#2#&D<<f--55c5AAD3w~-...r$   z(```(?:[^\n]*\n)?[\s\S]*?```)z	(`[^`]+`)c                 h     |                      d                              dd                    S )Nr   r  r  )r  r  r	  r  s    r%   rT  z0TelegramAdapter.format_message.<locals>.<lambda>  s+    cc!''!**,,T6::;; r$   c                     t          |                     d                    }|                     d                              dd                              dd          } d| d| d          S )	Nry   r'   r  r  r9  z\)[]()ri   r  r  )r	  displayurlr  s      r%   _convert_linkz5TelegramAdapter.format_message.<locals>._convert_link  sm    "1771::..G''!**$$T622::3FFC3,7,,c,,,---r$   z-\[([^\]]+)\]\(([^()]*(?:\([^()]*\)[^()]*)*)\)c                     |                      d                                          }t          j        dd|          } dt	          |           d          S )Nry   \*\*(.+?)\*\*rk   r)  )r  rs   rm   rg   ri   )r	  innerr  s     r%   _convert_headerz7TelegramAdapter.format_message.<locals>._convert_header  sV    GGAJJ$$&&EF+UE::E31<..111222r$   z^#{1,6}\s+(.+)$)flagsr  c                 b     dt          |                     d                     d          S )Nr)  ry   ri   r  r  s    r%   rT  z0TelegramAdapter.format_message.<locals>.<lambda>  /    cc9l1771::66999:: r$   z\*([^*\n]+)\*c                 b     dt          |                     d                     d          S )N_ry   r  r  s    r%   rT  z0TelegramAdapter.format_message.<locals>.<lambda>  r  r$   z	~~(.+?)~~c                 b     dt          |                     d                     d          S )N~ry   r  r  s    r%   rT  z0TelegramAdapter.format_message.<locals>.<lambda>  r  r$   z\|\|(.+?)\|\|c                 b     dt          |                     d                     d          S )N||ry   r  r  s    r%   rT  z0TelegramAdapter.format_message.<locals>.<lambda>  s/    cc;|AGGAJJ77;;;<< r$   c           	      2   |                      d          }|                      d          }|                    d          r;|                    d          r& | dt          |d d                    d          S  | dt          |                     S )Nry   r'   r   r   )r  r   r   ri   )r	  r  r  r  s      r%   _convert_blockquotez;TelegramAdapter.format_message.<locals>._convert_blockquote  s    WWQZZFggajjG   && G7+;+;D+A+A GsfEE|GCRCL'A'AEEEFFF3&::<#8#8::;;;r$   z^((?:\*\*)?>{1,3}) (.+)$z(```[\s\S]*?```|`[^`]+`)r'   ry   c                    |                                  }|                     d          }|dk    r||dz
           dk    r|S |dk    r|dk    r||dz
           dk    r|S |dk    r|d |         }d|v sd|v rnd}t          |dz
  t          |d	z
  d
          d
          D ]F}||         dk    r'|dz  }|dk     r|dk    r||dz
           dk    r|c S  n5||         dk    r|dz  }Gd|z   S )Nr   ry   r  (]r9  z](httpr  i  rz   )r   r  r  r   )r	  _segschbeforedepthr   s          r%   	_esc_barez1TelegramAdapter.format_message.<locals>._esc_bare  s1   		AB1uua!e!4!4!	SyyQUUtAE{c/A/A!	Syy!%bqb#v--$%E%*1q5#a$h2C2CR%H%H / /#'7c>>$)QJE',qyy+,q55T!a%[C5G5G35III(- (1 &*!W^^$)QJE"9$r$   z[(){}]r   )r   r   rm   rg   	MULTILINEri   reversedr  keysr  r   r   r   r   )r   r  rd   r  r  r  r  r  _code_split_safe_parts_idxr  r  r  r  r  s                @@@r%   r  zTelegramAdapter.format_message  s     	N#	s 	s 	 	 	 	 	 	 	  %T**	/ 	/ 	/ 	/ 	/ v,
 
 v;;;;
 
	. 	. 	. 	. 	.
 vFW[\\	3 	3 	3 	3 	3 vR\
 
 

 v::::
 
 v::::
 
 v::::
 
 v<<<<
 
	< 	< 	< 	< 	< v',	
 
 
 D!! D!2!2!4!45566 	8 	8C<<\#%677DD
 h:DAA#K00 	G 	GJD$ax1}}""4(((( '+ % % % %2 ""26)Y#E#EFFFFww{##r$   c                 
   | j         j                            d          }|:t          |t                    r|                                dv S t          |          S t          j        dd                                          dv S )zBReturn whether group chats should require an explicit bot trigger.require_mentionN>   r   r  r$  r%  TELEGRAM_REQUIRE_MENTIONr  	r   r   r   rd  r   r,  rt   r   r   r  s     r%   _telegram_require_mentionz)TelegramAdapter._telegram_require_mention<  s    [&**+<==
!*c** H!''))-GGG
###y3W==CCEEIcccr$   c                 L   | j         j                            d          }|| j         j                            d          }|:t          |t                    r|                                dv S t          |          S t          j        dd                                          dv S )aM  Return whether skipped unmentioned group messages are stored as context.

        When enabled with ``require_mention``, Telegram matches the Yuanbao /
        OpenClaw-style group UX: observe ordinary group chatter in the session
        transcript, but only dispatch the agent when the bot is explicitly
        addressed.
        "observe_unmentioned_group_messagesN!ingest_unmentioned_group_messages>   r   r  r$  r%  +TELEGRAM_OBSERVE_UNMENTIONED_GROUP_MESSAGESr  r  r  s     r%   ,_telegram_observe_unmentioned_group_messagesz<TelegramAdapter._telegram_observe_unmentioned_group_messagesE  s     [&**+OPP
*../RSSJ!*c** H!''))-GGG
###yFPPVVXX\vvvr$   c                 
   | j         j                            d          }|:t          |t                    r|                                dv S t          |          S t          j        dd                                          dv S )zFReturn whether non-allowlisted groups may trigger via direct @mention.
guest_modeN>   r   r  r$  r%  TELEGRAM_GUEST_MODEr  r  r  s     r%   _telegram_guest_modez$TelegramAdapter._telegram_guest_modeV  s~    [&**<88
!*c** H!''))-GGG
###y.88>>@@D^^^r$   c                 
   | j         j                            d          }|:t          |t                    r|                                dv S t          |          S t          j        dd                                          dv S )zJReturn whether explicit @...bot mentions exclusively route group messages.exclusive_bot_mentionsN>   r   r  r$  r%  TELEGRAM_EXCLUSIVE_BOT_MENTIONSr%  r  r  s     r%    _telegram_exclusive_bot_mentionsz0TelegramAdapter._telegram_exclusive_bot_mentions_  s    [&**+CDD
!*c** H!''))-GGG
###y:FCCIIKKOiiir$   c                    | j         j                            d          }|t          j        dd          }t          |t                    rd |D             S d t          |                              d          D             S )Nfree_response_chatsTELEGRAM_FREE_RESPONSE_CHATSr   c                     h | ]D}t          |                                          #t          |                                          ES r#   r   rs   r~   parts     r%   r   z@TelegramAdapter._telegram_free_response_chats.<locals>.<setcomp>m  =    KKK$T9J9JKCIIOO%%KKKr$   c                 ^    h | ]*}|                                 |                                 +S r#   r|   r  s     r%   r   z@TelegramAdapter._telegram_free_response_chats.<locals>.<setcomp>n  -    MMM

M

MMMr$   r(  	r   r   r   r   r   rd  r  r   r   r   r   s     r%   _telegram_free_response_chatsz-TelegramAdapter._telegram_free_response_chatsh  s    k##$9::;):B??Cc4   	LKK#KKKKMMS)<)<MMMMr$   c                    | j         j                            d          }|t          j        dd          }t          |t                    rd |D             S d t          |                              d          D             S )a_  Return the whitelist of group/supergroup chat IDs the bot will respond in.

        When non-empty, group messages from chats NOT in this set are
        silently ignored unless ``guest_mode`` is enabled and the bot is
        explicitly @mentioned.  DMs are never filtered.
        Empty set means no restriction (fully backward compatible).
        allowed_chatsNTELEGRAM_ALLOWED_CHATSr   c                     h | ]D}t          |                                          #t          |                                          ES r#   r  r  s     r%   r   z:TelegramAdapter._telegram_allowed_chats.<locals>.<setcomp>|  r  r$   c                 ^    h | ]*}|                                 |                                 +S r#   r|   r  s     r%   r   z:TelegramAdapter._telegram_allowed_chats.<locals>.<setcomp>}  r  r$   r(  r  r  s     r%   _telegram_allowed_chatsz'TelegramAdapter._telegram_allowed_chatsp  s     k##O44;)4b99Cc4   	LKK#KKKKMMS)<)<MMMMr$   c                    | j         j                            d          }|t          j        dd          }t          |t                    rd |D             S d t          |                              d          D             S )z0Return Telegram chats authorized at group scope.group_allowed_chatsNTELEGRAM_GROUP_ALLOWED_CHATSr   c                     h | ]D}t          |                                          #t          |                                          ES r#   r  r  s     r%   r   z@TelegramAdapter._telegram_group_allowed_chats.<locals>.<setcomp>  r  r$   c                 ^    h | ]*}|                                 |                                 +S r#   r|   r  s     r%   r   z@TelegramAdapter._telegram_group_allowed_chats.<locals>.<setcomp>  r  r$   r(  r  r  s     r%   _telegram_group_allowed_chatsz-TelegramAdapter._telegram_group_allowed_chats  s    k##$9::;):B??Cc4   	LKK#KKKKMMS)<)<MMMMr$   c                     |                                  }|st                      S |                                 }|r||z  S |S )a#  Chats where observed group context may use a shared source.

        ``group_allowed_chats`` is the gateway authorization allowlist for
        user-less group sources.  ``allowed_chats`` remains an optional response
        gate; when set, observed context must satisfy both lists.
        )r%  r   r  )r   group_allowedresponse_alloweds      r%   _telegram_observe_allowed_chatsz/TelegramAdapter._telegram_observe_allowed_chats  sR     ::<< 	55L7799 	4 #333r$   c                    | j         j                            d          }|t          j        dd          }t          |t                    rd |D             S d t          |                              d          D             S )aa  Return the whitelist of Telegram forum topic IDs this bot handles.

        When non-empty, group/supergroup messages from other topics are
        silently ignored. DMs are never filtered by topic. Telegram may omit
        ``message_thread_id`` for the forum General topic, so ``None`` is
        treated as topic ``1`` for matching purposes.
        allowed_topicsNTELEGRAM_ALLOWED_TOPICSr   c                     h | ]D}t          |                                          #t          |                                          ES r#   r  r  s     r%   r   z;TelegramAdapter._telegram_allowed_topics.<locals>.<setcomp>  r  r$   c                 ^    h | ]*}|                                 |                                 +S r#   r|   r  s     r%   r   z;TelegramAdapter._telegram_allowed_topics.<locals>.<setcomp>  r  r$   r(  r  r  s     r%   _telegram_allowed_topicsz(TelegramAdapter._telegram_allowed_topics  s     k##$455;)5r::Cc4   	LKK#KKKKMMS)<)<MMMMr$   c                 
   | j         j                            d          }|t          j        dd          }t          |t                    r|}n"t          |                              d          }t                      }|D ]}t          |          
                                }|s&	 |                    t          |                     J# t          t          f$ r$ t                              d| j        |           Y ~w xY w|S )Nignored_threadsTELEGRAM_IGNORED_THREADSr   r(  z,[%s] Ignoring invalid Telegram thread id: %r)r   r   r   r   r   rd  r  r   r   r   rs   r  rC  r   r   r-  rv  r   )r   r   r  ignoredr   rd   s         r%   _telegram_ignored_threadsz)TelegramAdapter._telegram_ignored_threads  s   k##$566;)6;;Cc4   	)FFXX^^C((FEE 	a 	aEu::##%%D aCII&&&&z* a a aMtyZ_`````as   ("C2D ?D c                    | j         j                            d          }|t          j        dd                                          }|rg	 t          j        |          }nO# t          $ rB d |	                                D             }|sd |
                    d          D             }Y nw xY w|}|g S t          |t                    r|g}t          |t                    s5t                              d| j        t#          |          j                   g S g }|D ]}t          |t                    r|                                s,	 |                    t)          j        |t(          j                             `# t(          j        $ r,}t                              d	| j        ||           Y d}~d}~ww xY w|r.t                              d
| j        t3          |                     |S )z=Compile optional regex wake-word patterns for group triggers.mention_patternsNTELEGRAM_MENTION_PATTERNSr   c                 ^    g | ]*}|                                 |                                 +S r#   r|   r  s     r%   r   z=TelegramAdapter._compile_mention_patterns.<locals>.<listcomp>  s-    XXXt4::<<XdjjllXXXr$   c                 ^    g | ]*}|                                 |                                 +S r#   r|   r  s     r%   r   z=TelegramAdapter._compile_mention_patterns.<locals>.<listcomp>  s-    !Z!Z!Z4TZZ\\!Z$**,,!Z!Z!Zr$   r(  z?[%s] telegram mention_patterns must be a list or string; got %sz,[%s] Invalid Telegram mention pattern %r: %sz*[%s] Loaded %d Telegram mention pattern(s))r   r   r   r   r   rs   jsonloadsrL   r(  r   rd  r   r  r-  rv  r   r  r   r   rm   compile
IGNORECASEr[  r  r   )r   patternsr   loadedcompiledpatternr  s          r%   r   z)TelegramAdapter._compile_mention_patterns  s   ;$(();<<)7<<BBDDC "[!Z__FF  [ [ [XXs~~7G7GXXXF! [!Z!Z399S>>!Z!Z!Z[ "Ih$$ 	" zH(D)) 	NNQ	X'  
 I%' 	h 	hGgs++ 7==?? h
7BM B BCCCC8 h h hMtyZacfggggggggh 	`KKDdiQTU]Q^Q^___s+   A! !A	B-,B-	2E<<F7"F22F7c                     t          |dd           }|sdS t          t          |dd                                        d          d                                         }|dv S )Nr  Fr  r   .rz   >   r  r  )r   r   r   r,  )r   r  r  r  s       r%   _is_group_chatzTelegramAdapter._is_group_chat  sd    w-- 	5fb112288==bAGGII	333r$   c                     | j         rt          |dd           sdS t          |j        dd           }t          |o(t          |dd           t          | j         dd           k              S )Nreply_to_messageFr  r  )r   r   rF  rt   )r   r  
reply_users      r%   _is_reply_to_botz TelegramAdapter._is_reply_to_bot  sm    y 	1CT J J 	5W5{DII
Jd7:tT#B#BgdiY]_cFdFd#deeer$   c           	      h    t                      } fd} |            D ]\  }}|D ]}t          t          |dd                                        d          d                                         }|dvrQt          t          |dd                    }t          t          |dd	                    }|d	k     s|d	k    r||||z                                            }	|d
k    r^|	                    d                                          }
t          j	        d|
t          j
                  r|                    |
           |	                    d          }|d	k     r:|	|dz   d                                                                         }t          j	        d|t          j
                  r|                    |            |            D ]\\  }}|r|r
t          j        d|          D ]<}|                    |                    d                                                     =]|S )a{  Extract explicit Telegram bot usernames mentioned in text/captions.

        Telegram bot usernames are 5-32 characters and must end in "bot".
        Entity mentions are authoritative. The raw-text fallback is intentionally narrow so
        entity-less mobile/client variants still work without treating email
        addresses or arbitrary substrings as bot mentions.
        c               3      K   t           dd           pdt           dd           pg fV  t           dd           pdt           dd           pg fV  d S Nrd   r   entitiesrG  caption_entitiesr   r  s   r%   _iter_sourceszETelegramAdapter._extract_bot_mention_usernames.<locals>._iter_sources  x      '64006BUY8Z8Z8`^`````'9d339r77L^`d;e;e;kikkkkkkkr$   r  r   rC  rz   >   mentionbot_commandoffsetlengthr   rR  @[a-z0-9_]{2,29}botry   Nz1(?i)(?<![A-Za-z0-9_`/])@([A-Za-z0-9_]{2,29}bot)\b)r   r   r   r   r,  rC  rs   r   rm   	fullmatchr=  r  findfinditerr  )r  mentioned_bot_usernamesrP  source_textrL  entityentity_typerT  rU  entity_texthandleat_indexcommand_targetraw_textr   s   `              r%   _extract_bot_mention_usernamesz.TelegramAdapter._extract_bot_mention_usernames  sl    -0EE	l 	l 	l 	l 	l &3]__ 	@ 	@!K" @ @!'&&""="=>>DDSII"MSSUU&@@@WVXr::;;WVXq99::A::1)&&*@AGGII)++(//44::<<F|$962=QQ </33F;;; '++C00a<<!,X\]]!;!A!A!C!C!I!I!K!K< 5~r}UU @+//???3@< #0-// 	D 	DHh x %Y[cdd D D'++EKKNN,@,@,B,BCCCCD '&r$   c           	         | j         sdS t          | j         dd           pd                    d                                          }t          | j         dd           }|rd| nd }fd} |            D ]\  }}|D ]}t	          t          |dd                                        d          d	                                         }	|	d
k    r|rt          t          |dd	                    }
t          t          |dd                    }|
dk     s|dk    r||
|
|z                                                                            |k    r  dS |	dk    r.t          |dd           }|rt          |dd           |k    r  dS |	dk    r|rt          t          |dd	                    }
t          t          |dd                    }|
dk     s|dk    r]||
|
|z            }|                    d          }|dk     r||d                                                                          |k    r  dS |r7t          j
        d|t          j                  r||                               v S dS )NFr  r   rV  r  c               3      K   t           dd           pdt           dd           pg fV  t           dd           pdt           dd           pg fV  d S rK  rN  rO  s   r%   rP  z<TelegramAdapter._message_mentions_bot.<locals>._iter_sources,  rQ  r$   r  rC  rz   rR  rT  rU  r   Ttext_mentionuserrS  rW  )r   r   r   r,  r   r   rC  rs   rY  rm   rX  r=  rd  )r   r  bot_usernamebot_idexpectedrP  r\  rL  r]  r^  rT  rU  rh  command_textra  s    `             r%   _message_mentions_botz%TelegramAdapter._message_mentions_bot$  s   y 	5	:t<<BJJ3OOUUWWD$//)5?%|%%%4	l 	l 	l 	l 	l &3]__ !	$ !	$!K"  $  $!'&&""="=>>DDSII"MSSUU)+++ 2!>!>??F 1!=!=>>FzzVq[[ "6&6/#9:@@BBHHJJhVV#ttt W N22"66488D $dD 9 9V C C#ttt M11h1 !2!>!>??F 1!=!=>>FzzVq[[ #.vfvo/E#FL+0055H!|| #HII.4466<<>>(JJ#tttA $B  	PBL)>bm\\ 	P4#F#Fw#O#OOOur$   c                     | j         sdS t          | j         dd          pd                    d                                          }|sdS |                     |          }t          |          o||vS )a  Return True when explicit bot handles target other bots, not this one.

        Telegram groups can contain several Hermes bot profiles. A message like
        ``@bot3 hi @bot4`` must not wake ``@bot1`` through reply/wake-word
        fallbacks. Treat explicit bot-handle mentions as an exclusive routing
        hint: if at least one @...bot username is present and none matches this
        adapter's own bot username, this adapter should ignore the message.

        MessageEntity values are preferred, but some Telegram clients expose
        selected bot handles as plain text in group messages. The raw-text
        fallback is intentionally limited to usernames ending in "bot", which
        Telegram requires for bot accounts.
        Fr  Nr   rV  )r   r   r   r,  rd  rt   )r   r  ri  r[  s       r%   #_explicit_bot_mentions_exclude_selfz3TelegramAdapter._explicit_bot_mentions_exclude_self\  s     y 	5	:t<<BJJ3OOUUWW 	5"&"E"Eg"N"N+,,\E\1\\r$   c                     | j         sdS t          |dd           t          |dd           fD ](}|s| j         D ]}|                    |          r  dS )dS )NFrd   rG  T)r   r   search)r   r  r
  rA  s       r%   !_message_matches_mention_patternsz1TelegramAdapter._message_matches_mention_patternst  s    % 	5!'6488''9VZ:[:[\ 	  	 I 1    >>),,  444   ur$   c                 T    |                                  o|                     |          S )zReturn True for the narrow guest-mode bypass: explicit bot mention.

        The caller (:meth:`_should_process_message`) has already verified
        the message is a group chat, so that check is not repeated here.
        )r  rm  )r   r  s     r%   _is_guest_mentionz!TelegramAdapter._is_guest_mention  s)     ((**Rt/I/I'/R/RRr$   rd   c                     |r| j         rt          | j         dd           s|S t          j        | j         j                  }t          j        d| dd|                                          }|p|S )Nr  z(?i)@z\b[,:\-]*\s*r   )r   r   rm   rK  r  rg   rs   )r   rd   r  rn   s       r%   _clean_bot_trigger_textz'TelegramAdapter._clean_bot_trigger_text  sv     	49 	GDIz4,P,P 	K9TY/00&8(888"dCCIIKK$r$   c           	      :   |                                  sdS |                     |          sdS t          |dd          }|                                 }|r|t	          |          n| j        }||vrdS |?	 t          |          |                                 v rdS n# t          t          f$ r Y dS w xY wt	          t          t          |dd          dd                    }| 
                                r|                     |          rdS |                                 }|r||vrdS ||                                 v rdS |                                 sdS |                     |          rdS |                     |          rdS |                     |          rdS dS )zEReturn True when a group message should be stored but not dispatched.Fr8  Nr  r  r   T)r  rD  r   r/  r   rW  rC  r4  r   r   r  ro  r)  r  r  rH  rm  rr  )r   r  r  r+  r?  chat_id_stralloweds          r%   )_should_observe_unmentioned_group_messagez9TelegramAdapter._should_observe_unmentioned_group_message  s   @@BB 	5""7++ 	5G%8$??	6688 	)2)>s9~~~DDaH~--u y>>T%C%C%E%EEE 5 Fz*   uu '''64"@"@$KKLL0022 	t7_7_`g7h7h 	56688  	+W445
 $<<>>>>5--// 	5  )) 	5%%g.. 	511':: 	5ts   6#B B10B1c                 2    t          j        |ddd          S )zFReturn a chat/topic-scoped source for observed Telegram group context.N)r  r  user_id_alt)dataclassesr  )r   r3  s     r%   %_telegram_group_observe_shared_sourcez5TelegramAdapter._telegram_group_observe_shared_source  s    "644UYZZZZr$   eventc                 ^    |j         j        pd}|j         j        p|}d| d| d|j        pd S )Nrp  r  rr   z]
r   )r3  r  r  rd   )r   r  r  senders       r%   '_telegram_group_observe_attributed_textz7TelegramAdapter._telegram_group_observe_attributed_text  sE    ,&3)'27:6::G::
(8b:::r$   c                     t          t          | dd           dd           pd}t          t          | dd           dd           pd}d| d| dS )Nr   r  rp  r  zIYou are handling a Telegram group chat message.
- Your identity: user_id=z , @-mention name in this group=@a)  
- observed Telegram group context may be provided in a separate context-only block before the current message; it is not necessarily addressed to you.
- Treat only the current new message as a request explicitly directed at you, and use observed context only when the current message asks for it.rN  )r   r  rj  s      r%   &_telegram_group_observe_channel_promptz6TelegramAdapter._telegram_group_observe_channel_prompt  su    7466
DIIVYvt44dDAANYR(.R RPXR R R	
r$   c           	      H   |                                  s|S t          |dd          }|r|                     |          s|S t          t          t          |dd          dd                    }|                                 }|r||vr|S |                     |j                  }|                                 }|j        r|j         d| n|}|j	        t          j        k    rt          j        |||          S t          j        ||                     |          ||          S )	z>Align triggered group turns with observed-history attribution.raw_messageNr  r  r   r   )r3  channel_prompt)rd   r3  r  )r  r   rD  r   r)  r~  r3  r  r  message_typer,   r  r}  r  r  )r   r  r  rx  ry  shared_sourceobserve_promptr  s           r%   )_apply_telegram_group_observe_attributionz9TelegramAdapter._apply_telegram_group_observe_attribution  sO   @@BB 	Le]D99 	$"5"5k"B"B 	L''+vt"D"DdBOOPP6688 	+W44LBB5<PPDDFFJOJ^rE0FFnFFFdr!444&$-   
 "==eDD )	
 
 
 	
r$   r  c                     |j         rt          j        S |j        rt          j        S |j        rt          j        S |j        rt          j        S |j	        rt          j
        S t          j        S )z5Classify a Telegram media message into a MessageType.)stickerr,   STICKERr  r  r  r  rY  r  rM  r  DOCUMENT)r   r  s     r%   _media_message_typez#TelegramAdapter._media_message_type  sj    ; 	'&&9 	%$$9 	%$$9 	%$$9 	%$$##r$   c                   K   ddl m} |                     |          \  }}}}|dS t          | dd          }t          |dd          }		 t	          |	pd          }
n# t
          t          f$ r d}
Y nw xY wd|
cxk     r|k    sHn |dz  }|                     |j        d| d	          |_        t          
                    d
|	           dS 	 |                                 d{V }t          |                                 d{V           }|s0t          j                            t          |dd          pd          } |||||          }n5# t"          $ r(}t                              d|d           Y d}~dS d}~ww xY w|"|                     |j        d          |_        dS |j        g|_        |j        g|_        |j        dk    rt.          j        |_        n|j        dk    rt.          j        |_        |                     |j        |                                          |_        t          
                    d|j        |j                   dS )a2  Cache an unmentioned group attachment and annotate the observed text.

        Passive group traffic, so downloads are bounded by the same
        ``_max_doc_bytes`` limit as the addressed document path. Oversized or
        unsupported attachments are noted in the transcript without downloading.
        r   )cache_media_bytesNr  r   r;  r>  zB[Observed Telegram attachment too large or unverifiable. Maximum: z MB.]z6[Telegram] Observed group attachment skipped (size=%s)r[  r   )r  	mime_typedefault_kindz3[Telegram] Failed to cache observed group media: %sTr   z=[Observed Telegram attachment: unsupported type, not cached.]imager  z)[Telegram] Cached observed group %s at %s)gateway.platforms.baser  _observed_media_sourcer   rC  r   r   _append_observed_noterd   r-  r  get_filebytesdownload_as_bytearrayr   r  r  rL   rv  
media_urls
media_typemedia_typesr  r,   r  r  r  context_note)r   r  r  r  r3  r  mimer  r<  r;  rD  r?  file_objr  r  r  s                   r%   _cache_observed_mediaz%TelegramAdapter._cache_observed_media  s      	=<<<<<'+'B'B3'G'G$$>FD"24DEE	FK66		y~A&&DD:& 	 	 	DDD	D%%%%I%%%% [1H33
dU]ddd EJ KKPR[\\\F	#__........Hx==????????@@D V7++GHk2,N,N,TRTUU&&th$]abbbFF 	 	 	NNPRU`dNeeeFFFFF	 >33
[ EJ F"K=#./;'!!!,!2E[G##!,!2E//
F<O<O<Q<QRR
?fkZZZZZs+   
A A21A2BE 
F E;;F c                 6   |j         r|j         d         dddfS |j        r|j        dddfS |j        r|j        dddfS |j        r!|j        t	          |j        d	d          pdddfS |j        r-|j        }||j        pd|j        pd                                d
fS dS )zEReturn (telegram_file_source, filename, mime, default_kind) or Nones.rz   r   r  	video/mp4r  z	voice.ogg	audio/oggrY  r  N)Nr   r   N)	r  r  rM  rY  r   r  r  r  r,  )r   r  docs      r%   r  z&TelegramAdapter._observed_media_source2  s    9 	29R="b'119 	79b+w669 	@9k;??9 	U9gcibAAGRWTT< 	Q,C+cm.Ar-H-H-J-JDPP!!r$   existingnotec                 $    |s| pdS | s|S |  d| S )Nr   r   r#   )r  r  s     r%   r  z%TelegramAdapter._append_observed_noteA  s5     	">r! 	K&&&&&r$   msg_type	update_idc                    t          | dd          }|sdS 	 |p|                     |||          }|                     |j                  }|                    |          }d|                     |          t          j        t          j	                  
                                dd}|j        rt          |j                  |d<   |                    |j        |           t          | d	d
          }	t                              d|	t          t          |dd          dd          |j        j        pd           dS # t$          $ r8}
t          | d	d
          }	t                              d|	|
           Y d}
~
dS d}
~
ww xY w)zGAppend skipped group chatter to the target session without dispatching._session_storeNr  rh  )tzT)roler  	timestampobservedr  r   rM   zF[%s] Telegram group message observed (no bot trigger): chat=%s from=%sr  r  rp  z1[%s] Failed to observe Telegram group message: %s)r   _build_message_eventr~  r3  get_or_create_sessionr  r   nowr   utc	isoformatr  r   append_to_transcript
session_idr-  r  r  rL   rv  )r   r  r  r  r  storer  session_entryr	  adapter_namer  s              r%   "_observe_unmentioned_group_messagez2TelegramAdapter._observe_unmentioned_group_messageI  s    .55 	F	c^T66wT]6^^E FFu|TTM!77FFMGGNN%\X\:::DDFF 	 E  <&)%*:&;&;l#&&}'?GGG"4<<LKKX66iHH$1		      	c 	c 	c"4<<LNNNP\^abbbbbbbbb	cs   D#D< <
E>-E99E>
is_commandr  c          	         |                      |          sdS t          |dd          }|                                 }|r|t          |          n| j        }||vrdS |_	 t          |          |                                 v rdS n8# t          t          f$ r$ t          
                    d| j        |           Y nw xY w|                      |          s^|Z| j        j                            dd          r:t          t          t          |dd          dd	                    }|s|| j        v rdS dS t          t          t          |dd          dd	                    }|                                 r|                     |          rdS |                     |          }|                                 }	|	r||	vr|S |rdS ||                                 v rdS |                                 sdS |                     |          rdS |                                 s|                     |          rdS |                     |          S )
u  Apply Telegram group trigger rules.

        DMs remain unrestricted. Group/supergroup messages are accepted when:
        - the chat passes the ``allowed_chats`` whitelist (when set), or
          ``guest_mode`` is enabled and the bot is explicitly mentioned
        - the chat is explicitly allowlisted in ``free_response_chats``
        - ``require_mention`` is disabled
        - the message replies to the bot
        - the bot is @mentioned
        - the text/caption matches a configured regex wake-word pattern

        When ``allowed_chats`` is non-empty, it remains a hard gate except for
        the narrow ``guest_mode`` bypass: group/supergroup messages that
        explicitly @mention this bot. Replies and regex wake words do not bypass
        ``allowed_chats``. When ``require_mention`` is enabled, slash commands are not given
        special treatment — they must pass the same mention/reply checks
        as any other group message.  Users can still trigger commands via
        the Telegram bot menu (``/command@botname``) or by explicitly
        mentioning the bot (``@botname /command``), both of which are
        recognised as mentions by :meth:`_message_mentions_bot`.
        Tr8  NFz8[%s] Ignoring non-numeric Telegram message_thread_id: %rignore_root_dmr  r  r   )rD  r   r/  r   rW  rC  r4  r   r   r-  rv  r   r   r   r   r  r  ro  rt  r  r  r  rH  r  rm  rr  )
r   r  r  r  r+  r?  r   rx  guest_mentionry  s
             r%   _should_process_messagez'TelegramAdapter._should_process_messagel  s   , ""7++ 	4G%8$??	6688 	)2)>s9~~~DDaH~--u  qy>>T%C%C%E%EEE 5 Fz* q q qY[_[dfopppppq ""7++ 	 T[%6%:%:;KU%S%S gggvt&D&DdBOOPP! !g1H&H&H 54'''64"@"@$KKLL0022 	t7_7_`g7h7h 	5 ..w77
 ..00 	!{'11   	4$<<>>>>4--// 	4  )) 	4 ((** 	t/I/I'/R/R 	455g>>>s    #B 2B;:B;c                    
K   | j         4 d{V  	 t          |dd          }|rt          |dd          s	 ddd          d{V  dS t          |j                  }|| j        v r	 ddd          d{V  dS ddlm
m} ddlm	}  |t                    \  }}
fd	|D             }| j                            | ||
                     d{V  | j                            |           t                              d| j        t#          |          |           n8# t$          $ r+}	t                              d| j        |	           Y d}	~	nd}	~	ww xY wddd          d{V  dS # 1 d{V swxY w Y   dS )u   Lazy-register bot commands for forum supergroups.

        Forum topics don't inherit AllGroupChats scope — Telegram resolves
        via BotCommandScopeChat(chat_id).  Register on first message so the
        command menu works in topic views.
        Nr  r  Fr   )rj  BotCommandScopeChatrn  rp  c                 .    g | ]\  }} ||          S r#   r#   rs  s      r%   r   z:TelegramAdapter._ensure_forum_commands.<locals>.<listcomp>  ru  r$   r   rv  z2[%s] Lazy-registered %d commands for forum chat %sz/[%s] Forum command lazy-registration failed: %s)r   r   rC  r  r   rM   rj  r  r  ro  r  r   r  r  r-  r  r   r   rL   rv  )r   r  r  r   r  ro  r  r  r  r   rj  s             @r%   _ensure_forum_commandsz&TelegramAdapter._ensure_forum_commands  s      # 	` 	` 	` 	` 	` 	` 	` 	``w55 74U#C#C 		` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	`
 dg,,d<<<	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` EDDDDDDDFFFFFF#9#9G]#^#^#^ qWWWWWWWi//DWDW`gDhDhDh/iiiiiiiii.227;;;PRVR[]`am]n]npwxxxx ` ` `PRVR[]^________`	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	` 	`sG   E-$D"D"<B%D"!E-"
E,!EE-EE--
E7:E7c                 F    t          |dd          pt          |dd          S )a|  Return the message-like payload for normal messages and channel posts.

        Telegram exposes channel broadcasts as ``update.channel_post`` rather
        than ``update.message``.  MessageHandler filters can still dispatch
        those updates, so handlers must use ``effective_message`` to avoid
        consuming channel posts without ever building a gateway event.
        effective_messageNr  rN  )r   rO  s     r%   _effective_update_messagez)TelegramAdapter._effective_update_message  s)     v2D99]WVYX\=]=]]r$   c                   K   |                      |          }|r|j        sdS |                     |          s>|                     |          r'|                     |t
          j        |j                   dS |                     |j	                   d{V  | 
                    |t
          j        |j                  }|                     |j                  |_        |                     |          }|                     |           dS )zHandle incoming text messages.

        Telegram clients split long messages into multiple updates.  Buffer
        rapid successive text messages from the same user/chat and aggregate
        them into a single MessageEvent before dispatching.
        Nr  )r  rd   r  rz  r  r,   r  r  r  r  r  rv  r  _enqueue_text_eventr   rO  r  r  r  s        r%   r  z$TelegramAdapter._handle_text_message  s      ,,V44 	#( 	F++C00 	==cBB k77[=MY_Yi7jjjF))&.999999999))#{/?6K[)\\11%*==
>>uEE  '''''r$   c                   K   |                      |          }|r|j        sdS |                     |d          sdS |                     |           d{V  |                     |t
          j        |j                  }|                     |j                  |_        | 	                    |          }| 
                    |           d{V  dS )z!Handle incoming command messages.NTr  r  )r  rd   r  r  r  r,   r  r  rv  r  handle_messager  s        r%   r  zTelegramAdapter._handle_command  s      ,,V44 	#( 	F++CD+AA 	F))#.........))#{/BfN^)__11%*==
>>uEE!!%(((((((((((r$   c                   K   |                      |          }|sdS |                     |          s>|                     |          r'|                     |t          j        |j                   dS t          |dd          }|rt          |dd          nt          |dd          }|sdS t          |dd          }t          |dd          }||dS dg}|rVt          |dd          }	t          |d	d          }
|	r|                    d
|	            |
r|                    d|
            |                    d|            |                    d|            |                    d| d|            |                    d           | 	                    |t          j        |j                  }d
                    |          |_        |                     |          }|                     |           d{V  dS )z,Handle incoming location/venue pin messages.Nr  venuelocationlatitude	longitudez![The user shared a location pin.]rS  addresszVenue: z	Address: z
latitude: zlongitude: z5Map: https://www.google.com/maps/search/?api=1&query=r(  zSAsk what they'd like to find nearby (restaurants, cafes, etc.) and any preferences.r   )r  r  rz  r  r,   r  r  r   r   r  r   rd   r  r  )r   rO  r  r  r  r  latlonr   rS  r  r  s               r%   r  z(TelegramAdapter._handle_location_message   sI     ,,V44 	F++C00 	==cBB o77[=Q]c]m7nnnFWd++7<`75*d333'#z[_B`B` 	Fh
D11hT22;#+F 55 	4E7D11EeY55G 0.u../// 4222333'#''((((3(()))XSXXSVXXYYYjkkk))#{/CvO_)``YYu%%
>>uEE!!%(((((((((((r$   c                     ddl m} |                     |            ||j        | j        j                            dd          | j        j                            dd                    S )a  Session-scoped key for text message batching.

        Applies the installed topic-recovery hook first so DM-topic batches
        coalesce on (and dispatch to) the recovered lane rather than the
        raw inbound ``message_thread_id`` Telegram may have attached.
        r   build_session_keygroup_sessions_per_userTthread_sessions_per_userFr  r  )r+  r  _apply_topic_recoveryr3  r   r   r   )r   r  r  s      r%   _text_batch_keyzTelegramAdapter._text_batch_key,  s}     	655555""5)))  L$(K$5$9$9:SUY$Z$Z%)[%6%:%:;UW\%]%]
 
 
 	
r$   c                    |                      |          }| j                            |          }t          |j        pd          }|||_        || j        |<   nw|j        r$|j        r|j         d|j         n|j        |_        ||_        |j        r>|j                            |j                   |j                            |j                   | j	                            |          }|r(|
                                s|                                 t          j        |                     |                    | j	        |<   dS )a2  Buffer a text event and reset the flush timer.

        When Telegram splits a long user message into multiple updates,
        they arrive within a few hundred milliseconds.  This method
        concatenates them and waits for a short quiet period before
        dispatching the combined message.
        r   Nr   )r  r   r   r   rd   _last_chunk_lenr  r   r  r   rh  r  r   r  _flush_text_batch)r   r  r  r  	chunk_len
prior_tasks         r%   r  z#TelegramAdapter._enqueue_text_event;  sP    ""5))-11#66
(b))	$-E!.3D&s++ z bDLM a8= @ @EJ @ @ @W\Wa'0H$ ?#**5+;<<<$++E,=>>> 377<<
 	 joo// 	 .5.A""3''/
 /
&s+++r$   c                   K   t          j                    }	 | j                            |          }|rt	          |dd          nd}|r t          t	          |dd          pd          nd}|| j        k    r| j        }nS|| j        k    rt          | j
        | j                  }n-|| j        k    rt          | j
        | j                  }n| j
        }t          j        |           d{V  | j                            |d          }|s<	 | j                            |          |u r| j                            |d           dS dS t"                              d|t          |j        pd                     |                     |           d{V  | j                            |          |u r| j                            |d           dS dS # | j                            |          |u r| j                            |d           w w xY w)zWait for the quiet period then dispatch the aggregated text.

        Uses a longer delay when the latest chunk is near Telegram's 4096-char
        split point, since a continuation chunk is almost certain.
        r  r   rd   r   Nz,[Telegram] Flushing text batch %s (%d chars))r   current_taskr   r   r   r   _SPLIT_THRESHOLDr   _TEXT_BATCH_FAST_LENr   r   _TEXT_BATCH_FAST_DELAY_S_TEXT_BATCH_SHORT_LEN_TEXT_BATCH_SHORT_DELAY_Sr  rx  r   r-  r  rd   r  )r   r  r  pendinglast_len	total_lenr  r  s           r%   r  z!TelegramAdapter._flush_text_batch[  sI      +--#	> 044S99GAHOww(91===aHCJQGGVR88>B???PQI4000<d777D:D<YZZd888D:D<Z[[6-&&&&&&&&&.223==E  -11#66,FF.223===== GF KK>S)r**   %%e,,,,,,,,,-11#66,FF.223===== GFt-11#66,FF.223==== Gs   C0G	 AG	 	:Hc                     ddl m}  ||j        | j        j                            dd          | j        j                            dd                    }t          |dd	          }|r| d
| S | dS )z1Return a batching key for Telegram photos/albums.r   r  r  Tr  Fr  media_group_idNz:album:z:photo-burst)r+  r  r3  r   r   r   r   )r   r  r  r  r7  r  s         r%   _photo_batch_keyz TelegramAdapter._photo_batch_key  s    555555''L$(K$5$9$9:SUY$Z$Z%)[%6%:%:;UW\%]%]
 
 

 !&6== 	;!::.:::++++r$   	batch_keyc                   K   t          j                    }	 t          j        | j                   d{V  | j                            |d          }|s<	 | j                            |          |u r| j                            |d           dS dS t          	                    d|t          |j                             |                     |           d{V  | j                            |          |u r| j                            |d           dS dS # | j                            |          |u r| j                            |d           w w xY w)z;Send a buffered photo burst/album as a single MessageEvent.Nz3[Telegram] Flushing photo batch %s with %d image(s))r   r  r  r   r   rx  r   r   r-  r  r   r  r  )r   r  r  r  s       r%   _flush_photo_batchz"TelegramAdapter._flush_photo_batch  sy     +--		E- ?@@@@@@@@@/33ItDDE  .229==MM/33ItDDDDD NM KKMyZ]^c^nZoZoppp%%e,,,,,,,,,.229==MM/33ItDDDDD NMt.229==MM/33ItDDDD Ns   <D A	D :Ec                    | j                             |          }||| j         |<   nj|j                            |j                   |j                            |j                   |j        r%|                     |j        |j                  |_        | j                            |          }|r(|                                s|	                                 t          j        |                     |                    | j        |<   dS )z;Merge photo events into a pending batch and schedule flush.N)r   r   r  r   r  rd   _merge_captionr   rh  r  r   r  r  )r   r  r  r  r  s        r%   _enqueue_photo_eventz$TelegramAdapter._enqueue_photo_event  s    .229==5:D'	22&&u'7888 ''(9:::z O $ 3 3HM5: N N488CC
 	 joo// 	 5<5HI`I`ajIkIk5l5l'	222r$   c                   K   |j         sdS |                     |j                   s|                     |j                   r|j         }|                     |          }|                     |||j                  }|j        r|                     |j                  |_        | 	                    ||           d{V  | 
                    ||j        |j        |           dS |j         }|                     |          }|                     |||j                  }|j        r|                     |j                  |_        |j        rN|                     ||           d{V  |                     |          }|                     |           d{V  dS |                     |          }|j        r~	 |j        d         }	|	                                 d{V }
|
                                 d{V }d}|
j        r5dD ]2}|
j                                                            |          r|} n3t-          t/          |          |          }|g|_        d|                    d	           g|_        t6                              d
|           t;          |dd          }|r*|                     t?          |          |           d{V  n,|                      ||          }| !                    ||           dS # tD          $ r'}t6          #                    d|d           Y d}~nd}~ww xY w|j$        rH	 | %                    |j$        d          \  }}|sn| &                    |j        |pd          |_        t6                              dt;          |j$        dd                     |                     |           d{V  dS |j$                                         d{V }
|
                                 d{V }tO          t/          |          d          }|g|_        dg|_        t6                              d|           n# tD          $ r(}t6          #                    d|d           Y d}~nd}~ww xY w|j(        rH	 | %                    |j(        d          \  }}|sn| &                    |j        |pd          |_        t6                              dt;          |j(        dd                     |                     |           d{V  dS |j(                                         d{V }
|
                                 d{V }tO          t/          |          d          }|g|_        dg|_        t6                              d|           n# tD          $ r(}t6          #                    d|d           Y d}~nfd}~ww xY w|j)        r	 |j)                                         d{V }
|
                                 d{V }d}t;          |
dd          r:tT          D ]2}|
j                                                            |          r|} n3tW          t/          |          |          }|g|_        tU          j,        |d          g|_        t6                              d |           nq# tD          $ r(}t6          #                    d!|d           Y d}~nDd}~ww xY w|j-        r4|j-        }	 d}|j.        pd}|r6t^          j0        1                    |          \  }}|                                }|j2        pd                                }|sR|rPtf          ,                    |d          }|s3d" ti          j5                    D             }|,                    |d          }|j6        r|j6        | j7        k    rR| j7        d#z  }d$| d%|_        t6                              d&|j6                   |                     |           d{V  dS |tp          v s|9                    d          r|                                 d{V }
|
                                 d{V }|tp          v r|ntf          ,                    |d          }	 t-          t/          |          |          }na# tt          $ rT}t6          #                    d'|d           d(|p|p|pd) d*|_        |                     |           d{V  Y d}~dS d}~ww xY wtv          j<        |_        |g|_        |9                    d          r|ntz          ,                    |d+          g|_        t6                              d,|           t;          |dd          }|r*|                     t?          |          |           d{V  n,|                      ||          }| !                    ||           dS |s?|j2        r8d- tU          j5                    D             }|,                    |j2        d          }|sS|j2        rLi }t}          j5                    D ]\  }} |?                    | |           |,                    |j2        d          }|tT          v r|                                 d{V }
|
                                 d{V }tW          t/          |          |          }|g|_        tT          |         g|_        tv          j@        |_        t6                              d.|           |                     |           d{V  dS |th          vr|d/A                    t          ti          jC                                        }!d0|pd) d1|! |_        t6                              d2|pd)           |                     |           d{V  dS |                                 d{V }
|
                                 d{V }"t/          |"          }#t          |#|pd3|           }th          |         }$|g|_        |$g|_        t6                              d4|           d5}%|d6v rt          |#          |%k    r	 |#F                    d7          }&|pd3| }'t          jH        d8d9|'          }'d:|' d;|& }(|j        r|( d<|j         |_        n|(|_        n,# t          $ r t6          #                    d=d           Y nw xY wn4# tD          $ r'}t6          #                    d>|d           Y d}~nd}~ww xY wt;          |dd          }|r+|                     t?          |          |           d{V  dS |                     |           d{V  dS )?zBHandle incoming media messages, downloading images to local cache.Nr  )r  r  rz   r=   )r>   r@   r<   r?   r=   r`  zimage/rC  z"[Telegram] Cached user photo at %sr  z$[Telegram] Failed to cache photo: %sTr   zvoice messager   z1[Telegram] Skipped oversized user voice (size=%s)r;  rL  r  z"[Telegram] Cached user voice at %sz$[Telegram] Failed to cache voice: %sz
audio filez1[Telegram] Skipped oversized user audio (size=%s)rX  z	audio/mp3z"[Telegram] Cached user audio at %sz$[Telegram] Failed to cache audio: %sz.mp4r[  r  z"[Telegram] Cached user video at %sz$[Telegram] Failed to cache video: %sc                     i | ]\  }}||	S r#   r#   r~   kr  s      r%   r  z9TelegramAdapter._handle_media_message.<locals>.<dictcomp>C  s    &Y&Y&Y1q!&Y&Y&Yr$   r>  zFThe document is too large or its size could not be verified. Maximum: z MB.z'[Telegram] Document too large: %s bytesz-[Telegram] Failed to cache image document: %szImage document 'rp  z ' could not be read as an image.rB   z+[Telegram] Cached user image-document at %sc                     i | ]\  }}||	S r#   r#   r  s      r%   r  z9TelegramAdapter._handle_media_message.<locals>.<dictcomp>r  s    (X(X(X$!QA(X(X(Xr$   z+[Telegram] Cached user video document at %srS  zUnsupported document type 'z'. Supported types: z([Telegram] Unsupported document type: %sr  z%[Telegram] Cached user document at %si  >   .md.txtr  z	[^\w.\- ]r  z[Content of z]:
r   zJ[Telegram] Could not decode text file as UTF-8, skipping content injectionz'[Telegram] Failed to cache document: %s)Jr  r  rz  r  r  r  rG  rv  rd   r  r  r  r  _handle_stickerr  r  r  r  r  r[  r,  r   r/   r  r  r   r  r-  r  r   _queue_media_group_eventr   r  r  rL   rv  rM  rE  r  r0   rY  r  r4   r1   r   r  r  r   r  r\  r  _TELEGRAM_IMAGE_MIME_TO_EXTr5   itemsr;  r  _TELEGRAM_IMAGE_EXTENSIONSr   r   r,   r  _TELEGRAM_IMAGE_EXT_TO_MIMEr6   r  r  r   sortedr  r2   r   r'  rm   rg   UnicodeDecodeError))r   rO  r  _m_observe_type_eventr  r  r  r  r  image_bytesr`  r
  cached_pathr  r  r   ry  r  audio_bytesvideo_bytesr  original_filenamer  doc_mimemime_to_extr?  	image_extvideo_mime_to_extimage_mime_to_ext_ext_mimesupported_list	doc_bytes	raw_bytesr  MAX_TEXT_INJECT_BYTEStext_contentr  	injections)                                            r%   r  z%TelegramAdapter._handle_media_message  s'     ~ 	F++FN;; 	==fnMM 	^ $ 8 8 < <222}PVP`2aa: K"&">">rz"J"JFK00V<<<<<<<<<77+v7Gv 8    Fn++C00))#x6CS)TT ; 	C55ckBBEJ ; 	&&sE222222222BB5IIE%%e,,,,,,,,,F >>uEE 9 	YY	"!&!1!1111111$,$B$B$D$DDDDDDD% "%O " "	#-3355>>yII ""+C!E" 5U;5G5GSQQQ$/= %?cjjoo%?%?$A!@+NNN!(.>!E!E! @77N8K8KUSSSSSSSSSS $ 5 5eS A AI--i??? Y Y YEqSWXXXXXXXXY 9 x	\Y $ A A#)_ ] ] !%!;!;EJ
PR!S!SEJKK SU\]`]fhsuyUzUz{{{--e444444444F!$!3!3!5!5555555$,$B$B$D$DDDDDDD4U;5G5GVTTT$/= %0M!@+NNNN Y Y YEqSWXXXXXXXXYY h	\Y $ A A#)\ Z Z !%!;!;EJ
PR!S!SEJKK SU\]`]fhsuyUzUz{{{--e444444444F!$!3!3!5!5555555$,$B$B$D$DDDDDDD4U;5G5GVTTT$/= %0M!@+NNNN Y Y YEqSWXXXXXXXXY Y W	\Y!$!3!3!5!5555555$,$B$B$D$DDDDDDD8[$77 "%: " "	#-3355>>yII ""+C!E" 5U;5G5GSQQQ$/= %:%>sK%P%P$Q!@+NNNN Y Y YEqSWXXXXXXXXY \ E	\,CC\$'M$7R!$ &W--.?@@FAs))++C  M/R6688  <x <599(BGGC <&Y&Y8P8V8X8X&Y&Y&Y)ooh;; } 8K(K(K#2{CH3$,3 3 3 J KK I3=YYY--e444444444F
 4448K8KH8U8U4%(\\^^333333H(0(F(F(H(H"H"H"H"H"H"HK'*.H'H'HNiNmNmnvx~NNI	&<U;=O=OU^&_&_&_%   'VXYdhiii=/@/`H/`PS/`W` = = = 
 #11%888888888 *5):E&(3}E$5=5H5H5R5R  *QXsXwXw  yB  DP  YQ  YQ  )RE%KK M{[[[%,S2BD%I%IN% D";;C<O<OQVWWWWWWWWWW$($9$9%$E$E	11)UCCCF Cs} C(X(X:O:U:W:W(X(X(X%+//rBBC Cs} C 9;%'E'K'M'M B Be)44UDAAAA+//rBBC///%(\\^^333333H(0(F(F(H(H"H"H"H"H"H"HK"8{9K9KQT"U"U"UK(3}E$)>s)C(DE%)4):E&KK M{[[[--e444444444F 666%)YYv6N6S6U6U/V/V%W%WN=c6FY = =,:= = J KK JCL\S\]]]--e444444444F "%//////"*"@"@"B"BBBBBBB	!),,	7	CTChXhcfXhXhii4S9	$/= %.K!C[QQQ )3%/))c)nn@U.U.U'0'7'7'@'@'8'L<Ls<L<L')vlC'N'N$S<$S$S\$S$S	 : 3,5)G)G5:)G)GEJJ)2EJ-   h%) '       \ \ \H!VZ[[[[[[[[\ !&6== 	//N0C0CUKKKKKKKKKF!!%(((((((((((s   2EK; ;
L,L''L,8BQ
 BQ
 

Q<Q77Q<BV BV 
W$WWC"Z< <
[.[))[.Ds9 A9s9 b5 4s9 5
d?A	ds9 dCs9 -Es9 Bs9 B)s9 1As s9 &s52s9 4s55s9 9
t*t%%t*r  c                   K   | j                             |          }||| j         |<   nj|j                            |j                   |j                            |j                   |j        r%|                     |j        |j                  |_        | j                            |          }|r|                                 t          j
        |                     |                    | j        |<   dS )a  Buffer Telegram media-group items so albums arrive as one logical event.

        Telegram delivers albums as multiple updates with a shared media_group_id.
        If we forward each item immediately, the gateway thinks the second image is a
        new user message and interrupts the first. We debounce briefly and merge the
        attachments into a single MessageEvent.
        N)r   r   r  r   r  rd   r  r   r  r   r  _flush_media_group_event)r   r  r  r  r  s        r%   r  z(TelegramAdapter._queue_media_group_event  s       +//??7<D$^44&&u'7888 ''(9:::z O $ 3 3HM5: N N,00@@
 	 292E)).993
 3
///r$   c                   K   	 t          j        | j                   d {V  | j                            |d           }||                     |           d {V  n1# t           j        $ r Y | j                            |d            d S w xY w| j                            |d            d S # | j                            |d            w xY wr   )r   r  MEDIA_GROUP_WAIT_SECONDSr   rx  r  CancelledErrorr   )r   r  r  s      r%   r  z(TelegramAdapter._flush_media_group_event  s      	>- =>>>>>>>>>,00FFE ))%000000000% 	 	 	#''=====	 #''=====D#''====s*   AA B* B
+B* 	B

B* *Cr+   c                 (  K   ddl m}m}m}m}m} |j        }|j        pd}	|j        pd}
|j	        s|j
        r ||	          |_        dS  ||j                  }|rb ||d         |                    d|	          |                    d|
                    |_        t                              d|j                   dS 	 |                                 d{V }|                                 d{V }t%          t'          |          d	
          }t                              d|           ddlm}  |||           d{V }t-          j        |          }|                    d          r=|                    dd          } ||j        ||	|
            |||	|
          |_        dS  ||	rd|	 nd|	|
          |_        dS # t0          $ rA}t                              d|d            ||	rd|	 nd|	|
          |_        Y d}~dS d}~ww xY w)a  
        Describe a Telegram sticker via vision analysis, with caching.

        For static stickers (WEBP), we download, analyze with vision, and cache
        the description by file_unique_id. For animated/video stickers, we inject
        a placeholder noting the emoji.
        r   )get_cached_descriptioncache_sticker_descriptionbuild_sticker_injection build_animated_sticker_injectionSTICKER_VISION_PROMPTr   NrD  emojiset_namez [Telegram] Sticker cache hit: %sr@   r  z"[Telegram] Analyzing sticker at %s)vision_analyze_tool)rz  user_promptr  analysisz	a stickerza sticker with emoji z%[Telegram] Sticker analysis error: %sTr   )gateway.sticker_cacher#  r$  r%  r&  r'  r  r(  r)  is_animatedis_videord   file_unique_idr   r-  r  r  r  r/   r  tools.vision_toolsr*  r:  r;  rL   rv  )r   r  r  r#  r$  r%  r&  r'  r  r(  r)  r  r  r  r  r*  result_jsonr  rD  r   s                       r%   r   zTelegramAdapter._handle_sticker  s     	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 +##)r  	'"2 	99%@@EJF ('(>?? 	00}%vzz'5'A'A6::jZbCcCc EJ KK:G<RSSSF	$--////////H ( > > @ @@@@@@@K0{1C1CQQQKKK<kJJJ>>>>>> 3 3%1! ! !      K Z,,Fzz)$$ 	$jj[AA))'*@+uV^___44[%RR


 547<M3E333+8 


  	 	 	NNBAPTNUUU0038I////kx EJJJJJJJ	s   ?C*G +G 
H6HHc                    	 ddl m}  |            dz  }|                                sdS ddl}t	          |dd          5 }|                    |          pi }ddd           n# 1 swxY w Y   |                    di                               d	i                               d
i                               dg           }|sg | _        t                      | _	        dS || _        d |D             | _	        |D ]}|                    d          }|s|                    dg           D ]y}	|	                    d          }
|	                    d          }|
rK|rI| d| }|| j
        vr9t          |
          | j
        |<   t                              d| j        ||
           zdS # t          $ r,}t                              d| j        |           Y d}~dS d}~ww xY w)zRe-read dm_topics from config.yaml and load any new thread_ids into cache.

        This allows topics created externally (e.g. by the agent via API) to be
        recognized without a gateway restart.
        r   r  r  Nr  r  r  r  rM   r   r   c                 @    h | ]}d |v t          |d                    S r   r   )r~   r  s     r%   r   z@TelegramAdapter._reload_dm_topics_from_config.<locals>.<setcomp>C  s7     ' ' '/9)WaJaJaJy)**JaJaJar$   r   r   r  r   r  z8[%s] Hot-loaded DM topic from config: %s -> thread_id=%sz/[%s] Failed to reload dm_topics from config: %s)r   r  r!  r"  r#  r$  r   r  r   r  r   rC  r-  r  r   rL   r.  )r   r  r/  r0  r1  r   r   r  cidr6  tidr   r  r   s                 r%   _reload_dm_topics_from_configz-TelegramAdapter._reload_dm_topics_from_config$  s   ,	Z888888)/++m;K%%''     k3999 2Q++1r2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 

;++Z$$Wb!!["%%	   )+&*-%%' &/D"' '=F' ' 'D# (  
 nnY// #"55 
 
A%%,,C55==D t '*OOTOO	$DO;;9<SDOI6"KK Z $	9c  
	   	Z 	Z 	ZLLJDIWXYYYYYYYYY	ZsH   'F' F' A%F' %A))F' ,A)-A1F'  CF' '
G1!GGc                 x   |sdS t          |          }| j                                        D ]\  }}||k    r|                    | d          r|                    dd          d         }| j        D ]b}t          |                    d                    |k    r:|                    dg           D ]#}|                    d          |k    r|c c c S $cd|ic S |                                  | j                                        D ]\  }}||k    r|                    | d          r|                    dd          d         }| j        D ]b}t          |                    d                    |k    r:|                    dg           D ]#}|                    d          |k    r|c c c S $cd|ic S dS )zLook up DM topic config by chat_id and thread_id.

        Returns the topic config dict (name, skill, etc.) if this thread_id
        matches a known DM topic, or None.
        Nr  ry   r   r   r   )	rC  r   r  r   r   r  r   r   r7  )	r   r   r  thread_id_intr  
cached_tidr  r  r6  s	            r%   _get_dm_topic_infoz"TelegramAdapter._get_dm_topic_infoX  s     	4I  $4466 		, 		,OC]**s~~mmm/L/L* YYsA..q1
"&"8 ) )J:>>)4455@@!+"!=!= ) )A uuV}}
::'(  ;
++++ 	**,,,  $4466 	, 	,OC]**s~~mmm/L/L* YYsA..q1
"&"8 ) )J:>>)4455@@!+"!=!= ) )A uuV}}
::'(  ;
++++tr$   c                     | d| }|| j         vr;t          |          | j         |<   t                              d| j        ||           dS dS )zLCache a thread_id -> topic_name mapping discovered from an incoming message.r  z5[%s] Cached DM topic from message: %s -> thread_id=%sN)r   rC  r-  r  r   )r   r   r  r  r  s        r%   _cache_dm_topic_from_messagez,TelegramAdapter._cache_dm_topic_from_message  sh    ----	DO++),YDOI&KKG	9i     ,+r$   c                    |j         }|j        }t          t          |dd                                        d          d                                         }d}|dv rd}n|dk    rd}|j        }t          t          |d	d
                    }	t          |dd
          du }
d}|1|dk    r|	s|
rt          |          }n|dk    r|	rt          |          }|dk    r|	|
r| j        }d}d}|dk    r|r| 	                    t          |j
                  |          }|r*|                    d          }|                    d          }t          |d          rB|j        r;|j        j        }|r-|                     t          |j
                  ||           |s|}n|dk    r|r| j        j                            dg           }|D ]}t          |                    dd                    t          |j
                  k    rq|                    dg           D ]X}|                    d          }|?t          |          |k    r,|                    d          }|                    d          } nY n|                     t          |j
                  |j        pt          |d          r|j        nd||rt          |j
                  n|dv rt          |j
                  nd|r|j        n+t          |d          r|dk    r|j        n|dk    r|j        nd||t          |j                            }d}d}|j        r^t          |j        j                  }t          |dd          }|t          |dd          nd}|r|}n|j        j        p|j        j        pd}ddlm} t          |j
                  } || j        j        |p||r|nd          }t7          |j        pd|||t          |j                  ||||||j                  S )aO  Build a MessageEvent from a Telegram message.

        ``update_id`` is the ``Update.update_id`` from PTB; passing it through
        lets ``/restart`` record the triggering offset so the new gateway
        process can advance past it (prevents ``/restart`` being re-delivered
        when PTB's graceful-shutdown ACK fails).
        r  r   rC  rz   r  >   r  r  r  r  is_topic_messageFr  TNr   skillforum_topic_createdgroup_topicsr   r   r  r  >   r  r  )r   	chat_namer  r  r  r  
chat_topicr  quoterd   r   )resolve_channel_prompt)rd   r  r3  r  r  platform_update_idrQ  reply_to_text
auto_skillr  r  )r  r  r   r   r   r,  r8  rt   rW  r;  r  r   r,  rA  r   r=  r   r   build_sourcerS  r  r  rF  rd   rG  r  rF  r+   date)r   r  r  r  r  rh  telegram_chat_typer  thread_id_rawr?  is_forum_groupthread_id_strrD  topic_skill
topic_infocreated_namegroup_topics_configr  r  r6  r3  r  rH  rE  
quote_textrF  _chat_id_str_channel_prompts                               r%   r  z$TelegramAdapter._build_message_event  s    | 
 !vr!:!:;;AA#FFrJPPRR	!888II9,,!I  11CU K KLL z599TA$G##)9#^# #M 2 2d""'7" #M 2 2 M$9n$9 9M
00TW}MMJ 6'^^F33
(nnW55 w 566 27;V 2&:? 255c$'llMS_```% 2%1
'!!m!(,(9(=(=nb(Q(Q1  
z~~i4455TWEE!+"!=!= " "#ii44?s3xx=/H/H).6):):J*/))G*<*<K!EE F ""LLj\wt[7Q7Q%[T^^W[ PDG&/3D&D&Dc$'lll$  t[11J6?46G6G NN(1Y(>(>$**D $!7-..) # 
 
> # 	g6ABBKGWd33E9>9J555PTJ  * ,1 /7  	BAAAAA47||00K)\)3LLt
 
 #!7-..( +'"*l
 
 
 	
r$   c                 T    t          j        dd                                          dvS )z6Check if message reactions are enabled via config/env.TELEGRAM_REACTIONSr  >   r  r  r  )r   r   r,  r   s    r%   _reactions_enabledz"TelegramAdapter._reactions_enabled   s'    y-w77==??G[[[r$   r(  c                   K   | j         sdS 	 | j                             t          |          t          |          |           d{V  dS # t          $ r-}t                              d| j        ||           Y d}~dS d}~ww xY w)z2Set a single emoji reaction on a Telegram message.Fr   r  reactionNTz)[%s] set_message_reaction failed (%s): %sr   set_message_reactionrC  rL   r-  r.  r   )r   r   r  r(  r   s        r%   _set_reactionzTelegramAdapter._set_reaction$  s      y 	5		)00Gz?? 1         
 4 	 	 	LLDdiQVXYZZZ55555	s   =A 
B"A>>Bc                 
  K   | j         sdS 	 | j                             t          |          t          |          d           d{V  dS # t          $ r,}t                              d| j        |           Y d}~dS d}~ww xY w)uN  Clear all reactions from a Telegram message.

        Calling ``set_message_reaction`` with ``reaction=None`` (or an empty
        sequence) is the documented Bot API way to remove all bot-set
        reactions on a message — equivalent to Bot API 10.0's
        ``deleteMessageReaction`` but supported in PTB 22.6 already.
        FNr[  Tz[%s] clear reactions failed: %sr]  r)  s       r%   _clear_reactionsz TelegramAdapter._clear_reactions3  s       y 	5		)00Gz?? 1         
 4 	 	 	LL:DIqIII55555	s   =A 
B!A==Bc                    K   |                                  sdS t          |j        dd          }t          |dd          }|r!|r!|                     ||d           d{V  dS dS dS )z;Add an in-progress reaction when message processing begins.Nr   r  u   👀)rY  r   r3  r_  )r   r  r   r  s       r%   on_processing_startz#TelegramAdapter.on_processing_startH  s      &&(( 	F%,	488UL$77
 	Hz 	H$$Wj,GGGGGGGGGGG	H 	H 	H 	Hr$   outcomec                 J  K   |                                  sdS t          |j        dd          }t          |dd          }|r|sdS |t          j        k    r|                     ||           d{V  dS |                     |||t          j        k    rdnd           d{V  dS )u  Swap the in-progress reaction for a final success/failure reaction.

        Unlike Discord (additive reactions), Telegram's set_message_reaction
        replaces all existing reactions in one call — no remove step needed.

        On CANCELLED outcomes (e.g. the user runs ``/stop``, or a session is
        interrupted mid-flight), we explicitly clear the 👀 in-progress
        reaction so it doesn't linger on the user's message indefinitely.
        Without this clear, the only way to remove the 👀 was to wait for
        another agent run to swap it to 👍/👎 — which never happens if the
        cancellation was the last activity in the chat.
        Nr   r  u   👍u   👎)rY  r   r3  r-   	CANCELLEDra  r_  SUCCESS)r   r  rd  r   r  s        r%   on_processing_completez&TelegramAdapter.on_processing_completeQ  s       &&(( 	F%,	488UL$77
 	J 	F'111''<<<<<<<<<<<$$ '+<+D D D,          r$   )NN)NNNr   r  rt  )r   r   N)rB  N)rO  r
   r  r  rF   N)Nre  )NNNN)r   r    r!   __doc__r  supports_code_blocksr  r   rW  r   rt   __annotations__r  r  r  r  staticmethodr   r   r   r   propertyr   r)   r   r   r	   r  r6  classmethodr;  r@  rC  rE  rG  rK  rM  rP  rU  rS  rZ  rL   r^  re  rp  r{  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r>  r  r  r  r.   r  r  r  r
  r(  r-  r4  r6  rA  rR  rW  rd  rx  r  rv  r  r  r  r  r   r  r:  rB  rE  r]  r   ru  r  r_  r  r  r  r  r  r  r  r  r  r  r   r  r  r%  r)  r/  r4  rm   Patternr   r   rD  rH  rd  rm  ro  rr  rt  rv  rz  r~  r+   r  r  r  r,   r  r  r  r  r  r  r  r
   r  r   r"   r  r  r  r  r  r  r  r  r  r  r  r  r   r7  r;  r=  r  rY  r_  ra  rc  r-   rh  __classcell__)r
  s   @r%   r   r   P  s           "" $(D''' #  $
 &*%)   E?	
 E? 
   \6   XY8~ Y8 Y8 Y8 Y8 Y8 Y8v. c3h0.	c3h. . . .& "&#'#'#'2G 2G 2G2G #	2G
 C=2G C=2G C=2G 
2G 2G 2G 2Gh A8DcN+C AQT A A A [A ?(4S>:R ?W_`cWd ? ? ? [? ?Xd38n5M ?RZ[^R_ ? ? ? [? S T    \ 

 C=
 4S>*	

 

 
 
 [
, rC r r r \r  .2'+	 3- 4S>*  }	
 
#   [ 
 .2-1'+*Q *Q*Q C=*Q 4S>*	*Q
 &c]*Q  }*Q 
c3h*Q *Q *Q [*QX HSM hsm    [
 
hsm 
QT 
 
 
 [
 8) 8 8 8 8 \8 Y 4    \ )) 4S>*) &c]	)
 
) ) ) [)d &* 1  1 1 #s(^ 1 4S>*	 1
 &c] 1  1 c] 1 
 1  1  1  1DctCy c c c c 
I 
$ 
 
 
 \
 * *t * * * \* 9     \6  	  d       \ D c D T    2d38n 2 2 2 2) ) ) )VOG OGt OG OG OG OGb+@ +@ +@ +@Zn)I n)$ n) n) n) n)h %).21 11 1 SM	1
 'sm1 
#1 1 1 1f55 5 
#	5 5 5 5$3 3S 3c 3QU 3bjknbo 3 3 3 3j

 
 	

 

 
 
 
8 "'Qj QjQj Qj 	Qj
 Qj 
Qj Qj Qj QjfR R R Rhkt k k k kZ	B B B BB$Xc] $ $QU $ $ $ $0 #'-1S@ S@S@ S@ 3-	S@
 4S>*S@ 
S@ S@ S@ S@v .2         	  4S>*  
       P -1K; K; K;K; K; 	K; K; 4S>*K; 
K; K; K; K;h .2D
 D
 D
D
 D
 	D
 D
 4S>*D
 
D
 D
 D
 D
LC S T    6 $(-1> >C=> 4S>*> 
	> > > >0 .2JA JAJA JA 	JA
 4S>*JA 
JA JA JA JAX  B 9;-1); ););$');25);); 4S>*); 
	); ); ); );Z /-1F; F;F;%(F;7:F;F; 4S>*F; 
	F; F; F; F;T ?C.; .;.;#&.;14.;CF.;.;#+DcN#;.; 
.; .; .; .;n .2P; P;P; P; $	P;
 P; P; 4S>*P; 
P; P; P; P;t .2E; E;E; E; 	E;
 E; E; 4S>*E; 
E; E; E; E;N 0*$ 0* 0* 0* 0*d'5D '5 '5 '5 '5 '5 '5Rj!j!),j!	j! j! j! j!X	ZSZS)DZS	ZS ZS ZS ZSH )r;KUZ[%r>UZ[)r;NUZ["r;LUZ[&y;UYZ&z;MUYZ-y=UYZ-z;OUYZ%y;KUYZ%z;RUYZ \ \ 
\ \ \ \|s # #    
C 
C 
TW 
\_ 
 
 
 
R3 Rs RuTS[\_S`M`Ga R R R R$ "&"&-1Yg YgYg Yg #	Yg
 3-Yg 4S>*Yg 
Yg Yg Yg Yg Yg Yg~ .2 E EE UE 4S>*	E
 E 
E E E E E EV "&"&-1[p [p[p [p #	[p
 3-[p 4S>*[p 
[p [p [p [p [p [pB "&#'"&-11t 1t1t 1t #	1t
 C=1t 3-1t 4S>*1t 
1t 1t 1t 1t 1t 1tn "&"&-1-g -g-g -g #	-g
 3--g 4S>*-g 
-g -g -g -g -g -gf "&"&-1\j \j\j \j #	\j
 3-\j 4S>*\j 
\j \j \j \j \j \jD "&"&-1-g -g-g -g #	-g
 3--g 4S>*-g 
-g -g -g -g^! ! !c3h8P !\` ! ! ! !F I3  I4S>  I  I  I  IDjc jc j j j j\d4 d d d dwd w w w w"_d _ _ _ _j$ j j j jNs3x N N N NNS N N N NNs3x N N N NS    N#c( N N N N3s8    *$4
+; $ $ $ $L4g 4$ 4 4 4 4f fD f f f f 3' 3'CH 3' 3' 3' \3'j6W 6 6 6 6 6p]7 ]t ] ] ] ]0	 	T 	 	 	 	S ST S S S SHSM hsm    / /T / / / /b[ [ [;\ ;c ; ; ; ;


 

 

 

 


| 
P\ 
 
 
 
6$w $; $ $ $ $3[w 3[| 3[PT 3[ 3[ 3[ 3[j"' " " " " ' 'S 'S ' ' ' \' $((,!c !c!c !c C=	!c
 %!c 
!c !c !c !cF OT L? L? L?w L?t L?X\ L? L? L? L?\` ` ` `2^ ^8G;L ^ ^ ^ ^( (,B[ (`d ( ( ( (*)F )\=V )[_ ) ) ) )&)V &)lF_ &)dh &) &) &) &)X
\ 
c 
 
 
 

 
$ 
 
 
 
@*>3 *>4 *> *> *> *>`,l , ,S , , , ,E# E$ E E E Emc m, m4 m m m m"F)& F)<C\ F)ae F) F) F) F)P
S 
 
Z^ 
 
 
 
2	>S 	>T 	> 	> 	> 	>? ? ?D ? ? ? ?B2Z 2Z 2Z 2Zh%# %(3- %HUYZ]_bZbUcLd % % % %NC C UX ]a     $(	S
 S
S
 S
 C=	S

 
S
 S
 S
 S
n\D \ \ \ \3 C  PT    c s t    *H| H H H H H, IZ _c        r$   r   )_ri  r   r}  r:  loggingr   r%  htmlrJ  rm   r   r   r  r   r   r   r   r	   	getLoggerr   r-  rM   r
   r   r   r   r   r   rN   rO   r   r   r   r   rR   r   r   rP   r   r   rQ   r   rJ   r   syspathlibr&   r!  r  insertr   __file__r  parentsgateway.configr(   r)   r  r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   "gateway.platforms.telegram_networkr8   r9   r:   utilsr;   r  r  r  r  rt   rc   r<  rf   ri   ro   r   rw   r  r   r   r   r   r#   r$   r%   <module>r|     sS           				      				 ' ' ' ' ' ' ' ' 1 1 1 1 1 1 1 1 1 1 1 1 1 1		8	$	$&%YYYYYYYYYYYYYY"/////// " " "!"                76666666------ % % %F
CGKN LGIH       $LLL+%. 


 ! ! ! ! ! ! 33uuX..008;<< = = = 3 3 3 3 3 3 3 3                                         
 !          GGG        4T 4 4 4 4r "*<==.s .s . . . .
c c    D !bj7  
. . . . . .:C :DI : : : :1($s) 1( 1( 1( 1( 1(h2 2 2 2 2 2j[\ [\ [\ [\ [\) [\ [\ [\ [\ [\s6   
B A  B  A*'B )A**#B 2CC