a
    bg'                     @   s   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eZG dd deZdS )	    N)Path)IteratorListUnion)BaseChatLoader)ChatSession)	AIMessageBaseMessageHumanMessagec                   @   sx   e Zd ZdZeeef dddZeee	dddZ
eee	ddd	Zeeee d
ddZee	 dd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                 C   s   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    r   w/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chat_loaders/telegram.py__init__   s    
zTelegramChatLoader.__init__)	file_pathreturnc                 C   s  zddl m} W n ty*   tdY n0 t| ddd}||d}W d   n1 sZ0    Y  g }d}|d	D ]}|d
d }|d}|du r|du rtd qvn|du r|}	n
|j	 }	|dj	 }
|
t|
|	d|igdd |	}qvt|d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ZsendereventscontentZadditional_kwargsmessages)Zbs4r   ImportErroropenselectZ
select_oneloggerdebugtextstripappendr
   r   )r   r   fileZsoupresultsZprevious_sendermessage	timestampZfrom_name_element	from_namer(   r   r   r   _load_single_chat_session_html'   s<    

(


	z1TelegramChatLoader._load_single_chat_session_htmlc           	      C   s   t | ddd}t|}W d   n1 s.0    Y  |dg }g }|D ]F}|dd}|dd}|d	d}|t||d
|igdd qLt|d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getr*   r
   r   )	r   r+   datar"   r,   r-   r(   r.   r/   r   r   r   _load_single_chat_session_jsonV   s"    
(
z1TelegramChatLoader._load_single_chat_session_json)r   r   c              
   c   s   t j| r| dr| V  nt j| rft | D ].\}}}|D ]}|drBt j||V  qBq4nt| rt	| `}|
 D ]F}|drt  }|j||dV  W d   q1 s0    Y  qW d   n1 s0    Y  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.jsonr   N)osr   isfileendswithisdirwalkjoinzipfile
is_zipfileZipFilenamelisttempfileTemporaryDirectoryextract)r   root_filesr+   zip_filetemp_dirr   r   r   _iterate_filesv   s    




z!TelegramChatLoader._iterate_files)r   c                 c   sD   |  | jD ]2}|dr(| |V  q|dr| |V  qdS )zLazy load the messages from the chat file and yield them
        in as chat sessions.

        Yields:
            ChatSession: The loaded chat session.
        r9   r:   N)rM   r   r=   r0   r8   )r   r   r   r   r   	lazy_load   s
    

zTelegramChatLoader.lazy_loadN)__name__
__module____qualname____doc__r   r   r   r   staticmethodr   r0   r8   r   rM   rN   r   r   r   r   r      s   
.r   )r4   loggingr;   rE   rA   pathlibr   typingr   r   r   Zlangchain_core.chat_loadersr   Zlangchain_core.chat_sessionsr   Zlangchain_core.messagesr   r	   r
   	getLoggerrO   r&   r   r   r   r   r   <module>   s   
