o
    Zzj2  ã                   @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlZd dlmZ z
d d	lmZ d
ZW n eyK   dZY nw d dlmZ d dlmZ d dlmZ erfd dlmZ eedd�ZG dd„ dƒZG dd„ dejj ƒZ!G dd„ dejj"ƒZ#dS )é    N)Ú
HTTPStatus)ÚPath)Úsocket)Ú
SSLContext)ÚTracebackType)ÚTYPE_CHECKING)Ú
HTTPServer)Úbind_unix_socketTF)ÚUpdate)Ú
get_logger)ÚExtBot)ÚBotÚUpdater)Ú
class_namec                   @   sj   e Zd ZdZdZ	ddededddedB d	eeB e	B dB f
d
d„Z
ddejdB ddfdd„Zddd„ZdS )ÚWebhookServerz6Thin wrapper around ``tornado.httpserver.HTTPServer``.)Ú_http_serverÚ_server_lockÚ_shutdown_lockÚ
is_runningÚlistenÚportÚunixNr   r   Úwebhook_appÚWebhookAppClassÚssl_ctxr   c                 C   sv   |rt stdƒ‚t||d�| _|| _|| _d| _d | _|r&t|t	ƒr&|| _n	|r/t
t|ƒƒ| _t ¡ | _t ¡ | _d S )Nz.This OS does not support binding unix sockets.)Ússl_optionsF)ÚUNIX_AVAILABLEÚRuntimeErrorr   r   r   r   r   r   Ú
isinstancer   r	   ÚstrÚasyncioÚLockr   r   )Úselfr   r   r   r   r   © r#   ú]/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telegram/ext/_utils/webhookhandler.pyÚ__init__A   s   
zWebhookServer.__init__ÚreadyÚreturnc              	   Ã   sŒ   �| j 4 I d H š1 | jr| j | j¡ n
| jj| j| jd� d| _|d ur)| ¡  t 	d¡ W d   ƒI d H  d S 1 I d H s?w   Y  d S )N)ÚaddressTzWebhook Server started.)
r   r   r   Ú
add_socketr   r   r   ÚsetÚ_LOGGERÚdebug)r"   r&   r#   r#   r$   Úserve_foreverW   s   €.özWebhookServer.serve_foreverc              	   Ã   s”   �| j 4 I d H š5 | jst d¡ 	 W d   ƒI d H  d S d| _| j ¡  | j ¡ I d H  t d¡ W d   ƒI d H  d S 1 I d H sCw   Y  d S )Nz.Webhook Server is already shut down. ReturningFzWebhook Server stopped)r   r   r+   r,   r   ÚstopÚclose_all_connections©r"   r#   r#   r$   Úshutdownd   s   €
ý
.ùzWebhookServer.shutdown©N©r'   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   Úintr   r   r   r%   r    ÚEventr-   r1   r#   r#   r#   r$   r   4   s"    úþýüû
úr   c                
   @   sJ   e Zd ZdZ	ddedddejdedB fdd	„Zd
