
    ih                    l   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	m
Z
mZmZmZ  ej        e          Z	 ddlmZmZmZmZmZ 	 ddlmZ n# e$ r dZY nw xY wddlmZmZmZmZmZm Z  ddl!m"Z"m#Z# ddl$m%Z% d	Z&n8# e$ r0 d
Z&eZeZeZeZeZdZeZeZeZeZeZ%dZ dZ"dZ# G d d          Z'e'ZY nw xY wddl(Z(ddl)m*Z+ e(j,        -                    d e. e+e/          0                                j1        d                              ddl2m3Z3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZEmFZFmGZG ddlHmIZI deJfdZK ejL        d          ZMde.de.fdZNde.de.fdZO ejL        d          ZPde.deJfdZQde.deRe.         fdZSdeRe.         de.fdZTde.de.fdZU G d  d!e6          ZVdS )"z
Telegram platform adapter.

Uses python-telegram-bot library for:
- Receiving messages from users/groups
- Sending responses back
- Handling media and commands
    N)DictListOptional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   9   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	utf16_len_prefix_within_utf16_limit)TelegramFallbackTransportdiscover_fallback_ipsparse_fallback_ip_env)atomic_replacereturnc                      t           S )z-Check if Telegram dependencies are available.)TELEGRAM_AVAILABLEr   r   r   check_telegram_requirementsr8   Z   s    r   z([_*\[\]()~`>#\+\-=|{}.!\\])textc                 8    t                               d|           S )zIEscape Telegram MarkdownV2 special characters with a preceding backslash.z\\\1)_MDV2_ESCAPE_REsubr9   s    r   _escape_mdv2r>   d   s    w---r   c                     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(?<!\w)_([^_]+)_(?!\w)z	~([^~]+)~z\|\|([^|]+)\|\|rer<   )r9   cleaneds     r   _strip_mdv2rD   i   sm     f6tDDG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rE   strippeds     r   _is_table_rowrL      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.rG      Nc                 6    g | ]}|                                 S r   rH   .0cells     r   
<listcomp>z-_split_markdown_table_row.<locals>.<listcomp>   s     999TDJJLL999r   )rH   
startswithendswithsplitrJ   s     r   _split_markdown_table_rowrY      su    zz||H3  ABB< !CRC=99X^^C%8%89999r   table_blockc                 <   t          |           dk     rd                    |           S t          | d                   }t          |          dk     rd                    |           S g }t          | dd         d          D ]\  }}t          |          }t          |          t          |          k     r7|                    dgt          |          t          |          z
  z             n7t          |          t          |          k    r|dt          |                   }t          d	 |D             d
|           }|                    d| d           |                    d t          ||          D                        d                    |          S )z<Render a detected GFM table as Telegram-friendly row groups.   
r   r    NrN   )start c              3      K   | ]}||V  	d S Nr   rR   s     r   	<genexpr>z3_render_table_block_for_telegram.<locals>.<genexpr>   s'      77$7777777r   zRow **c              3   ,   K   | ]\  }}d | d| V  dS )u   • z: Nr   )rS   headervalues      r   rb   z3_render_table_block_for_telegram.<locals>.<genexpr>   sH       
 
)6$6$$U$$
 
 
 
 
 
r   

)lenjoinrY   	enumerateextendnextappendzip)rZ   headersrendered_rowsindexrowcellsheadings          r    _render_table_block_for_telegramru      s   
;!yy%%%'A77G
7||ayy%%%!MABBq999 
 

s)#..u::G$$LL"WE

!:;<<<<ZZ#g,,&&.CLL.)E77777HH-'---... 
 
:=gu:M:M
 
 
 	
 	
 	
 	
 ;;}%%%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.
    rG   -r]   Fr   ```rN   r    )
rX   rh   lstriprV   rm   _TABLE_SEPARATOR_REmatchrL   ru   ri   )	r9   linesoutin_fenceirE   rK   rZ   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                   d
    e Zd ZdZdZdZdZdZd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
         dee         fd            Zedee
         dee         fd            Zededefd            Zdee
         fdZededefd            Zededefd            Zdde
dedefdZdee
ef         fdZddZdeddfd Zdd!Z deddfd"Z!	 	 dded#e
d$ee         d%ee
         dee         f
d&Z"ded'e
deddfd(Z#dd)Z$defd*Z%dd+Z&d,ee
         d-edefd.Z'	 	 dde
d/e
d,ee
         deee
ef                  de(f
d0Z)dd1de
d2e
d/e
d3ede(f
d4Z*de
d2e
defd5Z+	 	 	 dde
d7e
de
d8e
deee
ef                  de(fd9Z,	 	 dde
d;e
d8e
d<e
deee
ef                  de(fd=Z-	 dde
d>e
d?e
d8e
d@e
deee
ef                  de(fdAZ.	 dde
dBedCe
dDe
d8e
deee
ef                  de(fdEZ/dFZ0dGedHede1fdIZ2dJe
de
ddfdKZ3	 	 	 	 	 	 ddPZ4dQe
dRe
de
fdSZ5	 	 	 dde
dTe
dUee
         d,ee
         deee
ef                  de(f fdVZ6	 	 dde
dXe7e1         deee
ef                  dYe8ddf
 fdZZ9	 	 	 dde
d[e
dUee
         d,ee
         deee
ef                  de(f fd\Z:	 	 	 	 dde
d]e
dUee
         d^ee
         d,ee
         deee
ef                  de(f fd_Z;	 	 	 dde
d`e
dUee
         d,ee
         deee
ef                  de(f fdaZ<	 	 	 dde
dbe
dUee
         d,ee
         deee
ef                  de(f fdcZ=	 	 	 dde
dde
dUee
         d,ee
         deee
ef                  de(fdeZ>dde
deee
ef                  ddfdfZ?de
dee
ef         fdgZ@d/e
de
fdhZAdefdiZBdeCe
         fdjZDdeCe         fdkZEde7eFjG                 fdlZHd?eIdefdmZJd?eIdefdnZKd?eIdefdoZLd?eIdefdpZMdqee
         dee
         fdrZNddsd?eIdtedefduZOdLePdNeQjR        ddfdvZSdLePdNeQjR        ddfdwZTdLePdNeQjR        ddfdxZUdyeVde
fdzZWdyeVddfd{ZXde
ddfd|ZYdyeVd}eIde
fd~ZZde
ddfdZ[de
dyeVddfdZ\dLePdNeQjR        ddfdZ]de
dyeVddfdZ^de
ddfdZ_d}eIdydddfdZ`ddZade
dee
         deee
ef                  fdZbde
de
d'e
ddfdZc	 dd?eIdeddee         deVfdZedefdZfde
d2e
de
defdZgdyeVddfdZhdyeVdeiddfdZj xZkS )TelegramAdapterz
    Telegram bot adapter.

    Handles:
    - Receiving messages from users and groups
    - Sending responses with Telegram markdown
    - Forum topics (thread_id support)
    - Media messages
    i   i  g?1configc                    t                                          |t          j                   d | _        d | _        d| _        |                                 | _        t          |dd          pd| _
        |                     dd          | _        t          t          j        dd                    | _        i | _        i | _        i | _        i | _        t          t          j        dd                    | _        t          t          j        d	d
                    | _        i | _        i | _        d | _        d| _        d| _        d | _        i | _        | j        j                             dg           | _!        i | _"        i | _#        i | _$        d S )NFreply_to_modefirstdisable_link_previews)HERMES_TELEGRAM_MEDIA_BATCH_DELAY_SECONDSz0.8(HERMES_TELEGRAM_TEXT_BATCH_DELAY_SECONDSz0.6.HERMES_TELEGRAM_TEXT_BATCH_SPLIT_DELAY_SECONDSz2.0r   	dm_topics)%super__init__r!   TELEGRAM_app_bot_webhook_mode_compile_mention_patterns_mention_patternsgetattr_reply_to_mode_coerce_bool_extra_disable_link_previewsfloatosgetenv_media_batch_delay_seconds_pending_photo_batches_pending_photo_batch_tasks_media_group_events_media_group_tasks_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
_dm_topicsr   extraget_dm_topics_config_model_picker_state_approval_state_slash_confirm_state)selfr   	__class__s     r   r   zTelegramAdapter.__init__   sq   !2333+/	#'	#(!%!?!?!A!A#*6?G#L#L#WPW,0,C,CD[]b,c,c# +0	:egl0m0m*n*n'?A#CE'<> ;= */ry9cej/k/k)l)l&/4RY?oqv5w5w/x/x,>@"BD&;? ,-$12)+/(*,7;{7H7L7L[Z\7]7]46 /1 57!!!r   Nchat_id	chat_type	thread_id	user_nameuser_idr   r   r   r   r5   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d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_USERSc                 ^    h | ]*}|                                 |                                 +S r   rQ   )rS   uids     r   	<setcomp>z?TelegramAdapter._is_callback_user_authorized.<locals>.<setcomp>R  s-    TTTs		Tsyy{{TTTr   ,*)strrH   r   callablegateway.sessionr   lowerr!   r   rI   	Exceptionloggerdebugr   r   rX   )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%  s    !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 	4TTk.?.?.D.DTTTk!F%7;%FFs   )CD1 1'EEmetadatac                     |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_idU  sG     	4LL--R>Q1R1R	!*!6s9~~~D@r   c                 X    |rt          |          | j        k    rd S t          |          S ra   )r   _GENERAL_TOPIC_THREAD_IDintr   r   s     r   _message_thread_id_for_sendz+TelegramAdapter._message_thread_id_for_send\  s.     	C	NNc.JJJ49~~r   c                 (    |sd S t          |          S ra   )r   r   s     r   _message_thread_id_for_typingz-TelegramAdapter._message_thread_id_for_typingb  s     	49~~r   errorc                 H    dt          |                                           v S )Nzthread not found)r   r   )r   s    r   _is_thread_not_found_errorz*TelegramAdapter._is_thread_not_found_errorh  s    !SZZ%5%5%7%777r   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 ra   )r   )rS   vs     r   rb   z0TelegramAdapter._fallback_ips.<locals>.<genexpr>q  s(      -I-Ic!ff-I-I-I-I-I-Ir   )	r   r   r   r   
isinstancer   rX   r3   ri   r   
configureds     r   _fallback_ipszTelegramAdapter._fallback_ipsl  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   r9   s     r   _looks_like_polling_conflictz,TelegramAdapter._looks_like_polling_conflicts  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.)networkerrortimedoutconnectionerrorTr   NetworkErrorTimedOut)	r   r   r   telegram.errorr   r   r   ImportErrorOSError)r   namer   r   s       r   _looks_like_network_errorz)TelegramAdapter._looks_like_network_error|  s     '--//BBB4	========%,!9:: t 	 	 	D	%)))s   A 
AAFkeydefaultc                 (   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   truer   yesonT)false0nooffF)	r   r   r   r   r   r   rH   r   rI   )r   r  r  rf   lowereds        r   r   z"TelegramAdapter._coerce_bool_extra  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_requestr   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_        | 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
      <   rN   zXTelegram polling could not reconnect after %d network error retries. Restarting gateway.z[%s] %s Last error: %stelegram_network_errorT	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   _set_fatal_error_notify_fatal_errorminwarningasynciosleepr   updaterrunningstopr   r  start_pollingr   	ALL_TYPESr   infoensure_future_verify_polling_after_reconnect_background_tasksadd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   r8  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D+ +
D87D8CH 
K)BJ==Kc                 P  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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 heartbeatz;[%s] Polling heartbeat probe failed %ds after reconnect: %s)r*  r+  r%  r   r,  r-  r   r)  r   r8  RuntimeErrorwait_forr  get_mer   )r   HEARTBEAT_PROBE_DELAYPROBE_TIMEOUT	probe_errs       r   r3  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II 	@ 	@ 	@NNM	0)   44Y???????????????	@s   7C 
D%=D  D%c                   K   | j         r| j        dk    rd S | xj        dz  c_        d}d}| j        |k    rWt                              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        |           Y d }~d S d }~ww xY wd|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_conflictrN   r\   r  zD[%s] Telegram polling conflict (%d/%d), will retry in %ds. Error: %sFr!  z5[%s] Telegram polling resumed after conflict retry %dr   z&[%s] Telegram polling retry failed: %su   Another process is already polling this Telegram bot token (possibly OpenClaw or another Hermes instance). Hermes stopped Telegram polling after %d retries. Only one poller can run per token — stop the other process and restart with 'hermes start'.z[%s] %s Original error: %sr  z8[%s] Failed stopping Telegram polling after conflict: %sTr   )r%  fatal_error_coder   r   r)  r   r   r,  r-  r.  r   r*  r+  r  r/  r   r0  r   r1  r   r&  r'  )r   r   MAX_CONFLICT_RETRIESRETRY_DELAYr@  r=  
stop_errors          r   _handle_polling_conflictz(TelegramAdapter._handle_polling_conflictE  s      	D$9=X$X$XF 	$$)$$ '+???NNV	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         
 SUYU^`d`|}}}/0,   GT]^^^ 	/
 ## 	 	149guMMM97eTTT	}y /TY. /i',,......... 	} 	} 	}NNUW[W`blw{N||||||||	}&&(((((((((((sD   !AB* *
