o
    Fh/                     @   s   d dl Z d dlZd dlZd dl mZmZ d dlmZ d dlmZ d dl	m
Z
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 dlmZmZ eeZd	Zd
ZG dd dZG dd deZdS )    N)EventLock)suppress)TracebackType)Any	AwaitableCallableDictOptionalTypeUnion)BaseMiddlewareBot)get_flag)MessageTelegramObjectg      @g        c                   @   sB  e Zd ZdZddeeddedeee	f de
e	 ded	ed
eddfddZedefddZd	eddfddZd5ddZd5ddZd5ddZd6ddZde
ee  de
e de
e defddZedeefdee	ef dede
e	 d	ed
edd fdd Zedeefdee	ef dede
e	 d	ed
edd fd!d"Zedeefdee	ef dede
e	 d	ed
edd fd#d$Zedeefdee	ef dede
e	 d	ed
edd fd%d&Zedeefdee	ef dede
e	 d	ed
edd fd'd(Zedeefdee	ef dede
e	 d	ed
edd fd)d*Z edeefdee	ef dede
e	 d	ed
edd fd+d,Z!edeefdee	ef dede
e	 d	ed
edd fd-d.Z"edeefdee	ef dede
e	 d	ed
edd fd/d0Z#edeefdee	ef dede
e	 d	ed
edd fd1d2Z$edeefdee	ef dede
e	 d	ed
edd fd3d4Z%dS )7ChatActionSendera  
    This utility helps to automatically send chat action until long actions is done
    to take acknowledge bot users the bot is doing something and not crashed.

    Provides simply to use context manager.

    Technically sender start background task with infinity loop which works
    until action will be finished and sends the
    `chat action <https://core.telegram.org/bots/api#sendchataction>`_
    every 5 seconds.
    Ntyping)message_thread_idactionintervalinitial_sleepbotchat_idr   r   r   r   returnc                C   sF   || _ || _|| _|| _|| _|| _t | _t | _	t | _
