a
    y{Jh+                     @   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                	   @   sx  e Zd ZdZddeedeeee	f e
e	 eeeddddZeedd	d
ZeddddZddddZddddZddddZd dddZe
ee  e
e e
e edddZeeefee	ef eeed dddZeeefee	ef eeed dddZeeefee	ef eeed dddZeeefee	ef eeed dd d!Zeeefee	ef eeed dd"d#Zeeefee	ef eeed dd$d%Z eeefee	ef eeed dd&d'Z!eeefee	ef eeed dd(d)Z"eeefee	ef eeed dd*d+Z#eeefee	ef eeed dd,d-Z$eeefee	ef eeed dd.d/Z%dS )0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 )z
        :param bot: instance of the bot
        :param chat_id: target chat id
        :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    K/var/www/shaz/venv/lib/python3.9/site-packages/aiogram/utils/chat_action.py__init__   s    zChatActionSender.__init__)r   c                 C   s
   t | jS N)boolr   r   r    r    r!   running<   s    zChatActionSender.running)r   r   c                    sF   t tj( t| j |I d H  W d    n1 s80    Y  d S r#   )r   asyncioTimeoutErrorwait_forr   wait)r   r   r    r    r!   _wait@   s    zChatActionSender._waitc                    s   t d| j| j| jj zd}| | jI d H  | j	 st
 }t d| j| j| jj| | jj| j| j| jdI d H  |d7 }| jt
 |  }| |I d H  q0W t d| j| j| jj | j  n$t d| j| j| jj | j  0 d S )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	monotonicZsend_chat_actionr   r   r   set)r   counterstartr   r    r    r!   _workerD   sP    
zChatActionSender._workerc              	      sp   | j 4 I d H H | j  | j  | jr2tdt|  | _	W d   I d H  ql1 I d H sb0    Y  d S )NzAlready running)
r   r   clearr   r&   RuntimeErrorr'   Zcreate_taskr6   r   r%   r    r    r!   _runi   s    

zChatActionSender._runc              	      s   | j 4 I d H \ | js,W d   I d H  d S | j sP| j  | j I d H  d | _W d   I d H  q1 I d H sv0    Y  d S r#   )r   r&   r   r0   r3   r   r*   r   r%   r    r    r!   _stopq   s    

zChatActionSender._stopc                    s   |   I d H  | S r#   )r9   r%   r    r    r!   
__aenter__z   s    zChatActionSender.__aenter__)exc_type	exc_value	tracebackr   c                    s   |   I d H  d S r#   )r:   )r   r<   r=   r>   r    r    r!   	__aexit__~   s    zChatActionSender.__aexit__)r   r   r   r   r   c                 C   s   | ||d||dS )z2Create instance of the sender with `typing` actionr   r   r   r   r   r   r    clsr   r   r   r   r    r    r!   r      s    	zChatActionSender.typingc                 C   s   | ||d||dS )z8Create instance of the sender with `upload_photo` actionupload_photor@   r    rA   r    r    r!   rC      s    	zChatActionSender.upload_photoc                 C   s   | ||d||dS )z8Create instance of the sender with `record_video` actionrecord_videor@   r    rA   r    r    r!   rD      s    	zChatActionSender.record_videoc                 C   s   | ||d||dS )z8Create instance of the sender with `upload_video` actionupload_videor@   r    rA   r    r    r!   rE      s    	zChatActionSender.upload_videoc                 C   s   | ||d||dS )z8Create instance of the sender with `record_voice` actionrecord_voicer@   r    rA   r    r    r!   rF      s    	zChatActionSender.record_voicec                 C   s   | ||d||dS )z8Create instance of the sender with `upload_voice` actionupload_voicer@   r    rA   r    r    r!   rG      s    	zChatActionSender.upload_voicec                 C   s   | ||d||dS )z;Create instance of the sender with `upload_document` actionupload_documentr@   r    rA   r    r    r!   rH      s    	z ChatActionSender.upload_documentc                 C   s   | ||d||dS )z:Create instance of the sender with `choose_sticker` actionchoose_stickerr@   r    rA   r    r    r!   rI      s    	zChatActionSender.choose_stickerc                 C   s   | ||d||dS )z9Create instance of the sender with `find_location` actionfind_locationr@   r    rA   r    r    r!   rJ     s    	zChatActionSender.find_locationc                 C   s   | ||d||dS )z=Create instance of the sender with `record_video_note` actionrecord_video_noter@   r    rA   r    r    r!   rK     s    	z"ChatActionSender.record_video_notec                 C   s   | ||d||dS )z=Create instance of the sender with `upload_video_note` actionupload_video_noter@   r    rA   r    r    r!   rL   0  s    	z"ChatActionSender.upload_video_note)&__name__
__module____qualname____doc__DEFAULT_INTERVALDEFAULT_INITIAL_SLEEPr   r   strintr
   floatr"   propertyr$   r&   r+   r6   r9   r:   r;   r   BaseExceptionr   r   r?   classmethodr   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   r    r    r    r!   r      s  
%	











r   c                   @   sD   e Zd ZdZeeeeef ge	e f eeeef ed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r|d }rT||d< |d }rj||d< |d }	r|	|d< nt |trd|d< n||d< t |tr|jr|jnd |d< tf ||j	j
d|4 I d H & |||I d H W  d   I d H  S 1 I d H s0    Y  d S )	Nr   chat_actionr   r   r   r   r   )r   r   )
isinstancer   r   dictgetr$   Zis_topic_messager   r   Zchatr/   )
r   rZ   r[   r\   r   r]   kwargsr   r   r   r    r    r!   __call__G  s.    




"zChatActionMiddleware.__call__N)rM   rN   rO   rP   r   r   r	   rS   r   r   rb   r    r    r    r!   rY   B  s   
rY   ) r'   loggingr1   r   r   
contextlibr   typesr   r   r   r   r   r	   r
   r   r   Zaiogramr   r   Zaiogram.dispatcher.flagsr   Zaiogram.typesr   r   	getLoggerrM   r-   rQ   rR   r   rY   r    r    r    r!   <module>   s    $
  2