B76B7/A$E 
F!FF7H 
H=#H88H=r   
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   rP  rQ  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_topicr   r   r1  r   r   r   r   r)  )
r   r   r   rP  rQ  kwargstopicr   e
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
topic_namec                    	 ddl m}  |            dz  }|                                s#t                              d| j        |           dS ddl}t          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   |	                    di           	                    di           	                    d	i           	                    d
g           }	|	sdS d}
|	D ]}t          |	                    dd                    t          |          k    r7|	                    dg           D ]9}|	                    d          |k    r|	                    d          s	||d<   d}
 n:|
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   t+          ||           n5# t,          $ r( 	 t          j        |           n# t0          $ r Y nw xY w w xY wt                              d| j        ||           dS dS # t4          $ 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	platformstelegramr   r   Fr   topicsr   r   T.tmpz.config_)dirsuffixprefixwutf-8)encoding)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   r)  r   yamlopen	safe_loadr   r   tempfilemkstempr   parentr   fdopendumpflushfsyncfilenor4   BaseExceptionunlinkr   r1  r   )r   r   r\  r   r_  config_path_yamlfr   r   changed
chat_entrytfdtmp_pathrY  s                   r   _persist_dm_topic_thread_idz+TelegramAdapter._persist_dm_topic_thread_id  s   6	j888888)/++m;K%%'' []a]fhsttt    k3'' 21++1r2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 

;++Z$$Wb!!["%%	   G'  
z~~i3344GDD#"55  AuuV}}
22155;M;M2)2+"& '/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J J  B8J BJ BAJ %B;J !H4 8AHH4 HH4 H H4 3J 4
I&?II&
I!I& I!!I&&%J 
K#KK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   rd  z*[%s] Setting up %d DM topic(s) for chat %sr   :r   z4[%s] DM topic loaded from config: %s -> thread_id=%srP  rQ  )r   r   rP  rQ  z([%s] DM topic cached: %s -> thread_id=%su   📌 )r   r   r9   z2[%s] Could not send seed message to topic '%s': %s)r   r   r   r1  r   rh   r   r   r[  r  r   send_messager   r   )r   r  r   rd  
topic_confr\  	cache_keyexisting_thread_idrP  
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           
          !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        |           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|}t1          d|           }	|rw|	su|sst                              d! j        d                    |                     t3          dOi |d"d#t5          |          ii}
t3          dOi |d"d#t5          |          ii}n~|	rBt                              d$ j        |	           t3          dOi |d%|	i}
t3          dOi |d%|	i}n:|r t                              d& j                   t3          dOi |}
t3          dOi |}|                    |
                              |          }|                                 _         j        j         _          j        !                    tE          tF          j$        tF          j%         z   j&                              j        !                    tE          tF          j%         j'                              j        !                    tE          tF          j(        tS          tF          d'tF          j(                  z   j*                              j        !                    tE          tF          j+        tF          j,        z  tF          j-        z  tF          j.        z  tF          j/        j0        z  tF          j1        j0        z   j2                              j        !                    tg           j4                             	 d(d)l5m6}m7} n# tp          $ r tr          x}}Y nw xY wd*}tu          |          D ]}	  j        ;                                 d{V   ny# ||tr          f$ ri}||d+z
  k     rUty          d,|z  d-          }t          =                    d. j        |d+z   |||           t}          j?        |           d{V  n Y d}~d}~ww xY w j        @                                 d{V  t#          j        d/d                                          }|rt          t#          j        d0d1                    }t#          j        d2d                                          }|st          d3          d(d4lBmC}  ||          jD        pd5} j        jE        F                    d6||||t          jH        d78           d{V  d7 _I        t                              d9 j        ||           ntS           j         d:d          }t          |          r |d;           d{V  t}          jK                    !d<t          ddf! fd=}| _M         j        jE        N                    t          jH        d7|>           d{V  	 d(d?lOmP  d(d@lQmR}  |dAB          \  }} j         S                     fdC|D                        d{V  |r/t                              dD j        t          |          |           n:# t          $ r-}t          =                    dE j        |d7F           Y d}~nd}~ww xY w U                                  jI        rdGndH}t                              dI j        |           	  V                                 d{V  n:# t          $ r-}t          =                    dJ j        |d7F           Y d}~nd}~ww xY wd7S # t          $ r_} W                                 dK| } X                    dL|d7M           t                              dN j        |d7F           Y d}~dS d}~ww xY w)Pa_  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 tokenbase_urlbase_file_urlz'[%s] Using custom Telegram base_url: %sr   r  r5   c                     	 t          t          j        | t          |                              S # t          t
          f$ r |cY S w xY wra   )r   r   r   r   	TypeError
ValueErrorr   r  s     r   _env_intz)TelegramAdapter.connect.<locals>._env_intn  sP    #rys7||<<===!:. # # #"NNN#   .1 AAc                     	 t          t          j        | t          |                              S # t          t
          f$ r |cY S w xY wra   )r   r   r   r   r  r  r  s     r   
