o
    HEDi 8                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dlm
Z
mZmZmZmZ d dlZd dlZd dlmZ ddlm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 G dd dZ dS )    N)
ContextVar)DictListOptionalUnionType)sentinel   )api)TelegramAPIServerTELEGRAM_PRODUCTION   )	ParseModebase)json)check_integrity)
deprecatedc                   @   sH  e Zd ZdZedZedZdddddddddef
dej	de
eejejf  de
ej d	e
ej	 d
e
ej de
ej dej
ej	 de
ej de
ej dej
ejejejejf  defddZdejfddZede
ej fddZde
ej fddZeedddde
ej fddZedej
ejejejejf  dej
ej fd d!Z ed"d# Z!e!j"d$d# Z!e!j#d%d# Z!e$j%dejejejejf fd&d'Z&ed(d) Z'e'j"d*d) Z'e$j%dRd+ej	de
ej fd,d-Z(ed.dd/d0d1 Z)		dSd2ej	d3e
e* d4e
e* dee+e*ejf fd5d6Z,de-d7dddfd8ej	d9e
eej.e/j0f  de
ej d:e
ej d;e
ej d<e
ee1e/j0f  d=e
ej dee2j3e2j4f fd>d?Z5d@dA Z6dee*ejf fdBdCZ7edDdE Z8e8j"dFdE Z8e8j#dGdE Z8edHdI Z9e9j"dJdI Z9e9j#dKdI Z9edLdM Z:e:j"dNdM Z:e:j#dOdM Z:dPdQ Z;dS )TBaseBotz+
    Base class for bot. It's raw bot.
    TelegramRequestTimeoutBotDifferentTokenNTtokenloopconnections_limitproxy
proxy_authvalidate_token
parse_modedisable_web_page_previewprotect_contenttimeoutserverc                 C   s   || _ |r
t| d| _|| _t|jddd | _|| _|| _	|| _
tjt d}d| _tj| _t||d| _t|tr|dsI|drdd	lm} dd
lm} ||\}}}}}|rj|se|j}|sj|j}|| _| jj|||||dd d| _	d| _
d| _|
| _ || _!|| _"|	| _#dS )a  
        Instructions how to get Bot token is found here: https://core.telegram.org/bots#3-how-do-i-create-a-bot

        :param token: token from @BotFather
        :type token: :obj:`str`
        :param loop: event loop
        :type loop: Optional Union :obj:`asyncio.BaseEventLoop`, :obj:`asyncio.AbstractEventLoop`
        :param connections_limit: connections limit for aiohttp.ClientSession
        :type connections_limit: :obj:`int`
        :param proxy: HTTP proxy URL
        :type proxy: :obj:`str`
        :param proxy_auth: Authentication information
        :type proxy_auth: Optional :obj:`aiohttp.BasicAuth`
        :param validate_token: Validate token.
        :type validate_token: :obj:`bool`
        :param parse_mode: You can set default parse mode
        :type parse_mode: :obj:`str`
        :param disable_web_page_preview: You can set default disable web page preview parameter
        :type disable_web_page_preview: :obj:`bool`
        :param protect_content: Protects the contents of sent messages
            from forwarding and saving
        :type protect_content: :obj:`typing.Optional[base.Boolean]`
        :param timeout: Request timeout
        :type timeout: :obj:`typing.Optional[typing.Union[base.Integer, base.Float, aiohttp.ClientTimeout]]`
        :param server: Telegram Bot API Server endpoint.
        :type server: :obj:`TelegramAPIServer`
        :raise: when token is invalid throw an :obj:`aiogram.utils.exceptions.ValidationError`
        N:)sepr   )cafile)limitsslz	socks5://z	socks4://)SocksConnector)parse_proxy_urlT)	socks_verhostportusernamepasswordrdns)$
_main_loopr
   check_token_token_BaseBot__tokenintsplitidr    r   r   r%   create_default_contextcertifiwhere_sessionaiohttpTCPConnector_connector_classdict_connector_init
