o
    "h]                     @   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mZmZ d dlZd dlmZ z
d d	lmZ d
ZW n eyO   dZY nw d dlmZ d dlmZ d dlmZ erjd 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OptionalUnion)
HTTPServer)bind_unix_socketTF)Update)
get_logger)ExtBot)BotUpdater)
class_namec                   @   sl   e Zd ZdZdZ	ddededddee d	ee	ee
ef  f
d
dZddeej 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%   U/var/www/html/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   r'   r"   Eventr/   r3   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e 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$   rG   r%   r%   r&   log_request   s    zWebhookAppClass.log_requestr4   )r6   r7   r8   r9   r!   r"   Queuer   r'   rC   rD   RequestHandlerrH   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e  dee dee dd	fddZd	S )rB   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_headerr2   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criticalrC   rD   	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 JSONrM   Nzapplication/jsonzX-Telegram-Bot-Api-Secret-Tokenz(Request did not include the secret tokenrR   z&Request had the wrong secret token: %sz"Request had the wrong secret token)rU   headersgetrC   rD   r_   r   	FORBIDDENr@   r-   r.   )r$   	ct_headertokenr%   r%   r&   rT      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 TelegramHandlerrP   N)r-   r.   rU   	remote_ip)r$   rn   ro   rp   r%   r%   r&   log_exception   s   
zTelegramHandler.log_exceptionr5   )r6   r7   r8   r9   r:   SUPPORTED_METHODSr"   rI   r!   rL   rO   rh   rT   r   typeBaseExceptionr   rr   r%   r%   r%   r&   rB      s"    


$
rB   )&r"   rX   httpr   pathlibr   r   sslr   typesr   typingr   r   r	   tornado.webrC   tornado.httpserverr
   tornado.netutilr   r   ImportErrortelegramr   telegram._utils.loggingr   telegram.ext._extbotr   r   r6   r-   r   rD   rE   r   rJ   rB   r%   r%   r%   r&   <module>   s2   ;