
    Ng'                         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 d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZmZ  ej        e          Z G d de          ZdS )	    N)Path)IteratorListUnion)BaseChatLoader)ChatSession)	AIMessageBaseMessageHumanMessagec                       e Zd ZdZdeeef         fdZedede	fd            Z
edede	fd            Zededee         fd            Zdee	         fd	Zd
S )TelegramChatLoadera  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                 \    t          |t                    r|nt          |          | _        dS )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     e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/chat_loaders/telegram.py__init__zTelegramChatLoader.__init__   s(     'tS11@DDs4yy			    	file_pathreturnc           	         	 ddl m} n# t          $ r t          d          w xY wt          | dd          5 } ||d          }ddd           n# 1 swxY w Y   g }d}|                    d	          D ]}|                    d
          d         }|                    d          }||t                              d           Q||}	n|j        	                                }	|                    d          j        	                                }
|
                    t          |
|	d|igd                     |	}t          |          S )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_namer/   s              r   _load_single_chat_session_htmlz1TelegramChatLoader._load_single_chat_session_html'   s   	))))))) 	 	 	A  	 )S7333 	6t =}55D	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 9;{{#566 	( 	(G**+EFFwOI ' 2 2< @ @ (_-D=>>>"*+		-288::	%%g..399;;DNN "+$2I#>"?' '     (OOG,,,,s   	 #AAAc           	         t          | dd          5 }t          j        |          }ddd           n# 1 swxY w Y   |                    dg           }g }|D ]n}|                    dd          }|                    dd          }|                    d	d          }|                    t          ||d
|igd                     ot          |          S )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'   r/    datefromr   r    r#   r&   )r*   jsonloadgetr1   r   r   )	r   r2   datar'   r4   r6   r/   r7   r9   s	            r   _load_single_chat_session_jsonz1TelegramChatLoader._load_single_chat_session_jsonV   s5    )S7333 	#t9T??D	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 88J++%' 	 	G;;vr**DFB//IFB//INN "+$2I#>"?' '      G,,,,s   488c              #     K   t           j                            |           r|                     d          r| V  dS t           j                            |           rYt          j        |           D ]B\  }}}|D ]9}|                    d          r"t           j                            ||          V  :CdS t          j        |           rt          j	        |           5 }|
                                D ][}|                    d          rDt          j                    5 }|                    ||          V  ddd           n# 1 swxY w Y   \	 ddd           dS # 1 swxY w Y   dS dS )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_filesr2   zip_filetemp_dirs          r   _iterate_filesz!TelegramChatLoader._iterate_filesv   s5      7>>$ 	HDMM2D$E$E 	HJJJJJW]]4   
	H"$'$-- 7 7a! 7 7D}}%788 7 gll46666677 7 %% 	H&& H($--// H HD}}%788 H%8:: Hh"*"2"24h"2"G"GGGGH H H H H H H H H H H H H H HHH H H H H H H H H H H H H H H H H H	H 	Hs6   ?EE6EEE	E
EE #E c              #      K   |                      | j                  D ][}|                    d          r|                     |          V  /|                    d          r|                     |          V  \dS )zLazy load the messages from the chat file and yield them
        in as chat sessions.

        Yields:
            ChatSession: The loaded chat session.
        rE   rF   N)rY   r   rI   r:   rC   )r   r   s     r   	lazy_loadzTelegramChatLoader.lazy_load   s       ,,TY77 	E 	EI!!'** E99)DDDDDD##G,, E99)DDDDD		E 	Er   N)__name__
__module____qualname____doc__r   r   r   r   staticmethodr   r:   rC   r   rY   r[    r   r   r   r      s        
ACI
A 
A 
A 
A ,-# ,-+ ,- ,- ,- \,-\ -# -+ - - - \-> HS HXc] H H H \H.E8K0 E E E E E Er   r   )r?   loggingrG   rQ   rM   pathlibr   typingr   r   r   langchain_core.chat_loadersr   langchain_core.chat_sessionsr   langchain_core.messagesr	   r
   r   	getLoggerr\   r-   r   ra   r   r   <module>ri      s      				         ( ( ( ( ( ( ( ( ( ( 6 6 6 6 6 6 4 4 4 4 4 4 H H H H H H H H H H		8	$	$IE IE IE IE IE IE IE IE IE IEr   