
    ynh]                     |   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 	 d d	lmZ d
Zd dlmZ d dlmZ d dlmZ erd dlmZ  eed      Z G d d      Z  G d dejB                  jD                        Z# G d dejB                  jH                        Z%y# e$ r dZY vw xY w)    N)
HTTPStatus)Path)socket)
SSLContext)TracebackType)TYPE_CHECKINGOptionalUnion)
HTTPServer)bind_unix_socketTF)Update)
get_logger)ExtBot)BotUpdater)
class_namec                       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
Zddeej                     ddfdZddZy)WebhookServerz6Thin wrapper around ``tornado.httpserver.HTTPServer``.)_http_server_server_lock_shutdown_lock
is_runninglistenportunixNr   r   webhook_appWebhookAppClassssl_ctxr   c                 T   |rt         st        d      t        ||      | _        || _        || _        d| _        d | _        |rt        |t              r|| _        n|rt        t        |            | _        t        j                         | _        t        j                         | _        y )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   s         c/var/www/html/profi_bot/bot/venv/lib/python3.12/site-packages/telegram/ext/_utils/webhookhandler.py__init__zWebhookServer.__init__A   s     OPP&{H		JtV,DI(T3DI#LLN%lln    readyreturnc                   K   | j                   4 d {    | j                  r&| j                  j                  | j                         n1| j                  j	                  | j
                  | j                         d| _        ||j                          t        j                  d       d d d       d {    y 7 7 # 1 d {  7  sw Y   y xY ww)N)addressTzWebhook Server started.)
r   r   r   
add_socketr   r   r   set_LOGGERdebug)r'   r+   s     r(   serve_foreverzWebhookServer.serve_foreverW   s     $$$yy!!,,TYY7!!((DKK(H"DO 		MM34 %$$$$$$sE   CB:CBB>)C4B<5C<C>CCCCc                   K   | j                   4 d {    | j                  s't        j                  d       	 d d d       d {    y d| _        | j                  j                          | j                  j                          d {    t        j                  d       d d d       d {    y 7 7 p7 .7 # 1 d {  7  sw Y   y xY ww)Nz.Webhook Server is already shut down. ReturningFzWebhook Server stopped)r   r   r1   r2   r   stopclose_all_connectionsr'   s    r(   shutdownzWebhookServer.shutdownd   s     &&&??NO '&& $DO""$##99;;;MM23 '&&& < '&&&ss   CB4C#B<CB6C>B<	B8
B<#C.B:/C6C8B<:C<CCC
CNr,   N)__name__
__module____qualname____doc__	__slots__r$   intr	   r   r
   r   r   r)   r%   Eventr3   r8    r*   r(   r   r   4   s}    @I  48-- - '	-
 *%- uS$./0-,5'--)@ 5D 54r*   r   c            
       ~    e Zd ZdZ	 ddedddej                  dee   fdZd	e	j                  j                  d
dfdZy)r   z!Application used in the WebserverNwebhook_pathbotr   update_queuesecret_tokenc                     |||d| _         | dt        | j                   fg}t        j                  j                  j                  | |       y )N)rE   rF   rG   z/?)shared_objectsTelegramHandlertornadowebApplicationr)   )r'   rD   rE   rF   rG   handlerss         r(   r)   zWebhookAppClass.__init__r   sO     ((

 &b)?D<O<OPQ((x8r*   handlerr,   c                      y)zIOverrides the default implementation since we have our own logging setup.NrB   )r'   rO   s     r(   log_requestzWebhookAppClass.log_request   s    r*   r9   )r;   r<   r=   r>   r$   r%   Queuer	   r)   rK   rL   RequestHandlerrQ   rB   r*   r(   r   r   o   s]    + '+99 9 mm	9
 sm9X7;;#=#= X$ Xr*   r   c                       e Zd ZdZdZdZdddej                  dedd	fd
Z	ddZ
ddZddZdeee      dee   dee   dd	fdZy	)rJ   z:BaseHandler that processes incoming requests from Telegram)rE   rG   rF   )POSTrE   r   rF   rG   r,   Nc                 ^    || _         || _        || _        |rt        j	                  d       yy)zFInitialize for each request - that's the interface provided by tornadozLThe webhook server has a secret token, expecting it in incoming requests nowN)rE   rF   rG   r1   r2   )r'   rE   rF   rG   s       r(   
initializezTelegramHandler.initialize   s2     ((MM^ r*   c                 (    | j                  dd       y)zSets default headersContent-Typez!application/json; charset="utf-8"N)
set_headerr7   s    r(   set_default_headersz#TelegramHandler.set_default_headers   s    (KLr*   c                    K   t         j                  d       | j                          | j                  j                  j                         }t        j                  |      }| j                  t        j                         t         j                  d|       	 t        j                  || j                        }|ryt         j                  d|j(                         t+        | j                  t,              r| j                  j/                  |       | j0                  j3                  |       d{    yy# t        $ rM}t         j                  d||       t         j"                  j%                  t        j&                  d      |d}~ww xY w7 _w)	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)r1   r2   _validate_postrequestbodydecodejsonloads
set_statusr   OKr   de_jsonrE   	ExceptioncriticalrK   rL   	HTTPErrorBAD_REQUEST	update_idr#   r   insert_callback_datarF   put)r'   json_stringdataupdateexcs        r(   postzTelegramHandler.post   s>    )*ll''..0zz+&
&1;?	^^D$((3F MM7   $((F+--f5##''///   		K	   ++''&&/N ( 		, 0s8   BF D3 7A5F,F-F3	F	<AFF		Fc                 L   | j                   j                  j                  dd      }|dk7  r-t        j                  j                  t        j                        | j                  | j                   j                  j                  d      }|sDt        j                  d       t        j                  j                  t        j                  d      || j                  k7  rEt        j                  d|       t        j                  j                  t        j                  d      yy)	z+Only accept requests with content type JSONrY   Nzapplication/jsonzX-Telegram-Bot-Api-Secret-Tokenz(Request did not include the secret tokenr_   z&Request had the wrong secret token: %sz"Request had the wrong secret token)rb   headersgetrK   rL   rl   r   	FORBIDDENrG   r1   r2   )r'   	ct_headertokens      r(   ra   zTelegramHandler._validate_post   s    LL((,,^TB	**++''
(<(<==(LL((,,-NOEHIkk++((1[ ,   )))FNkk++((1U ,   * )r*   typvaluetbc                     t         j                  d| j                  j                  d|r|r|r|||f       y|       y)z@Override the default logging and instead use our custom logging.z%s - %szException in TelegramHandlerr]   N)r1   r2   rb   	remote_ip)r'   r|   r}   r~   s       r(   log_exceptionzTelegramHandler.log_exception   sI     	LL""*),2c5"%	 	 	
 DI	 	 	
r*   r:   )r;   r<   r=   r>   r?   SUPPORTED_METHODSr%   rR   r$   rW   r[   ru   ra   r	   typeBaseExceptionr   r   rB   r*   r(   rJ   rJ      s    D7I!	e 	7== 	PS 	X\ 	M"0H&
d=)*
 &
 ]#	

 

r*   rJ   )&r%   re   httpr   pathlibr   r   sslr   typesr   typingr   r	   r
   tornado.webrK   tornado.httpserverr   tornado.netutilr   r!   ImportErrortelegramr   telegram._utils.loggingr   telegram.ext._extbotr   r   r;   r1   r   rL   rM   r   rS   rJ   rB   r*   r(   <module>r      s   (        1 1  )0N  . ' X)
484 84vXgkk-- X.Y
gkk00 Y
A  Ns   B1 1B;:B;