
    dhw                         S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJrJ	r	J
r
  S SKJr  S SKJr  S SKJrJrJr  \R&                  " \5      r " S S\5      rg)	    N)Path)IteratorListUnion)BaseChatLoader)ChatSession)	AIMessageBaseMessageHumanMessagec                       \ rS rSrSrS\\\4   4S jr\	S\S\
4S j5       r\	S\S\
4S j5       r\	S\S\\   4S	 j5       rS\\
   4S
 jrSrg)TelegramChatLoader   a  Load `telegram` conversations to LangChain chat messages.

To export, use the Telegram Desktop app from
https://desktop.telegram.org/, select a conversation, click the three dots
in the top right corner, and select "Export chat history". Then select
"Machine-readable JSON" (preferred) to export. Note: the 'lite' versions of
the desktop app (like "Telegram for MacOS") do not support exporting chat
history.
pathc                 \    [        U[        5      (       a  Xl        g[        U5      U l        g)zInitialize the TelegramChatLoader.

Args:
    path (Union[str, Path]): Path to the exported Telegram chat zip,
         directory, json, or HTML file.
N)
isinstancestrr   )selfr   s     a/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/chat_loaders/telegram.py__init__TelegramChatLoader.__init__   s     'tS11D	s4y	    	file_pathreturnc           	      @    SSK Jn  [        U SSS9 nU" US5      nSSS5        / nSnWR	                  S	5       H  nUR                  S
5      S   nUR                  S5      nUc  Uc  [        R                  S5        ME  Uc  Un	OUR                  R                  5       n	UR                  S5      R                  R                  5       n
UR                  [        U
U	SU0/S.S95        U	nM     [        US9$ ! [         a    [        S5      ef = f! , (       d  f       N= f)zLoad a single chat session from an HTML file.

Args:
    file_path (str): Path to the HTML file.

Returns:
    ChatSession: The loaded chat session.
r   )BeautifulSoupzPlease install the 'beautifulsoup4' package to load Telegram HTML files. You can do this by running'pip install beautifulsoup4' in your terminal.rutf-8encodingzhtml.parserNz.message.defaultz.pull_right.date.detailstitlez
.from_namezfrom_name not found in messagez.textmessage_timesendereventscontentadditional_kwargsmessages)bs4r   ImportErroropenselect
select_oneloggerdebugtextstripappendr   r   )r   r   filesoupresultsprevious_sendermessage	timestampfrom_name_element	from_namer1   s              r   _load_single_chat_session_html1TelegramChatLoader._load_single_chat_session_html'   s7   	) )S73t }5D 4 9;{{#56G**+EFwOI ' 2 2< @ (_-D=>"*+	-2288:	%%g.3399;DNN "+$2I#>"?' (O) 7, G,,C  	A 	 43s   C6 
D6D
Dc           	      x   [        U SSS9 n[        R                  " U5      nSSS5        WR                  S/ 5      n/ nU H]  nUR                  SS5      nUR                  SS5      nUR                  S	S5      nUc  S
nUR	                  [        UUSU0/S.S95        M_     [        US9$ ! , (       d  f       N= f)zLoad a single chat session from a JSON file.

Args:
    file_path (str): Path to the JSON file.

Returns:
    ChatSession: The loaded chat session.
r   r   r   Nr)   r1    datefromzDeleted Accountr!   r"   r%   r(   )r,   jsonloadgetr3   r   r   )	r   r4   datar)   r6   r8   r1   r9   r;   s	            r   _load_single_chat_session_json1TelegramChatLoader._load_single_chat_session_jsonV   s     )S73t99T?D 4 88J+%'G;;vr*DFB/IFB/I -	NN "+$2I#>"?'  " G,,- 43s   B++
B9c              #     #    [         R                  R                  U 5      (       a  U R                  S5      (       a  U v   g[         R                  R	                  U 5      (       ac  [         R
                  " U 5       HH  u  pnU H<  nUR                  S5      (       d  M  [         R                  R                  X5      v   M>     MJ     g[        R                  " U 5      (       a~  [        R                  " U 5       nUR                  5        HJ  nUR                  S5      (       d  M  [        R                  " 5        nUR                  XFS9v   SSS5        ML     SSS5        gg! , (       d  f       Mg  = f! , (       d  f       g= f7f)zIterate over files in a directory or zip file.

Args:
    path (str): Path to the directory or zip file.

Yields:
    str: Path to each file.
).html.jsonr   N)osr   isfileendswithisdirwalkjoinzipfile
is_zipfileZipFilenamelisttempfileTemporaryDirectoryextract)r   root_filesr4   zip_filetemp_dirs          r   _iterate_files!TelegramChatLoader._iterate_filesx   s     77>>$DMM2D$E$EJWW]]4  "$''$-!D}}%788 ggll466 " #0 %%&($--/D}}%788%88:h"*"2"24"2"GG ;: 0 '& & ;: '&sC   BFAF:(E5&E5;E#E5
F#
E2-E55
F?Fc              #      #    U R                  U R                  5       HY  nUR                  S5      (       a  U R                  U5      v   M.  UR                  S5      (       d  MF  U R	                  U5      v   M[     g7f)zLazy load the messages from the chat file and yield them
in as chat sessions.

Yields:
    ChatSession: The loaded chat session.
rI   rJ   N)r^   r   rN   r<   rF   )r   r   s     r   	lazy_loadTelegramChatLoader.lazy_load   sd      ,,TYY7I!!'**99)DD##G,,99)DD	 8s   AA<$A<rK   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   staticmethodr   r<   rF   r   r^   ra   __static_attributes__ r   r   r   r      s    
ACI
A ,-# ,-+ ,- ,-\ -# -+ - -B HS HXc] H H.E8K0 Er   r   )rB   loggingrL   rV   rR   pathlibr   typingr   r   r   langchain_core.chat_loadersr   langchain_core.chat_sessionsr   langchain_core.messagesr	   r
   r   	getLoggerrc   r/   r   rj   r   r   <module>rr      sI      	    ( ( 6 4 H H			8	$KE KEr   