isinstancestr
startswithaiohttp_socksr&   aiohttp_socks.utilsr'   loginr,   update_timeoutr   r   r   r   )selfr   r   r   r   r   r   r   r   r   r   r    ssl_contextr&   r'   r(   r)   r*   r+   r,    rH   _/var/www/www-root/data/www/ovozai.pdev.uz/venv/lib/python3.10/site-packages/aiogram/bot/base.py__init__   sD   *

zBaseBot.__init__returnc                    s    t j| jdi | jtjdS )N)	connectorjson_serializerH   )r9   ClientSessionr;   r=   r   dumpsrF   rH   rH   rI   get_new_sessionx   s
   zBaseBot.get_new_sessionc                 C      | j S N)r.   rP   rH   rH   rI   r   ~   s   zBaseBot.loopc                    sV   | j d u s
| j jr|  I d H | _ | j j s(| j  I d H  |  I d H | _ | j S rS   )r8   closedrQ   _loop
is_runningcloserP   rH   rH   rI   get_session   s   zBaseBot.get_sessionz]Client session should be created inside async function, use `await bot.get_session()` instead   )reason
stacklevelc                 C   rR   rS   )r8   rP   rH   rH   rI   session   s   zBaseBot.sessionvaluec                 C   s$   | d u s
t | tjr| S tj| dS )N)total)r>   r9   ClientTimeout)r]   rH   rH   rI   _prepare_timeout   s   zBaseBot._prepare_timeoutc                 C   s   | j | j}|d u rtS |S rS   )_ctx_timeoutgetrE   r   )rF   r   rH   rH   rI   r      s   zBaseBot.timeoutc                 C   s   |  || _d S rS   )r`   rE   rF   r]   rH   rH   rI   r      s   c                 C   
   d | _ d S rS   )r   rP   rH   rH   rI   r         
c              	   c   s@    |  |}| j|}zdV  W | j| dS | j| w )a  
        Context manager implements opportunity to change request timeout in current context

        :param timeout: Request timeout
        :type timeout: :obj:`typing.Optional[typing.Union[base.Integer, base.Float, aiohttp.ClientTimeout]]`
        :return:
        N)r`   ra   setreset)rF   r   r   rH   rH   rI   request_timeout   s   
	zBaseBot.request_timeoutc                 C   s   | j | jS rS   )
_ctx_tokenrb   r0   rP   rH   rH   rI   __token   s   zBaseBot.__tokenc                 C   s
   || _ d S rS   )r0   rc   rH   rH   rI   rj      re   	bot_tokenc              	   c   sD    |rt | | j|}zd V  W | j| d S | j| w rS   )r
   r/   ri   rf   rg   )rF   rk   r   r   rH   rH   rI   
with_token   s   
zBaseBot.with_tokenzkThis method's behavior will be changed in aiogram v3.0. More info: https://core.telegram.org/bots/api#closer[   c                    s    | j r| j  I dH  dS dS )z+
        Close all client sessions
        N)r8   rW   rP   rH   rH   rI   rW      s   zBaseBot.closemethoddatafilesc                    s@   t j|  I dH | j| j|||f| j| j| jd|I dH S )a  
        Make an request to Telegram Bot API

        https://core.telegram.org/bots/api#making-requests

        :param method: API method
        :type method: :obj:`str`
        :param data: request parameters
        :type data: :obj:`dict`
        :param files: files
        :type files: :obj:`dict`
        :return: result
        :rtype: Union[List, Dict]
        :raise: :obj:`aiogram.exceptions.TelegramApiError`
        N)r   r   r   )r
   make_requestrX   r    r1   r   r   r   )rF   rn   ro   rp   kwargsrH   rH   rI   request   s    zBaseBot.requesti   	file_pathdestination
