
    7|h'                         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y)	    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y
)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        |t              r|| _        yt        |      | _        y)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     h/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/chat_loaders/telegram.py__init__zTelegramChatLoader.__init__   s     'tS1D	s4y	    	file_pathreturnc           	      :   	 ddl m} t        | dd      5 } ||d      }ddd       g }d}j	                  d	      D ]  }|j                  d
      d   }|j                  d      }||t        j                  d       B||}	n|j                  j                         }	|j                  d      j                  j                         }
|j                  t        |
|	d|igd             |	} t        |      S # t        $ r t        d      w xY w# 1 sw Y   xY w)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'   sC   	) )S73 	6t }5D	6 9;{{#56 	(G**+EFwOI ' 2 2< @ (_-D=>"*+	-2288:	%%g.3399;DNN "+$2I#>"?' (O)	(, G,,C  	A 		6 	6s   C9 
D9DDc           	      p   t        | dd      5 }t        j                  |      }ddd       j                  dg       }g }|D ]Z  }|j                  dd      }|j                  dd      }|j                  d	d      }|j	                  t        ||d
|igd             \ t        |      S # 1 sw Y   xY w)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   s     )S73 	#t99T?D	# 88J+%' 	G;;vr*DFB/IFB/INN "+$2I#>"?'	 G,,)	# 	#s   B,,B5c              #     K   t         j                  j                  |       r| j                  d      r|  yt         j                  j	                  |       rZt        j
                  |       D ]A  \  }}}|D ]6  }|j                  d      st         j                  j                  ||       8 C yt        j                  |       ryt        j                  |       5 }|j                         D ]F  }|j                  d      st        j                         5 }|j                  ||       ddd       H 	 ddd       yy# 1 sw Y   ]xY w# 1 sw Y   yxY ww)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   s2     77>>$DMM2D$EJWW]]4 "$''$- 7a! 7D}}%78 ggll46677 %& H($--/ HD}}%78%88: Hh"*"2"24h"2"GGH HHH H &H HH HsC   BE
AE%EEE +
E6
E E	EEEc              #      K   | j                  | j                        D ]L  }|j                  d      r| j                  |       (|j                  d      s:| j	                  |       N yw)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   sf      ,,TYY7 	EI!!'*99)DD##G,99)DD		Es   AA.A.N)__name__
__module____qualname____doc__r   r   r   r   staticmethodr   r:   rC   r   rY   r[    r   r   r   r      s    
ACI
A ,-# ,-+ ,- ,-\ -# -+ - -> HS HXc] H H.E8K0 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      sK      	    ( ( 6 4 H H			8	$IE IEr   