_env_floatz+TelegramAdapter.connect.<locals>._env_floatt  sP    # 4W!>!>???!:. # # #"NNN#r  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      rN   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.0T)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   create_taskrO  r   r   r)  r   r8  r   )r   loopr   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)telegram_menu_commandsd   )max_commandsc                 .    g | ]\  }} ||          S r   r   )rS   r   descr  s      r   rU   z+TelegramAdapter.connect.<locals>.<listcomp>-  s6     1 1 1/9tTJJtT**1 1 1r   zd[%s] Telegram menu: %d commands registered, %d hidden (over 100 limit). Use /commands for full list.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   )Yr7   r   r   r   r   token_acquire_platform_lockr   builderr   r   r  r  r1  r   r   r   r   r   rH   r   r   r2   ri   r,   r   r1   requestget_updates_requestbuildr   r  r   add_handlerTelegramMessageHandlerr   TEXTCOMMAND_handle_text_message_handle_commandLOCATIONr   _handle_location_messagePHOTOVIDEOAUDIOVOICEDocumentALLSticker_handle_media_messager   _handle_callback_queryr   r   r   r   r   ranger  r(  r)  r*  r+  r^   rC  urllib.parser  pathr,  start_webhookr   r0  r   r   get_running_loopr   r   r/  rc  r  hermes_cli.commandsr  set_my_commandsrh   _mark_connectedr  _release_platform_lockr&  )"r   r  custom_base_urlr  r  request_kwargsdisable_fallbackr   proxy_targets	proxy_urlr  r  r   r   _max_connect_attemptinit_errwaitr  webhook_portwebhook_secretr  webhook_pathr  r  r  menu_commandshidden_countrY  mode
topics_errr=  r  r  s"   `                               @@r   connectzTelegramAdapter.connectA  s      " 	LL^	   5{  	LL7CCC5y	../CT[EVXlmm u ")++11$+2CDDG"k/33J??O !**?;;!//K%))/?KK  =I  #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         //////FFFFFF /E.DRU.V.V.V+|i// 1 1 1 1=J1 1 1            KK~	3}#5#5|      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   !c+ R c+ "T+ *c+ +U>c+  Uc+ U97c+ 9W/AW*%c+ *W//F0c+  A8` c+ 
a##ac+ aAc+ b/ .c+ /
c&9#c!c+ !c&&c+ +
e5Aee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  r   r   r)  r   r  r   r  r   _mark_disconnectedr   r1  )r   pending_media_group_tasksrA  rY  s       r   
disconnectzTelegramAdapter.disconnectU  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reply_to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
        Fr  allTr   )r   )r   r  r  r  s       r   _should_thread_replyz$TelegramAdapter._should_thread_replyv  s@      	5"5==5U]]4!##r   contentc                 z
  K   | j         st          dd          S |r|                                st          dd          S 	 |                     |          }|                     || j        t                    }t          |          dk    rd	 |D             }g }|                     |          }	 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 ]R\  }}|                     ||          }|rt%          |          nd}|                     |          }d}t)          d          D ]}	 	  | j         j        d#t%          |          |t,          j        ||d|                                  d{V }n# t2          $ r}dt5          |                                          v s#dt5          |                                          v rnt8                              d| j        |           t?          |          } | j         j        d#t%          |          |d||d|                                  d{V }n Y d}~nd}~ww xY w n# |	$ r}|
rtA          ||
          r| !                    |          r+|)t8                              d| j        |           d}Y d}~tt5          |                                          }d|v r+|)t8                              d| j        |           d}Y d}~Ă |rtA          ||          r |dk     rFd|z  }t8                              d| j        |dz   ||           tE          j#        |           d{V  n Y d}~+d}~wt2          $ r}tI          |dd          }|#dt5          |                                          v r_|dk     rY|tK          |          nd}t8                              d| j        |dz   ||           tE          j#        |           d{V  Y d}~͂ d}~ww xY w|&                    t5          |j'                             Tt          d|r|d
         ndd|i          S # t2          $ r}t8          (                    d| j        |d           tS                      *                    d           }t5          |                                          }|rtA          ||          pd!|v }t          dt5          |          | "          cY d}~S d}~ww xY w)$z"Send a message to a Telegram chat.FNot connectedsuccessr   TNr  
message_id)len_fnrN   c                 :    g | ]}t          j        d d|          S )z \((\d+)/(\d+)\)$z \\(\1/\2\\)rA   )rS   chunks     r   rU   z(TelegramAdapter.send.<locals>.<listcomp>  s7        F/%HH  r   r   )r   )
BadRequest)r   r\   )r   r9   
parse_modereply_to_message_idr   parsemarkdownz<[%s] MarkdownV2 parse failed, falling back to plain text: %sz<[%s] Thread %s not found, retrying without message_thread_idzmessage to be replied not foundz8[%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 after      ?zI[%s] Telegram flood control on send (attempt %d/3), retrying in %.1fs: %smessage_ids)r  r  raw_responsez([%s] Failed to send Telegram message: %sr   	_TimedOutz	timed out)r  r   r   r   )+r   r'   rH   format_messagetruncate_messageMAX_MESSAGE_LENGTHr/   rh   r   r   r   r   r   r  r   AttributeErrorrj   r  r   r   r  r  r   MARKDOWN_V2r  r   r   r   r   r)  r   rD   r   r   r*  r+  r   r   rm   r  r   localsr   )r   r   r  r  r   	formattedchunksr  r   _NetErr_BadReqr  r   r  should_threadreply_to_ideffective_thread_idmsg_send_attemptmd_errorplain_chunksend_err	err_lowerr  r  rY  _toerr_str
is_timeouts                                r   sendzTelegramAdapter.send  s      y 	De?CCCC  	=gmmoo 	=dt<<<<J	U++G44I**429 +  F 6{{Q !'  
 K00::I"BBBBBBB " " "!"@@@@@@@   !@@@@@@@0 ! ! ! 			! &f-- Z8 Z85 $ 9 9(A F F/<Fc(mmm$&*&F&Fy&Q&Q#%*1XX S SMR&(>	(> )(+G%*+4+@4?2E) ) #'";";"="=) ) # # # # # #CC  ) & & &&#h--*=*=*?*???:QTU]Q^Q^QdQdQfQfCfCf &/mosox  {C  !D  !D  !D.9%.@.@,BDI,B -",/LL)4/38C6I-" -" '+&?&?&A&A-" -" '" '" '" '" '" '" !& !$& " (" (" ("
 # "z(G'D'D "#>>xHH 	)M`Ml !'$b$(I/B!" !" !" 7; 3 ((+H(;(;(=(=I@IMMR]Ri !'$^$(Ix!" !" !" /3 (! % "Hi)H)H "!(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-8B;q>>d+[9     	U 	U 	ULLCTYPQ\`Laaa ((,,{++C!ffllnnG4*Q"4"4O9OJe3q66^TTTTTTTTT	Us  A-R 1B8 7R 8C	R CR C R C&#R %C&&R *C1 0R 1DR DA*R 3AF;:J
;
JB5I?:J
?JJ
R 
QANR "A
N,R 2A!NR Q&BP<5R ;P<<QA	R 
T:BT5/T:5T:)finalizer  r3  c                  K   | j         st          dd          S 	 |                     |          }	 | j                             t	          |          t	          |          |t
          j                   d{V  n# t          $ r}dt          |          	                                v rt          d|          cY d}~S | j                             t	          |          t	          |          |	           d{V  Y d}~nd}~ww xY wt          d|          S # t          $ r6}t          |          	                                }d|v rt          d|          cY d}~S d
|v sd|v rt          || j        dz
            dz   }		 | j                             t	          |          t	          |          |		           d{V  n# t          $ r Y nw xY wt          d|          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t                              d| j        ||d           t          dt          |                    cY d}~S d}~ww xY w)z(Edit a previously sent Telegram message.Fr	  r
  )r   r  r9   r  Nznot modifiedTr  )r   r  r9   message_too_longztoo long   u   …r  r  r  z*[%s] Telegram flood control, waiting %.1fsg      @zflood_control:z+[%s] Edit retry failed after flood wait: %sz+[%s] Failed to edit Telegram message %s: %sr   )r   r'   r  edit_message_textr   r   r   r   r   r   r0   r  r   r   r)  r   r*  r+  r   )r   r   r  r  r3  r"  fmt_errrY  r0  	truncatedr  r  r@  s                r   edit_messagezTelegramAdapter.edit_message%  s      y 	De?CCCCJ	;++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i11LL":  2              	 dzBBBB 6	; 6	; 6	;!ffllnnG((!$:FFFFFFFFF "W,,
g0E0E6T4r9 	)55 #G#&z??& 6          
 !   D!$:FFFFFFFFF "!]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 LL=	     e3q66:::::::::m6	;s   D AA; :D ;