chunk_sizeseekdestination_dir	make_dirsc              	      s.  |r	|r	t d|du r|du rt }n	|rtj||}|r8t|tjs8tj|r8tj	tj|dd | 
|}t|tjrE|nt|d}	|  I dH }
|
j||| j| jdd4 I dH "}	 |j|I dH }|son
|	| |	  qcW d  I dH  n1 I dH sw   Y  |r|	d |	S )aY  
        Download file by file_path to destination file or directory

        if You want to automatically create destination (:class:`io.BytesIO`) use default
        value of destination and handle result of this method.

        At most one of these parameters can be used: :param destination:, :param destination_dir:

        :param file_path: file path on telegram server (You can get it from :obj:`aiogram.types.File`)
        :type file_path: :obj:`str`
        :param destination: filename or instance of :class:`io.IOBase`. For e. g. :class:`io.BytesIO`
        :param timeout: Integer
        :param chunk_size: Integer
        :param seek: Boolean - go to start of file when downloading is finished.
        :param destination_dir: directory for saving files
        :param make_dirs: Make dirs if not exist
        :return: destination
        z>Use only one of the parameters:destination or destination_dir.NT)exist_okwb)r   r   r   raise_for_statusr   )
ValueErrorioBytesIOospathjoinr>   IOBasedirnamemakedirsget_file_urlopenrX   rb   r   r   contentreadwriteflushrw   )rF   rt   ru   r   rv   rw   rx   ry   urldestr\   responsechunkrH   rH   rI   download_file   sB   


(
zBaseBot.download_filec                 C   s   | j j| j|dS )N)r   r   )r    file_urlr1   )rF   rt   rH   rH   rI   r   .  s   zBaseBot.get_file_urlc                    sD   |du ri }nt |tr|||< d}n||i}| |||I dH S )a  
        Send file

        https://core.telegram.org/bots/api#inputfile

        :param file_type: field name
        :param method: API method
        :param file: String or io.IOBase
        :param payload: request payload
        :return: response
        N)r>   r?   rs   )rF   	file_typern   filepayloadrp   rH   rH   rI   	send_file1  s   
zBaseBot.send_filec                 C      t | dd S )N_parse_modegetattrrP   rH   rH   rI   r   H     zBaseBot.parse_modec                 C   s   |d u rt | dd  d S t|tstdt| | }|t vr-tdt  t | d| |dkr@t	j
ddd d S d S )Nr   zParse mode must be str, not zParse mode must be one of markdownzParse mode `Markdown` is legacy since Telegram Bot API 4.5, retained for backward compatibility. Use `MarkdownV2` instead.
https://core.telegram.org/bots/api#markdown-stylerY   rm   )setattrr>   r?   	TypeErrortypelowerr   allr}   warningswarnrc   rH   rH   rI   r   L  s   

c                 C   rd   rS   )r   rP   rH   rH   rI   r   \  re   c                 C   r   )N_disable_web_page_previewr   rP   rH   rH   rI   r   `  r   z BaseBot.disable_web_page_previewc                 C   D   |d u rt | dd  d S t|tstdt| t | d| d S )Nr   z+Disable web page preview must be bool, not r   r>   boolr   r   rc   rH   rH   rI   r   d  s
   
c                 C   rd   rS   )r   rP   rH   rH   rI   r   m  re   c                 C   r   )N_protect_contentr   rP   rH   rH   rI   r   q  r   zBaseBot.protect_contentc                 C   r   )Nr   z"Protect content must be bool, not r   rc   rH   rH   rI   r   u  s   
c                 C   rd   rS   )r   rP   rH   rH   rI   r   ~  re   c                 C   s   t | j|S rS   )r   r1   )rF   ro   rH   rH   rI   check_auth_widget  s   zBaseBot.check_auth_widget)T)NN)<__name__
__module____qualname____doc__r   ra   ri   r   r   Stringr   r   asyncioBaseEventLoopAbstractEventLoopIntegerr9   	BasicAuthBooleantypingFloatr_   r   rJ   rN   rQ   propertyr   rX   r   r\   staticmethodr`   r   setterdeleter
contextlibcontextmanagerrh   r1   rl   rW   r   r   rs   r   	InputFilepathlibPathr?   r~   r   FileIOr   r   r   r   r   r   r   rH   rH   rH   rI   r      s    
	

Y



"

	


	
?








r   )!r   r   r~   r   r   r%   r   r   contextvarsr   r   r   r   r   r   r9   r6   aiohttp.helpersr    r
   r   r   typesr   r   utilsr   utils.auth_widgetr   utils.deprecatedr   r   rH   rH   rH   rI   <module>   s(    