ej	j
ddfdd„ZdS )r   z!Application used in the WebserverNÚwebhook_pathÚbotr   Úupdate_queueÚsecret_tokenc                 C   s6   |||dœ| _ |› d�t| j fg}tjj | |¡ d S )N)r<   r=   r>   z/?)Úshared_objectsÚTelegramHandlerÚtornadoÚwebÚApplicationr%   )r"   r;   r<   r=   r>   Úhandlersr#   r#   r$   r%   r   s   ýzWebhookAppClass.__init__Úhandlerr'   c                 C   s   dS )zIOverrides the default implementation since we have our own logging setup.Nr#   )r"   rE   r#   r#   r$   Úlog_request�   s    zWebhookAppClass.log_requestr2   )r4   r5   r6   r7   r   r    ÚQueuer%   rA   rB   ÚRequestHandlerrF   r#   r#   r#   r$   r   o   s    ûþýü
ûr   c                   @   s|   e Zd ZdZdZdZdddejdedd	fd
d„Z	ddd„Z
ddd„Zddd„Zdee d	B ded	B ded	B dd	fdd„Zd	S )r@   z:BaseHandler that processes incoming requests from Telegram)r<   r>   r=   )ÚPOSTr<   r   r=   r>   r'   Nc                 C   s(   || _ || _|| _|rt d¡ dS dS )zFInitialize for each request - that's the interface provided by tornadozLThe webhook server has a secret token, expecting it in incoming requests nowN)r<   r=   r>   r+   r,   )r"   r<   r=   r>   r#   r#   r$   Ú
initialize�   s   ÿÿzTelegramHandler.initializec                 C   s   |   dd¡ dS )zSets default headersúContent-Typez!application/json; charset="utf-8"N)Ú
set_headerr0   r#   r#   r$   Úset_default_headers˜   s   z#TelegramHandler.set_default_headersc              
   Ã   sÖ   �t  d¡ |  ¡  | jj ¡ }t |¡}|  t	j
¡ t  d|¡ z	t || j¡}W n tyH } zt jd||d� tjjt	jdd�|‚d}~ww |rit  d|j¡ t| jtƒr^| j |¡ | j |¡I dH  dS dS )	zHandle incoming POST requestzWebhook triggeredzWebhook received data: %szySomething went wrong processing the data received from Telegram. Received data was *not* processed! Received data was: %r©Úexc_infozUpdate could not be processed©ÚreasonNz%Received Update with ID %d on Webhook)r+   r,   Ú_validate_postÚrequestÚbodyÚdecodeÚjsonÚloadsÚ
set_statusr   ÚOKr
   Úde_jsonr<   Ú	ExceptionÚcriticalrA   rB   Ú	HTTPErrorÚBAD_REQUESTÚ	update_idr   r   Úinsert_callback_datar=   Úput)r"   Újson_stringÚdataÚupdateÚexcr#   r#   r$   Úpostœ   s>   €

üÿþ€ùýõzTelegramHandler.postc                 C   sŽ   | j j dd¡}|dkrtj tj¡‚| jdurC| j j d¡}|s/t	 
d¡ tjjtjdd�‚|| jkrEt	 
d|¡ tjjtjdd�‚dS dS )	z+Only accept requests with content type JSONrK   Nzapplication/jsonzX-Telegram-Bot-Api-Secret-Tokenz(Request did not include the secret tokenrP   z&Request had the wrong secret token: %sz"Request had the wrong secret token)rS   ÚheadersÚgetrA   rB   r]   r   Ú	FORBIDDENr>   r+   r,   )r"   Ú	ct_headerÚtokenr#   r#   r$   rR   À   s"   

ÿ
ÿ÷zTelegramHandler._validate_postÚtypÚvalueÚtbc                 C   s0   t jd| jjd|r|r|r|||fn|d� dS )z@Override the default logging and instead use our custom logging.z%s - %szException in TelegramHandlerrN   N)r+   r,   rS   Ú	remote_ip)r"   rl   rm   rn   r#   r#   r$   Úlog_exceptionÓ   s   
üzTelegramHandler.log_exceptionr3   )r4   r5   r6   r7   r8   ÚSUPPORTED_METHODSr    rG   r   rJ   rM   rf   rR   ÚtypeÚBaseExceptionr   rp   r#   r#   r#   r$   r@   †   s"    


$
þýüûr@   )$r    rV   Úhttpr   Úpathlibr   r   Ússlr   Útypesr   Útypingr   Útornado.webrA   Útornado.httpserverr   Útornado.netutilr	   r   ÚImportErrorÚtelegramr
   Útelegram._utils.loggingr   Útelegram.ext._extbotr   r   r4   r+   r   rB   rC   r   rH   r@   r#   r#   r#   r$   Ú<module>   s2   ÿ;