D3D 8D9D >=D ;D  DD M$5MM#M=G M
GMGM!M'AM?MM AJ3-M3
L=>L;L<M MLAMMM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_messager   r   r   r   r   )r   r   r  rY  s       r   r<  zTelegramAdapter.delete_message|  s       y 	5	)**Gz?? +          4 	 	 	LL?	:q   55555	s   <A 
B"A==Br_   promptsession_keyc           	        K   | j         st          dd          S 	 |rd| dnd}d| | }t          t          dd	
          t          dd
          gg          }|                     |          }	|                     |	          }
 | j         j        dt          |          |t          j	        ||
d| 
                                 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:nr   r9   r  reply_markupr   NTr  z"[%s] send_update_prompt failed: %sr   )r   r'   r   r
   r   r   r  r   r   MARKDOWNr  r   r  r   r   r)  r   )r   r   r=  r  r>  r   default_hintr9   keyboardr   r   r)  rY  s                r   send_update_promptz"TelegramAdapter.send_update_prompt  s      y 	De?CCCC	;7>F33333BLNNNND+(BSTTT(ARSSS-  H 00::I $ @ @ K K.	. G$-%"3  ++--       C ds3>7J7JKKKK 	; 	; 	;NN?ANNNe3q66:::::::::	;s   CC1 1
E;>D?9E?Edangerous commandcommanddescriptionc           	        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         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)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_counterrN   u   ✅ Allow Oncezea:once:rA  u   ✅ Sessionzea:session:u
   ✅ Alwaysz
ea:always:u   ❌ Denyzea:deny:r   r9   r  rD  r   Tr  z"[%s] send_exec_approval failed: %sr   )r   r'   rh   _htmlescaper   	itertoolshasattrcountrO  rl   r   r
   r   r   HTMLr  r   r  r   r   r  r   r   r)  r   )r   r   rJ  r>  rK  r   cmd_previewr9   r   rS  approval_idrG  rW  r   r)  rY  s                   r   send_exec_approvalz"TelegramAdapter.send_exec_approval  s      y 	De?CCCC1	;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 !% @ @ K K ,.?*+.	.8888888888C 1<D -ds3>7J7JKKKK 	; 	; 	;NN?ANNNe3q66:::::::::	;s   FF1 1
H;>G?9H?H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         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
  rM  NrN  u   ✅ Approve Oncezsc:once:rA  u   🔒 Always Approvez
sc:always:u
   ❌ Cancelz
sc:cancel:rP  r   Tr  z"[%s] send_slash_confirm failed: %sr   )r   r'   rh   r   r
   r   r   r   rE  r  r   r  r   r   r  r   r   r)  r   )r   r   rZ  r=  r>  r[  r   previewrG  r   rW  r   r)  rY  s                 r   send_slash_confirmz"TelegramAdapter.send_slash_confirm  s     
 y 	De?CCCC 	; "%W!5!5gg75D5>E;QG+();KbV`KbKbccc()>Ng[eNgNghhh
 )E^R\E^E^___-  H 00::Iw<<'0 (	& &
 ++--&F !% @ @ K K ,.?*+.	.8888888888C4?D%j1ds3>7J7JKKKK 	; 	; 	;NN?ANNNe3q66:::::::::	;s   DD0 0
F:>E>8F>F	providerscurrent_modelcurrent_providerc           
        K   | j         st          dd          S 	 ddlm} n# t          $ r d }Y nw xY w	 g |D ]}	|	                    dt          |	                    dg                               }
|	d	          d
|
 d}|	                    d          rd| }                    t          |d|	d                               fdt          dt                    d          D             }|                    t          dd          g           t          |          } ||          }d|pd d| d}|r|                    d          nd} | j         j        d t          |          |t          j        ||rt          |          ndd|                                  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 ra   r   slugs    r   rd  z4TelegramAdapter.send_model_picker.<locals>.get_label8      r   total_modelsmodelsr    (r@  
is_current   ✓ mp:rg  rA  c                 *    g | ]}||d z            S r    r   rS   r   buttonss     r   rU   z5TelegramAdapter.send_model_picker.<locals>.<listcomp>H  &    JJJ1GAAI&JJJr   r    
   ✗ Cancelmx+   ⚙ *Model Configuration*

Current model: `unknown`
Provider: 

Select a provider:r   NrC  )msg_idr_  r>  on_model_selectedr`  ra  Tr  z![%s] send_model_picker failed: %sr   )r   r'   hermes_cli.providersrd  r   r   rh   rm   r
   r  r   r  r   r   rE  r  r  r   r   r   r   r)  r   )r   r   r_  r`  ra  r>  r{  r   rd  prU  labelrowsrG  provider_labelr9   r   r)  rY  rr  s                      @r   send_model_pickerz!TelegramAdapter.send_model_picker#  s      y 	De?CCCC	6666666 	 	 	    	0	;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-l$OOOPQQQ+D11H&Y'788N&#0#=I& &+& & &  6>G[1114I.	. G$-%4="G#i...4  ++--       C .&*%6!.$46 6D$S\\2 ds3>7J7JKKKK 	; 	; 	;NN>	1MMMe3q66:::::::::	;s*   $ 44F=G6 6
I	 >I>I	I	r  rj  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).rN   r   /rO   &   N#   rN  mm:rA  c                 *    g | ]}||d z            S rp  r   rq  s     r   rU   z9TelegramAdapter._build_model_keyboard.<locals>.<listcomp>  s&    FFFqAE	"FFFr   r    u   ◀ Prevmg:zmx:noopu   Next ▶u   ◀ Backmbrt  ru  rk  u   –z of r@  r_   )
_MODEL_PAGE_SIZErh   maxr(  rj   rX   rm   r
   r  r   )r   rj  r  	page_sizetotaltotal_pagesr^   endpage_modelsr   model_idabs_idxshortr  nav	page_inforr  s                   @r   _build_model_keyboardz%TelegramAdapter._build_model_keyboardo  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          rI|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          ri	 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{V }n7# t          $ r*}t                               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|                    d$           d{V  | j                             |d           dS |d%k    rug |d
         D ]}|                    dt          |                    dg                               }|d          d&| d'}|                    d(          rd)| }                    t)          |d|d*          +                     fd,t+          dt                    d-          D             }|                    t)          d.d/+          g           t-          |          }	  ||d0                   }n# t          $ r |d0         }Y nw xY w|
                    d1|d2         pd3 d4| d5t          j        |           d{V  |                                 d{V  dS |d/k    rT| j                             |d           |
                    d6d7           d{V  |                                 d{V  dS |                                 d{V  dS )8zDHandle model picker inline keyboard callbacks (mp:/mm:/mb:/mx:/mg:).u$   Picker expired — use /model again.r=   Nr   rc  c                     | S ra   r   rf  s    r   rd  z@TelegramAdapter._handle_model_picker_callback.<locals>.get_label  rh  r   rn  r\   c              3   4   K   | ]}|d          k    |V  dS rg  Nr   rS   r}  provider_slugs     r   rb   z@TelegramAdapter._handle_model_picker_callback.<locals>.<genexpr>  1      MMq!F)}2L2L2L2L2L2LMMr   r_  zProvider not found.rj  selected_providerr   selected_provider_name
model_list
model_pageri  z
_u2    more available — type `/model <name>` directly_r_   u&   ⚙ *Model Configuration*

