o
    5˜ŽjI/  ã                   @   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mZ d dlm	Z	 d dl
mZ d dl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)Ú	AwaitableÚCallable)Úsuppress)ÚTracebackType)ÚAny)ÚBaseMiddlewareÚBot)Úget_flag)ÚMessageÚTelegramObjectg      @g        c                   @   s  e Zd ZdZddeedœdedeeB dedB 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 dB dedB dedB defdd„ZedeefdeeB dededB d	e	d
e	dd fdd „ƒZedeefdeeB dededB d	e	d
e	dd fd!d"„ƒZedeefdeeB dededB d	e	d
e	dd fd#d$„ƒZedeefdeeB dededB d	e	d
e	dd fd%d&„ƒZedeefdeeB dededB d	e	d
e	dd fd'd(„ƒZedeefdeeB dededB d	e	d
e	dd fd)d*„ƒZedeefdeeB dededB d	e	d
e	dd fd+d,„ƒZedeefdeeB dededB d	e	d
e	dd fd-d.„ƒZ edeefdeeB dededB d	e	d
e	dd fd/d0„ƒZ!edeefdeeB dededB d	e	d
e	dd fd1d2„ƒZ"edeefdeeB dededB 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   úV/var/www/kodo/Anonymous/send/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   Ú_waitB   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   Ú_workerF   sd   €ü
ûýïüçüúüzChatActionSender._workerc              	   Ã   sx   �| j 4 I d H š' | j ¡  | j ¡  | jrd}t|ƒ‚t |  ¡ ¡| _	W d   ƒI d H  d S 1 I d H s5w   Y  d S )NzAlready running)
r   r   Úclearr   r"   ÚRuntimeErrorr#   Úcreate_taskr3   r   )r   Úmsgr   r   r   Ú_runk   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   r,   r0   r   r&   r   r!   r   r   r   Ú_stopt   s   €þ

.úzChatActionSender._stopc                 Ã   s   �|   ¡ I d H  | S r   )r8   r!   r   r   r   Ú
__aenter__}   s   €zChatActionSender.__aenter__Úexc_typeÚ	exc_valueÚ	tracebackc                 Ã   s   �|   ¡ I d H  d S r   )r9   )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   r?   )z8Create instance of the sender with `upload_photo` actionÚupload_photor@   r   rA   r   r   r   rD   œ   rC   zChatActionSender.upload_photoc                 C   r?   )z8Create instance of the sender with `record_video` actionÚrecord_videor@   r   rA   r   r   r   rE   ¯   rC   zChatActionSender.record_videoc                 C   r?   )z8Create instance of the sender with `upload_video` actionÚupload_videor@   r   rA   r   r   r   rF   Â   rC   zChatActionSender.upload_videoc                 C   r?   )z8Create instance of the sender with `record_voice` actionÚrecord_voicer@   r   rA   r   r   r   rG   Õ   rC   zChatActionSender.record_voicec                 C   r?   )z8Create instance of the sender with `upload_voice` actionÚupload_voicer@   r   rA   r   r   r   rH   è   rC   zChatActionSender.upload_voicec                 C   r?   )z;Create instance of the sender with `upload_document` actionÚupload_documentr@   r   rA   r   r   r   rI   û   rC   z ChatActionSender.upload_documentc                 C   r?   )z:Create instance of the sender with `choose_sticker` actionÚchoose_stickerr@   r   rA   r   r   r   rJ     rC   zChatActionSender.choose_stickerc                 C   r?   )z9Create instance of the sender with `find_location` actionÚfind_locationr@   r   rA   r   r   r   rK   !  rC   zChatActionSender.find_locationc                 C   r?   )z=Create instance of the sender with `record_video_note` actionÚrecord_video_noter@   r   rA   r   r   r   rL   4  rC   z"ChatActionSender.record_video_notec                 C   r?   )z=Create instance of the sender with `upload_video_note` actionÚupload_video_noter@   r   rA   r   r   r   rM   G  rC   z"ChatActionSender.upload_video_note)r   N)r   r   )$Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDEFAULT_INTERVALÚDEFAULT_INITIAL_SLEEPr
   ÚstrÚintÚfloatr   Úpropertyr    r"   r'   r3   r8   r9   r:   ÚtypeÚBaseExceptionr   r   r>   Úclassmethodr   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   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 sˆw   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   r_   Úkwargsr   r   r   r   r   r   Ú__call__`  s4   €

€

ÿÿý"0ÿzChatActionMiddleware.__call__N)rN   rO   rP   rQ   r   r   ra   rT   r   r   rf   r   r   r   r   r[   [  s    þý
üûr[   )r#   Úloggingr-   r   r   Úcollections.abcr   r   Ú
contextlibr   Útypesr   r   r   Úaiogramr	   r
   Úaiogram.dispatcher.flagsr   Úaiogram.typesr   r   Ú	getLoggerrN   r)   rR   rS   r   r[   r   r   r   r   Ú<module>   s$    
  J