d| _dS )a`  
        :param bot: instance of the bot
        :param chat_id: target chat id
        :param message_thread_id: unique identifier for the target message thread; supergroups only
        :param action: chat action type
        :param interval: interval between iterations
        :param initial_sleep: sleep before first sending of the action
        N)r   r   r   r   r   r   r   _lockr   _close_event_closed_event_task)selfr   r   r   r   r   r    r    L/var/www/html/venv/lib/python3.10/site-packages/aiogram/utils/chat_action.py__init__   s   
zChatActionSender.__init__c                 C   s
   t | jS N)boolr   r   r    r    r!   running=   s   
zChatActionSender.runningc                    sJ   t tj t| j |I d H  W d    d S 1 sw   Y  d S r#   )r   asyncioTimeoutErrorwait_forr   wait)r   r   r    r    r!   _waitA   s   "zChatActionSender._waitc                    s$  t d| j| j| jj zrd}| | jI d H  | j	 slt
 }t d| j| j| jj| | jj| j| j| jdI d H  |d7 }| jt
 |  }| |I d H  | j	 rW t d| j| j| jj | j  d S W t d| j| j| jj | j  d S t d| j| j| jj | j  w )Nz9Started chat action %r sender in chat_id=%s via bot id=%dr   zFSent chat action %r to chat_id=%s via bot %d (already sent actions %d))r   r   r      z:Finished chat action %r sender in chat_id=%s via bot id=%d)loggerdebugr   r   r   idr+   r   r   is_settime	monotonicsend_chat_actionr   r   r   set)r   counterstartr   r    r    r!   _workerE   sd   
zChatActionSender._workerc              	      st   | j 4 I d H % | j  | j  | jrtdt|  | _	W d   I d H  d S 1 I d H s3w   Y  d S )NzAlready running)
r   r   clearr   r&   RuntimeErrorr'   create_taskr7   r   r%   r    r    r!   _runj   s   

.zChatActionSender._runc              	      s   | j 4 I d H 0 | js	 W d   I d H  d S | j s*| j  | j I d H  d | _W d   I d H  d S 1 I d H s>w   Y  d S r#   )r   r&   r   r0   r4   r   r*   r   r%   r    r    r!   _stopr   s   

.zChatActionSender._stopc                    s   |   I d H  | S r#   )r;   r%   r    r    r!   
__aenter__{   s   zChatActionSender.__aenter__exc_type	exc_value	tracebackc                    s   |   I d H  d S r#   )r<   )r   r>   r?   r@   r    r    r!   	__aexit__   s   zChatActionSender.__aexit__c                 C      | |||d||dS )z2Create instance of the sender with `typing` actionr   r   r   r   r   r   r   r    clsr   r   r   r   r   r    r    r!   r         
zChatActionSender.typingc                 C   rB   )z8Create instance of the sender with `upload_photo` actionupload_photorC   r    rD   r    r    r!   rG      rF   zChatActionSender.upload_photoc                 C   rB   )z8Create instance of the sender with `record_video` actionrecord_videorC   r    rD   r    r    r!   rH      rF   zChatActionSender.record_videoc                 C   rB   )z8Create instance of the sender with `upload_video` actionupload_videorC   r    rD   r    r    r!   rI      rF   zChatActionSender.upload_videoc                 C   rB   )z8Create instance of the sender with `record_voice` actionrecord_voicerC   r    rD   r    r    r!   rJ      rF   zChatActionSender.record_voicec                 C   rB   )z8Create instance of the sender with `upload_voice` actionupload_voicerC   r    rD   r    r    r!   rK      rF   zChatActionSender.upload_voicec                 C   rB   )z;Create instance of the sender with `upload_document` actionupload_documentrC   r    rD   r    r    r!   rL      rF   z ChatActionSender.upload_documentc                 C   rB   )z:Create instance of the sender with `choose_sticker` actionchoose_stickerrC   r    rD   r    r    r!   rM     rF   zChatActionSender.choose_stickerc                 C   rB   )z9Create instance of the sender with `find_location` actionfind_locationrC   r    rD   r    r    r!   rN     rF   zChatActionSender.find_locationc                 C   rB   )z=Create instance of the sender with `record_video_note` actionrecord_video_noterC   r    rD   r    r    r!   rO   2  rF   z"ChatActionSender.record_video_notec                 C   rB   )z=Create instance of the sender with `upload_video_note` actionupload_video_noterC   r    rD   r    r    r!   rP   E  rF   z"ChatActionSender.upload_video_note)r   N)r   r   )&__name__
__module____qualname____doc__DEFAULT_INTERVALDEFAULT_INITIAL_SLEEPr   r   strintr
   floatr"   propertyr$   r&   r+   r7   r;   r<   r=   r   BaseExceptionr   r   rA   classmethodr   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   r    r    r    r!   r      s   
	


%

	












r   c                   @   sJ   e Zd ZdZdeeeeef ge	e f dedeeef defddZ
dS )	ChatActionMiddlewarezP
    Helps to automatically use chat action sender for all message handlers
    handlereventdatar   c           
   	      s  t |ts|||I d H S |d }t|dpd}i }t |trB|d }r+||d< |d }r6||d< |d }	rA|	|d< nt |trLd|d< n||d< t |tr[|jr[|jnd |d< td	||j	j
d|4 I d H  |||I d H W  d   I d H  S 1 I d H sw   Y  d S )
Nr   chat_actionr   r   r   r   r   )r   r   r    )
isinstancer   r   dictgetr$   is_topic_messager   r   chatr/   )
r   r^   r_   r`   r   ra   kwargsr   r   r   r    r    r!   __call__^  s4   



"0zChatActionMiddleware.__call__N)rQ   rR   rS   rT   r   r   r	   rW   r   r   rh   r    r    r    r!   r]   Y  s    
r]   ) r'   loggingr1   r   r   
contextlibr   typesr   r   r   r   r   r	   r
   r   r   aiogramr   r   aiogram.dispatcher.flagsr   aiogram.typesr   r   	getLoggerrQ   r-   rU   rV   r   r]   r    r    r    r!   <module>   s"    $
  I