Provider: *r   z
Select a model:r9   r  rD  r  zInvalid page.c              3   4   K   | ]}|d          k    |V  dS r  r   r  s     r   rb   z@TelegramAdapter._handle_model_picker_callback.<locals>.<genexpr>  r  r   r  zInvalid selection.zInvalid model index.r{  zPicker expired.zModel picker switch failed: %szError switching model: zModel switched!r  rk  r@  rl  rm  rg  rA  c                 *    g | ]}||d z            S rp  r   rq  s     r   rU   zATelegramAdapter._handle_model_picker_callback.<locals>.<listcomp>)  rs  r   r    rt  ru  ra  rv  r`  rw  rx  ry  zModel selection cancelled.)r9   rD  )r   r   answerr|  rd  r   rV   rl   r  rh   r7  r   rE  r   r  r   r   r   poprm   r
   r  r   )r   queryr  r   staterd  providerrj  rG  r  pnamer  shownr   r  idxr  r  callbackresult_textexcr}  rU  r~  r  r  rr  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!! b	! 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))."'. .*3. .&+. . %-% *          ,,..         __U## 	!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))."'. .*3. .&+. . %-% *          ,,..         __U## ]	!$qrr(mm   ll(<l========= <44JQww#Z00ll(>l?????????!#H!II&92>>Myy!455H ll(9l:::::::::>$,HWh$N$NNNNNNN > > >=sCCC===>
--$(1!% .          
  	 	 	11(#'%) 2          
 !   D	 ,,$5,666666666 $(($77777T\\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-l$OOOPQQQ+D11H;!*51C+D!E!E ; ; ;!&'9!:; ))*',_'='J* *!/* * *
 %-% * 	 	 	 	 	 	 	 	 	 ,,..         T\\$(($777))1! *          ,,..          ,,..         s    A AAG( (&HHM' '&NN5Q	 	
Q= Q88Q=(R* *
S'5SS'
S!S' S!!S'&S'"X4 4Y	Y	updater   contextContextTypes.DEFAULT_TYPEc           	        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
d          }t          |          dk    r|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# t,          $ r Y nw xY w	 ddlm}  |||          }t2                              d ||||           n2# t,          $ r%}t2                              d!|           Y d}~nd}~ww xY wdS |                    d"          rY|	                    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# t,          $ r Y nw xY w	 dd(lm} |                    |||           d{V }|rw|j        rpt          |j        dd          }t          |j        j                  |t(          j        d)|                                  }|||d<    | j!        j"        d<i | d{V  n:# t,          $ r-}t2                              d*| j#        |d+,           Y d}~nd}~ww xY wdS |                    d-          sdS |	                    d
d          d         }t          t          |j        dd                    }|                     |||t          |          nd|	t          |	          nd|
          s|                    d.           d{V  dS |                    d/| d0           d{V  |d1k    rd2nd3}	 |                    d4| d5t(          j        d           d{V  n# t,          $ r Y nw xY w	 dd6l$m%}  |            }|d7z  }|&                    d8          } | '                    |           | (                    |           t2                              d9|t          |j        dd:                     dS # t,          $ r&}t2                              d;|           Y d}~dS d}~ww xY w)=z%Handle inline keyboard button clicks.Nr=  r   chattyper   
first_name)rn  r  r  ru  r  zea:r  r    r\   rN   zInvalid approval data.r=   idr_   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Resolvedz 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_confirm)r   r9   r  z&[%s] slash-confirm callback failed: %sTr   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_responsere  z/Telegram update prompt answered '%s' by user %srw  z1Failed to write update response from callback: %sr   ))callback_queryr  r   	from_userrV   r=  r   r   r  rX   rh   r   r  
IndexErrorr  r   r   r  r   r7  r   rE  r   tools.approvalr  r   r1  r   r   toolsr  resolver  r   r  r   rn  r_  with_suffix
write_textreplace)!r   r  r  r  r  query_messagequery_chat_id
query_chatquery_chat_typequery_thread_idquery_user_namer   partschoicerX  	caller_idr>  	label_mapuser_displayr~  r  rU  r  r[  _slash_confirm_modr  r   send_kwargsr  r_  homeresponse_pathtmps!                                    r   r  z&TelegramAdapter._handle_callback_queryK  s	      % 	EJ 	Fzy$77y$??]FD99
!*fd;;!-1DdKK!%/<FF ??<== 	49MKc%-/000tG O88gNNNNNNNNNF ??5!! ;	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 %99<99#,#5%) 2          
 !   DeGGGGGG44[&IIEKKe{FL    ! e e eLL!^`cdddddddde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 %99<99#,#5%) 2          
 !   DjIIIIII(:(B(B#Z) ) # # # # # #K # Du} D %,EM;NPT$U$U	'*5=+@'A'A$/*3*<7 7 #7799	7 %0?HK(;<4di4CC{CCCCCCCCC  j j jLL!I49VYdhLiiiiiiiij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	))=U===$-! *          
  	 	 	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   (D> >-E/.E/:-J( (
J54J590K* *
L4LL5-R# #
R0/R04BU 
V#VV(,Z 
Z"!Z"&B\, ,
]6]]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.))rV   )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   
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 rx| 	                    |          }	| j         
                    t          |          ||r
|dd	         nd|rt          |          nd|                     |	          
           d{V }
n|dv rx| 	                    |          }| j                             t          |          ||r
|dd	         nd|rt          |          n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"          $ rW}t$                              d| j        |d           t+                      
                    ||||           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rbrN   ).oggz.opusN   )r   voicer  r  r   ).mp3z.m4a)r   audior  r  r   )r   	file_pathr  r  r   Tr  zJ[%s] Failed to send Telegram voice/audio, falling back to base adapter: %sr   )r   r'   r   r  ro  r  rq  splitextr   r   
send_voicer   r   
send_audiosend_documentr   r  r   r   r   r   r   )r   r   r  r  r  r   rW  
audio_fileext_voice_threadr)  _audio_threadrY  r   s                r   r  zTelegramAdapter.send_voice  sK      y 	De?CCCC,	T7>>*-- l!%t7U7UV]_i7j7jkkkkj$'' :g&&z2215;;==+++$($<$<X$F$FM $	 4 4 #G(29 Ct=E,OCMMM4*.*J*J=*Y*Y !5 ! !      CC ,,,$($<$<X$F$FM $	 4 4 #G(29 Ct=E,OCMMM4*.*J*J=*Y*Y !5 ! !      CC "&!3!3 '", '!)!) "4 " "      3                      @ ds3>7J7JKKKK 	T 	T 	TLL\		     ++GZ(SSSSSSSSSSSSSS	TsP   AH !H 1EG H H GH  G!%H 
I(AI#I(#I(        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        |           |t1          |d          }|                    |           |                     |||                     |                     |||                     |s-	 |D ]'}	 |                                 # t          $ r Y $w xY wEt                              d| j        t#          |          |dz   t#          |                     | j                             t9          |          ||           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Y# |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://)r  )unquoter  c                 *    g | ]}||z            S r   r   )rS   r   CHUNKphotoss     r   rU   z8TelegramAdapter.send_multiple_images.<locals>.<listcomp>  s&    LLL!&1u9%LLLr   r     z.[%s] Skipping missing image in media group: %sr  )mediar  z5[%s] Sending media group of %d photo(s) (chunk %d/%d)rN   )r   r  r   zI[%s] send_media_group failed (chunk %d/%d), falling back to per-image: %sTr   )r   rc  r  r   r   r)  r   r   send_multiple_imagesrV   _is_animation_urlrm   r  r  r   r   r  rh   rj   r*  r+  r   r  ro  rq  closer1  send_media_groupr   )r   r   r  r   r  r  r  
animations	image_urlalt_text_unquote_thread
_thread_idr#  	chunk_idxr  r  opened_filesr  
local_pathfhrY  r  r  r   s                         @@r   r  z$TelegramAdapter.send_multiple_imagesS  s     $ 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55g>>
 LLLLLuQFU/K/KLLL )& 1 1 2	 2	IuQ9q==mK000000000!E&(L,+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++   i00LL&0 1          
  	 	 	_Iy1}c&kk1!     gg22UH+ 3              	 '  B



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



$   ]2	 2	s    
A;AA66A;1C'L6J33
K ?K A0L65O6
N* A N% O%N**O2O
OOPO43P4
P>P PP
image_pathc           
        K   | j         st          dd          S 	 t          j                            |          s%t          d|                     d|                    S |                     |          }t          |d          5 }| j                             t          |          ||r
|dd         nd|rt          |          nd| 
                    |                     d{V }	ddd           n# 1 swxY w Y   t          d	t          |	j                  
          S # t          $ rW}
t                              d| j        |
d	           t#                                          ||||           d{V cY d}
~
S d}
~
ww xY w)z5Send a local image file natively as a Telegram photo.Fr	  r
  Imager  Nr  r   photor  r  r   Tr  zJ[%s] Failed to send Telegram local image, falling back to base adapter: %sr   )r   r'   r   r  ro  r  r   rq  
send_photor   r   r   r  r   r   r   r   r   send_image_file)r   r   r  r  r  r   rW  r  
image_filer)  rY  r   s              r   r  zTelegramAdapter.send_image_file  s      y 	De?CCCC	Y7>>*-- l!%t7U7UV]_i7j7jkkkk..x88Gj$'' : I00LL$.5?GETENN49A(KHt&*&F&Fw&O&O 1                       ds3>7J7JKKKK 	Y 	Y 	YLL\		     00*gxXXXXXXXXXXXXXX	YsJ   AD# !%D# A#C5)D# 5C99D# <C9=%D# #
F-AE?9F?Fr  	file_namec                 B  K   | j         st          dd          S 	 t          j                            |          s%t          d|                     d|                    S |pt          j                            |          }|                     |          }	t          |d          5 }
| j         	                    t          |          |
||r
|dd         nd|rt          |          nd|                     |	                     d{V }ddd           n# 1 swxY w Y   t          d	t          |j                  
          S # t          $ rO}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  Nr  )r   documentfilenamer  r  r   Tr  [z] Failed to send document: )r   r'   r   r  ro  r  basenamer   rq  r  r   r   r   r  r   printr   r   )r   r   r  r  r  r  r   rW  display_namer  r  r)  rY  r   s                r   r  zTelegramAdapter.send_document  s      y 	De?CCCC	a7>>),, j!%t7U7UV\^g7h7hiiii$C(8(8(C(CL..x88Gi&& ! I33LL).5?GETENN49A(KHt&*&F&Fw&O&O 4                       ds3>7J7JKKKK 	a 	a 	a?di??A??@@@..w	7IW_``````````````	asK   AE !AE 'A$DE DE D%E 
FAFFF
video_pathc           
        K   | j         st          dd          S 	 t          j                            |          s%t          d|                     d|                    S |                     |          }t          |d          5 }| j                             t          |          ||r
