o
    /hc                     @   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mZ d dlZd dlmZ z
d d	lmZ d
ZW n eyQ   dZY nw d dlmZ d dlmZ d dlmZ erld 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Type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&   [/var/www/html/govbot/env/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#   Eventr0   r4   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@   rA   z/?)shared_objectsTelegramHandlertornadowebApplicationr(   )r%   r>   r?   r@   rA   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%   rH   r&   r&   r'   log_request   s    zWebhookAppClass.log_requestr5   )r7   r8   r9   r:   r"   r#   Queuer   r(   rD   rE   RequestHandlerrI   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 )rC   z:BaseHandler that processes incoming requests from Telegram)r?   rA   r@   )POSTr?   r   r@   rA   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@   rA   r.   r/   )r%   r?   r@   rA   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_headerr3   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criticalrD   rE   	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 JSONrN   Nzapplication/jsonzX-Telegram-Bot-Api-Secret-Tokenz(Request did not include the secret tokenrS   z&Request had the wrong secret token: %sz"Request had the wrong secret token)rV   headersgetrD   rE   r`   r   	FORBIDDENrA   r.   r/   )r%   	ct_headertokenr&   r&   r'   rU      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 TelegramHandlerrQ   N)r.   r/   rV   	remote_ip)r%   ro   rp   rq   r&   r&   r'   log_exception   s   
zTelegramHandler.log_exceptionr6   )r7   r8   r9   r:   r;   SUPPORTED_METHODSr#   rJ   r"   rM   rP   ri   rU   r   r	   BaseExceptionr   rs   r&   r&   r&   r'   rC      s"    


$
rC   )'r#   rY   httpr   pathlibr   r   sslr   typesr   typingr   r   r	   r
   tornado.webrD   tornado.httpserverr   tornado.netutilr   r   ImportErrortelegramr   telegram._utils.loggingr   telegram.ext._extbotr   r   r7   r.   r   rE   rF   r   rK   rC   r&   r&   r&   r'   <module>   s2   ;