|dd         nd|rt          |          nd| 
                    |                     d{V }	ddd           n# 1 swxY w Y   t          d	t          |	j                  
          S # t          $ rN}
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  Nr  )r   videor  r  r   Tr  r  z] Failed to send video: )r   r'   r   r  ro  r  r   rq  
send_videor   r   r   r  r   r  r   r   )r   r   r  r  r  r   rW  r  r  r)  rY  r   s              r   r"  zTelegramAdapter.send_video		  s      y 	De?CCCC	T7>>*-- l!%t7U7UV]_i7j7jkkkk..x88Gj$'' 1 I00LL.5?GETENN49A(KHt&*&F&Fw&O&O 1                       ds3>7J7JKKKK 	T 	T 	T<di<<<<===++GZ(SSSSSSSSSSSSSS	TsJ   AD# !%D# A#C5)D# 5C99D# <C9=%D# #
E;-AE60E;6E;r  c           
      p  K   | j         st          dd          S ddlm}  ||          sLt                              d| j                   t                                          |||||           d{V S 	 | 	                    |          }| j         
                    t          |          ||r
|dd	         nd|rt          |          n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         
                    t          |          ||r
|dd	         nd|rt          |          n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  Tr  z8[%s] URL-based send_photo failed, trying file upload: %sr   g      >@)timeoutz+[%s] File upload send_photo also failed: %s)r   r'   tools.url_safetyr$  r   r)  r   r   
send_imager   r  r   r   r   r  r   httpxAsyncClientr   raise_for_statusr  r   )r   r   r  r  r  r   r$  _photo_threadr)  rY  r(  clientresp
image_data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*	W 44X>>M	,,G*1;t5=$GCMMM4"&"B"B="Q"Q -        C ds3>7J7JKKKK 	W 	W 	WNNJ		    W ,,T,:: . . . . . . .f!'I!6!6666666D))+++!%J. . . . . . . . . . . . . . . . . . . . . . . . . . .
 !I00LL$.5?GETENN49A(KHt&*&F&F}&U&U 1         "$3s~;N;NOOOOOOOOO W W WAI!	     #WW//GXVVVVVVVVVVVVVVVVVVVW/	Wsu   :BD J5#J0 I#7F,I,
F6	6I9F6	:BIJ5
J-AJ(J-J0"J5(J--J00J5animation_urlc           	        K   | j         st          dd          S 	 |                     |          }| j                             t	          |          ||r
|dd         nd|rt	          |          nd|                     |                     d{V }t          dt          |j                            S # t          $ rK}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
  Nr  )r   	animationr  r  r   Tr  zA[%s] Failed to send Telegram animation, falling back to photo: %sr   )r   r'   r   send_animationr   r   r   r  r   r   r   r   r'  )	r   r   r0  r  r  r   _anim_threadr)  rY  s	            r   r3  zTelegramAdapter.send_animationi	  sb      y 	De?CCCC	T33H==L	00G'*1;t5=$GCMMM4"&"B"B<"P"P 1        C ds3>7J7JKKKK 	T 	T 	TLLS		     -(SSSSSSSSSSSSSS	Ts   BB6 6
D A D DDc                   K   | j         r	 |                     |          }|                     |          }	 | j                             t	          |          d|           d{V  dS # t
          $ rT}|F|                     |          r1| j                             t	          |          dd           d{V  n Y d}~dS d}~ww xY w# t
          $ r.}t                              d| j	        |d           Y d}~dS d}~ww xY wdS )zSend typing indicator.typing)r   actionr   Nz1[%s] Failed to send Telegram typing indicator: %sTr   )
r   r   r   send_chat_actionr   r   r   r   r   r   )r   r   r   _typing_threadr   rY  s         r   send_typingzTelegramAdapter.send_typing	  s     9 	!%!9!9(!C!C$($F$F~$V$V!)44 #G'*; 5           
 !   (49X9XYZ9[9[4"i88$'LL#+.2 9                    GI!	          '	 	s;   *C	 0A( (
C2A	C;C	 CC	 	
D#C<<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_chatr   r  r   GROUP
SUPERGROUPr>  CHANNELrZ  	full_namer   r?  r   r   r   r   r   )r   r   r  r   rY  s        r   get_chat_infozTelegramAdapter.get_chat_info	  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   rf   r5   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    rN   r   )rf   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\   rN   \\\`z\`rx   )r   rq   r  )mrawopen_endopeningbody_and_closebodyrK  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   rN  rO  )r   r  rQ  rK  s    r   <lambda>z0TelegramAdapter.format_message.<locals>.<lambda>	  s+    cc!''!**,,T6::;; r   c                     t          |                     d                    }|                     d                              dd                              dd          } d| d| d          S )	NrN   r    rN  rO  r@  z\)r  ]()r>   r   r  )rQ  displayurlrK  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 )NrN   \*\*(.+?)\*\*r@   r   )r   rH   rB   r<   r>   )rQ  innerrK  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+(.+)$)flagsra  c                 b     dt          |                     d                     d          S )Nr   rN   r>   r   rY  s    r   rZ  z0TelegramAdapter.format_message.<locals>.<lambda>
  /    cc9l1771::66999:: r   z\*([^*\n]+)\*c                 b     dt          |                     d                     d          S )N_rN   rf  rY  s    r   rZ  z0TelegramAdapter.format_message.<locals>.<lambda>
  rg  r   z	~~(.+?)~~c                 b     dt          |                     d                     d          S )N~rN   rf  rY  s    r   rZ  z0TelegramAdapter.format_message.<locals>.<lambda>&
  rg  r   z\|\|(.+?)\|\|c                 b     dt          |                     d                     d          S )N||rN   rf  rY  s    r   rZ  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 )NrN   r    rc   rm   )r   rV   rW   r>   )rQ  rh  r  rK  s      r   _convert_blockquotez;TelegramAdapter.format_message.<locals>._convert_blockquote4
  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    rN   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   rN   rN  (]r@  z](httpr\  i  rO   )r^   r   r  r  )rQ  _segschbeforedepthr   s          r   	_esc_barez1TelegramAdapter.format_message.<locals>._esc_bareW
  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   rB   r<   	MULTILINEr>   reversedr  keysr  rX   rj   rm   ri   )r   r  r9   rW  r_  rc  rq  r  _code_split_safe_parts_idxru  rz  rK  rI  rJ  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_mentionNr  TELEGRAM_REQUIRE_MENTIONr  )	r   r   r   r   r   r   rI   r   r   r   s     r   _telegram_require_mentionz)TelegramAdapter._telegram_require_mentionw
  s    [&**+<==
!*c** H!''))-GGG
###y3W==CCEEIcccr   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   rH   rS   parts     r   r   z@TelegramAdapter._telegram_free_response_chats.<locals>.<setcomp>
  s=    KKK$T9J9JKCIIOO%%KKKr   c                 ^    h | ]*}|                                 |                                 +S r   rQ   r  s     r   r   z@TelegramAdapter._telegram_free_response_chats.<locals>.<setcomp>
  s-    MMM

M

MMMr   r   )	r   r   r   r   r   r   r  r   rX   )r   rR  s     r   _telegram_free_response_chatsz-TelegramAdapter._telegram_free_response_chats
  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|}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   r   r  r   rX   setrH   r5  r   r  r  r   r)  r   )r   rR  r  ignoredrf   r9   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   rQ   r  s     r   rU   z=TelegramAdapter._compile_mention_patterns.<locals>.<listcomp>
  s-    XXXt4::<<XdjjllXXXr   c                 ^    g | ]*}|                                 |                                 +S r   rQ   r  s     r   rU   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   rH   jsonloadsr   
splitlinesrX   r   r   r  r   r)  r   r  r   rm   rB   compile
IGNORECASEr   r1  rh   )r   patternsrR  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_   .rO   )r   r   )r   r   rX   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   r  rI   )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           	         | 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 dS )NFr?  r_   @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 )Nr9   r_   entitiesr  caption_entities)r   )r=  s   r   _iter_sourcesz<TelegramAdapter._message_mentions_bot.<locals>._iter_sources
  sx      '64006BUY8Z8Z8`^`````'9d339r77L^`d;e;e;kikkkkkkkr   r  r  rO   mentionoffsetlengthr   Ttext_mentionuserbot_command)	r   r   ry   r   r   rX   r   rH   find)r   r=  bot_usernamebot_idexpectedr  source_textr  entityentity_typer  r  r  command_textat_indexs    `             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 ur   c                     | j         sdS t          |dd           t          |dd           fD ](}|s| j         D ]}|                    |          r  dS )dS )NFr9   r  T)r   r   search)r   r=  	candidater  s       r   !_message_matches_mention_patternsz1TelegramAdapter._message_matches_mention_patterns  s    % 	5!'6488''9VZ:[:[\ 	  	 I 1    >>),,  444   ur   r9   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   rB   rR  r?  r<   rH   )r   r9   r?  rC   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   
is_commandr  c          	      L   |                      |          sdS t          |dd          }|_	 t          |          |                                 v rdS n8# t          t
          f$ r$ t                              d| j        |           Y nw xY wt          t          t          |dd          dd                    | 
                                v rdS |                                 sdS |                     |          rdS |                     |          rdS |                     |          S )	u+  Apply Telegram group trigger rules.

        DMs remain unrestricted. Group/supergroup messages are accepted when:
        - 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 ``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`.
        Tr   NFz8[%s] Ignoring non-numeric Telegram message_thread_id: %rr  r  r_   )r  r   r   r  r  r  r   r)  r   r   r  r  r  r  r  )r   r=  r  r   s       r   _should_process_messagez'TelegramAdapter._should_process_message  sJ   " ""7++ 	4G%8$??	 qy>>T%C%C%E%EEE 5 Fz* q q qY[_[dfopppppqwww55tR@@AATEgEgEiEiii4--// 	4  )) 	4%%g.. 	455g>>>s   #A 2BBc                 ,  K   |j         r|j         j        sdS |                     |j                   sdS |                     |j         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.
        N	update_id)	r=  r9   r  _build_message_eventr%   r  r  r  _enqueue_text_eventr   r  r  events       r   r  z$TelegramAdapter._handle_text_message<  s       ~ 	V^%8 	F++FN;; 	F))&.+:JV\Vf)gg11%*==
  '''''r   c                    K   |j         r|j         j        sdS |                     |j         d          sdS |                     |j         t          j        |j                  }|                     |           d{V  dS )z!Handle incoming command messages.NTr  r  )r=  r9   r  r  r%   r  r  handle_messager  s       r   r  zTelegramAdapter._handle_commandL  s      ~ 	V^%8 	F++FNt+LL 	F))&.+:MY_Yi)jj!!%(((((((((((r   c                 R  K   |j         sdS |                     |j                   sdS |j         }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.Nvenuelocationlatitude	longitudez![The user shared a location pin.]rZ  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=  r  r   rm   r  r%   r  r  ri   r9   r  )r   r  r  r)  r  r  latlonr  rZ  r  r  s               r   r  z(TelegramAdapter._handle_location_messageV  s     ~ 	F++FN;; 	Fn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%%
!!%(((((((((((r   r  c                     ddl m}  ||j        | j        j                            dd          | j        j                            dd                    S )z-Session-scoped key for text message batching.r   build_session_keygroup_sessions_per_userTthread_sessions_per_userFr  r  )r   r  r   r   r   r   )r   r  r  s      r   _text_batch_keyzTelegramAdapter._text_batch_key  sg    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   rh   r9   _last_chunk_len
media_urlsrk   media_typesr   r  r  r*  r  _flush_text_batch)r   r  r  existing	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}|| j        k    r| 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   Nz,[Telegram] Flushing text batch %s (%d chars)r_   )r*  current_taskr   r   r   _SPLIT_THRESHOLDr   r   r+  r  r   r   r1  rh   r9   r  )r   r  r  pendinglast_lenr>  r  s          r   r  z!TelegramAdapter._flush_text_batch  s      +--	> 044S99GAHOww(91===aH4000<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   B E AE :Fr)  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  r   r   r   r   r   )r   r  r)  r  r>  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   r  r   r   r   r1  rh   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  rk   r  r9   _merge_captionr   r  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dS |j         }|j        rt          j        }np|j        rt          j        }n\|j        rt          j        }nH|j	        rt          j
        }n4|j        rt          j        }n |j        rt          j        }nt          j        }|                     |||j                  }|j        r|                     |j                  |_        |j        r9|                     ||           d{V  |                     |           d{V  dS |j        r~	 |j        d         }|                                 d{V }|                                 d{V }d}	|j        r5dD ]2}
|j                                                            |
          r|
}	 n3t7          t9          |          |	          }|g|_        d|	                    d           g|_        t@          !                    d	|           tE          |d
d          }|r*| #                    tI          |          |           d{V  n,| %                    ||          }| &                    ||           dS # tN          $ r'}t@          (                    d|d           Y d}~nd}~ww xY w|j        r	 |j                                         d{V }|                                 d{V }tS          t9          |          d          }|g|_        dg|_        t@          !                    d|           n# tN          $ r(}t@          (                    d|d           Y d}~nd}~ww xY w|j	        r	 |j	                                         d{V }|                                 d{V }tS          t9          |          d          }|g|_        dg|_        t@          !                    d|           n.# tN          $ r(}t@          (                    d|d           Y d}~nd}~ww xY w|j        r	 |j                                         d{V }|                                 d{V }d}	tE          |dd          r:tT          D ]2}
|j                                                            |
          r|
}	 n3tW          t9          |          |	          }|g|_        tU          j,        |	d          g|_        t@          !                    d|           n# tN          $ r(}t@          (                    d|d           Y d}~nd}~ww xY w|j        r|j        }	 d}	|j-        pd}|r6t\          j/        0                    |          \  }}	|	                                }	|	s?|j1        r8d te          j3                    D             }|,                    |j1        d          }	|	s?|j1        r8d tU          j3                    D             }|,                    |j1        d          }	|	tT          v r|                                 d{V }|                                 d{V }tW          t9          |          |	          }|g|_        tT          |	         g|_        t          j        |_4        t@          !                    d|           |                     |           d{V  dS |	td          vr|d5                    tm          te          j7                                        }d |	pd! d"| |_        t@          !                    d#|	pd!           |                     |           d{V  dS d$}|j8        r|j8        |k    rDd%|_        t@          !                    d&|j8                   |                     |           d{V  dS |                                 d{V }|                                 d{V }t9          |          }ts          ||pd'|	           }td          |	         }|g|_        |g|_        t@          !                    d(|           d)}|	d*v rtu          |          |k    r	 |;                    d+          }|pd'|	 }ty          j=        d,d-|          }d.| d/| }|j        r| d0|j         |_        n||_        n,# t|          $ r t@          (                    d1d           Y nw xY wn4# tN          $ r'}t@          (                    d2|d           Y d}~nd}~ww xY wtE          |d
d          }|r+| #                    tI          |          |           d{V  dS |                     |           d{V  dS )3zBHandle incoming media messages, downloading images to local cache.Nr  rO   .jpg)z.png.webpz.gifz.jpegr  r  zimage/r  z"[Telegram] Cached user photo at %sr  z$[Telegram] Failed to cache photo: %sTr   r  z	audio/oggz"[Telegram] Cached user voice at %sz$[Telegram] Failed to cache voice: %sr  z	audio/mp3z"[Telegram] Cached user audio at %sz$[Telegram] Failed to cache audio: %sz.mp4r  z	video/mp4z"[Telegram] Cached user video at %sz$[Telegram] Failed to cache video: %sr_   c                     i | ]\  }}||	S r   r   rS   kr   s      r   
<dictcomp>z9TelegramAdapter._handle_media_message.<locals>.<dictcomp>n  s    "U"U"UDAq1a"U"U"Ur   c                     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 %sr  zUnsupported document type 'rw  z'. Supported types: z([Telegram] Unsupported document type: %si  @zLThe document is too large or its size could not be verified. Maximum: 20 MB.z'[Telegram] Document too large: %s bytesr  z%[Telegram] Cached user document at %si  )z.mdz.txtrj  z	[^\w.\- ]ri  z[Content of z]:
rg   zJ[Telegram] Could not decode text file as UTF-8, skipping content injectionz'[Telegram] Failed to cache document: %s)?r=  r  stickerr%   STICKERr  r  r!  r  r  r  r  r  r  DOCUMENTr  r  r  r  r9   _handle_stickerr  get_filedownload_as_bytearrayr  r   rW   r(   bytesr  ry   r  r   r1  r   _queue_media_group_eventr   r  r  r   r)  r)   r-   r*   r   r  r   r  r  	mime_typer.   itemsmessage_typeri   sortedr}  	file_sizer+   rh   decoderB   r<   UnicodeDecodeError)r   r  r  r)  msg_typer  r  file_objimage_bytesr  r  cached_pathr  r  rY  audio_bytesvideo_bytesdocoriginal_filenameri  mime_to_extvideo_mime_to_extsupported_listMAX_DOC_BYTES	doc_bytes	raw_bytesr  MAX_TEXT_INJECT_BYTEStext_contentr  	injections                                  r   r  z%TelegramAdapter._handle_media_message  s     ~ 	F++FN;; 	Fn ; 	,"*HHY 	,"(HHY 		,"(HHY 	,"(HHY 	,"(HH\ 	,"+HH"+H))#x6CS)TT ; 	C55ckBBEJ ; 	&&sE222222222%%e,,,,,,,,,F 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!$!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 n	\Y!$!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 c	\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 \ Q	\,CO\$'M$7R!$ &W--.?@@FAs))++C  =s} ="U"U4L4R4T4T"U"U"UK%//#-<<C Cs} C(X(X:O:U:W:W(X(X(X%+//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 !1} (E(E* J KK I3=YYY--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   ?EJ 
J9J44J9BM 
M:M55M:BP	 	
P;P66P;C"T+ +
U5UU0Fd 4Bd 9Ad B)d ;Ac d &c?<d >c??d 
d4d//d4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  rk   r  r9   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ra   )r*  r+  MEDIA_GROUP_WAIT_SECONDSr   r  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_   NrK  emojiset_namez [Telegram] Sticker cache hit: %sr  r  z"[Telegram] Analyzing sticker at %s)vision_analyze_tool)r  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_videor9   file_unique_idr   r   r1  r  r  r(   r  tools.vision_toolsr,  r  r  r   r)  )r   r)  r  r%  r&  r'  r(  r)  r  r*  r+  cachedr  r  r  r,  result_jsonresultrK  rY  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                 f   	 ddl m}  |            dz  }|                                sdS ddl}t	          |d          5 }|                    |          pi }ddd           n# 1 swxY w Y   |                    di                               di                               di                               d	g           }|sdS || _        |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`  Nra  rb  rc  r   r   r   rd  r   r   r  z8[%s] Hot-loaded DM topic from config: %s -> thread_id=%sz/[%s] Failed to reload dm_topics from config: %s)rn  r_  ro  rp  rq  rr  r   r   r   r   r   r1  r   r   r   )r   r_  r}  r~  r  r   r   r  cidr  tidr   r  rY  s                 r   _reload_dm_topics_from_configz-TelegramAdapter._reload_dm_topics_from_config!  sO   %	Z888888)/++m;K%%''     k3'' 21++1r2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 

;++Z$$Wb!!["%%	    &/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sG   'E: E: A#E: #A''E: *A'+AE: B4E: :
F0!F++F0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  rN   r   rd  r   )	r   r   r  rV   rX   r   r   r   r:  )	r   r   r   thread_id_intr  
cached_tidr\  r  r  s	            r   _get_dm_topic_infoz"TelegramAdapter._get_dm_topic_infoN  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   r   r   r1  r   )r   r   r   r\  r  s        r   _cache_dm_topic_from_messagez,TelegramAdapter._cache_dm_topic_from_messageu  sh    ----	DO++),YDOI&KKG	9i     ,+r   r  r  c                    |j         }|j        }d}|j        t          j        t          j        fv rd}n|j        t          j        k    rd}|j        }|t          |          nd}|dk    r|t          |dd          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k    rt          |j                  nd|r|j        nt          |d          r|dk    r|j        nd||	          }d}d}|j        r3t          |j        j                  }|j        j        p|j        j        pd}ddlm} t          |j                  } || j        j        |p||r|nd          }t;          |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   r=  Nr>  Fr   skillforum_topic_createdgroup_topicsr   r_   rd  r   rD  )r   	chat_namer   r   r   r   
chat_topicr   )resolve_channel_prompt)r9   r	  r   raw_messager  platform_update_idr  reply_to_text
auto_skillchannel_prompt	timestamp)r  r  r  r   rA  rB  rC  r   r   r   r   r>  r  r   rT  rC  r   r@  r   r   build_sourcerZ  rD  r  r  r9   r  gateway.platforms.baserG  r$   date)r   r=  r  r  r  r  r   thread_id_rawthread_id_strrF  topic_skill
topic_infocreated_namegroup_topics_configr  rX  r9  r   r'  rJ  rG  _chat_id_str_channel_prompts                          r   r  z$TelegramAdapter._build_message_event  s    |  	9)<===IIY(***!I  1.;.GM***TM$9gdJX]>^>^$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[$([CLLLyD?P?Ps47|||VZ(,  AdnnWTS^E_E_3dmqududu4>>{#! # 
 
 # 	fg6ABBK#49eW=U=]eaeM 	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_reactionr   r   r   r   r   )r   r   r  r*  rY  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   |                                  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   👀)r[  r   r   r_  )r   r  r   r  s       r   on_processing_startz#TelegramAdapter.on_processing_start  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                   K   |                                  sdS t          |j        dd          }t          |dd          }|rC|rC|t          j        k    r5|                     |||t          j        k    rdnd           d{V  dS dS dS 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.
        Nr   r  u   👍u   👎)r[  r   r   r&   	CANCELLEDr_  SUCCESS)r   r  rb  r   r  s        r   on_processing_completez&TelegramAdapter.on_processing_complete   s       &&(( 	F%,	488UL$77
 	z 	g1B1L&L&L$$ '+<+D D D,          	 	 	 	&L&Lr   )F)r5   N)NN)r_   r_   N)rI  Nra   )r  r   r  r  r5   N)NNN)Nr  )NNNN)lr   r   r   __doc__r  r  r"  r   r"   r   r   r   rI   r   classmethodr   r   r   r   r   r   staticmethodr   r   r  r   r   r   r   r  r  r8  r3  rO  r[  r  r  r  r  r  r'   r2  r:  r<  rH  rY  r^  r  r  tupler  r  r  r  r  r   r   r  r  r  r"  r'  r3  r:  rE  r  r  r  r  r  rB   Patternr   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[  r_  ra  r&   rf  __classcell__)r   s   @r   r   r      s          ""#7~ #7 #7 #7 #7 #7 #7R "&#'#'#'.G .G .G.G #	.G
 C=.G C=.G C=.G 
.G .G .G .G` A8DcN+C AQT A A A [A HSM hsm    [
 hsm QT    [
 8) 8 8 8 8 \8ctCy c c c c 
I 
$ 
 
 
 \
 * *t * * * \* c D T    2d38n 2 2 2 2) ) ) )VNG NGt NG NG NG NG`*@ *@ *@ *@X9)I 9)$ 9) 9) 9) 9)~ %).21 11 1 SM	1
 'sm1 
#1 1 1 1f8j3 8jC 8jTW 8j\` 8j 8j 8j 8jtR R R RhRt R R R RhB B B BB$Xc] $ $QU $ $ $ $0 #'-1YU YUYU YU 3-	YU
 4S>*YU 
YU YU YU YUB U; U; U;U; U; 	U; U; 
U; U; U; U;nC S T    4 9;-1"; ";";$'";25";"; 4S>*"; 
	"; "; "; ";L /-1>; >;>;%(>;7:>;>; 4S>*>; 
	>; >; >; >;D ?C(; (;(;#&(;14(;CF(;(;#+DcN#;(; 
(; (; (; (;d .2H; H;H; H; 	H;
 H; H; 4S>*H; 
H; H; H; H;T '5D '5 '5 '5 '5 '5 '5Rq!q!),q!	q! q! q! q!f|S|S)D|S	|S |S |S |S|s # #    & "&"&-19T 9T9T 9T #	9T
 3-9T 4S>*9T 
9T 9T 9T 9T 9T 9T~ .2 m mm Um 4S>*	m
 m 
m m m m m mf "&"&-1"Y "Y"Y "Y #	"Y
 3-"Y 4S>*"Y 
"Y "Y "Y "Y "Y "YP "&#'"&-1!a !a!a !a #	!a
 C=!a 3-!a 4S>*!a 
!a !a !a !a !a !aN "&"&-1T TT T #	T
 3-T 4S>*T 
T T T T T TF "&"&-1?W ?W?W ?W #	?W
 3-?W 4S>*?W 
?W ?W ?W ?W ?W ?WJ "&"&-1T TT T #	T
 3-T 4S>*T 
T T T T@  c3h8P \`    < I3  I4S>  I  I  I  IDjc jc j j j j\d4 d d d dNs3x N N N N3s8    *$4
+; $ $ $ $L4g 4$ 4 4 4 4f fD f f f f4W 4 4 4 4 4l	 	T 	 	 	 	HSM hsm     OT "? "? "?w "?t "?X\ "? "? "? "?H( (,B[ (`d ( ( ( ( )F )\=V )[_ ) ) ) )#)V #)lF_ #)dh #) #) #) #)R
\ 
c 
 
 
 

 
$ 
 
 
 
@>3 >4 > > > >B,l , ,S , , , ,E# E$ E E E Emc m, m4 m m m m"E)& E)<C\ E)ae E) E) E) E)N
S 
 
Z^ 
 
 
 
2	>S 	>T 	> 	> 	> 	>? ? ?D ? ? ? ?B+Z +Z +Z +ZZ%# %(3- %HUYZ]_bZbUcLd % % % %NC C UX ]a     $(	a
 a
a
 a
 C=	a

 
a
 a
 a
 a
J\D \ \ \ \3 C  PT    H| H H H H H, IZ _c        r   r   )Wrg  r*  r  loggingr   rs  htmlrQ  rB   r6  r   r   r   r   	getLoggerr   r   rc  r   r   r	   r
   r   r   r   telegram.extr   r   r   r   r  r   r   telegram.constantsr   r   telegram.requestr   r7   r   syspathlibr   _Pathr  insertr   __file__r  parentsgateway.configr!   r"   rO  r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   "gateway.platforms.telegram_networkr1   r2   r3   utilsr4   rI   r8   r  r;   r>   rD   rz   rL   r  rY   ru   r   r   r   r   r   <module>r|     s       				      				 , , , , , , , , , , , ,		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                                         
 !          T     "*<==.s .s . . . .
c c    @ !bj7  
. . . . . .:C :DI : : : :&$s) & & & & &42 2 2 2 2 2jb4 b4 b4 b4 b4) b4 b4 b4 b4 b4s5   B  A B  AB  A#B   2B54B5