o
    HEDi                     @   s   d Z ddlZddlmZmZ ddlmZ edZdde	d	e	fd
dZ
dde	d	e	fddZdde	fddZde	d	e	fddZde	d	e	fddZdd ZdS )u  
Deep linking

Telegram bots have a deep linking mechanism, that allows for passing
additional parameters to the bot on startup. It could be a command that
launches the bot — or an auth token to connect the user's Telegram
account to their account on some external service.

You can read detailed description in the source:
https://core.telegram.org/bots#deep-linking

We have add some utils to get deep links more handy.

Basic link example:

    .. code-block:: python

        from aiogram.utils.deep_linking import get_start_link
        link = await get_start_link('foo')

        # result: 'https://t.me/MyBot?start=foo'

Encoded link example:

    .. code-block:: python

        from aiogram.utils.deep_linking import get_start_link

        link = await get_start_link('foo', encode=True)
        # result: 'https://t.me/MyBot?start=Zm9v'

Decode it back example:
    .. code-block:: python

        from aiogram.utils.deep_linking import decode_payload
        from aiogram.types import Message

        @dp.message_handler(commands=["start"])
        async def handler(message: Message):
            args = message.get_args()
            payload = decode_payload(args)
            await message.answer(f"Your payload: {payload}")

    N)urlsafe_b64decodeurlsafe_b64encode   )Botz[^_A-z0-9-]Fpayloadreturnc                       t d| |dI dH S )z
    Get 'start' deep link with your payload.

    If you need to encode payload or pass special characters -
        set encode as True

    :param payload: args passed with /start
    :param encode: encode payload with base64url
    :return: link
    start	link_typer   encodeN_create_linkr   r    r   i/var/www/www-root/data/www/ovozai.pdev.uz/venv/lib/python3.10/site-packages/aiogram/utils/deep_linking.pyget_start_link5      r   c                    r   )a  
    Get 'startgroup' deep link with your payload.

    If you need to encode payload or pass special characters -
        set encode as True

    :param payload: args passed with /start
    :param encode: encode payload with base64url
    :return: link
    
startgroupr
   Nr   r   r   r   r   get_startgroup_linkG   r   r   c                    st   t  I dH }t|tst|}|rt|}tt|r"d}t|t|dkr.d}t|d|j	 d|  d| S )z
    Create deep link.

    :param link_type: `start` or `startgroup`
    :param payload: any string-convertible data
    :param encode: pass True to encode the payload
    :return: deeplink
    NzfWrong payload! Only A-Z, a-z, 0-9, _ and - are allowed. Pass `encode=True` or encode payload manually.@   z)Payload must be up to 64 characters long.zhttps://t.me/?=)
_get_bot_user
isinstancestrencode_payloadresearchBAD_PATTERN
ValueErrorlenusername)r   r   r   botmessager   r   r   r   Y   s   	
r   c                 C   s(   t | } t|  }| }|ddS )z'Encode payload with URL-safe base64url.r    )r   r   r   decodereplace)r   bytes_payloadstr_payloadr   r   r   r   x   s   r   c                 C   s(   | ddt | d   7 } t| }| S )z'Decode payload with URL-safe base64url.r      )r!   r   r&   )r   resultr   r   r   decode_payload   s   r,   c                     s   t  } | jI dH S )zGet current user of bot.N)r   get_currentme)r#   r   r   r   r      s   r   )F)__doc__r   base64r   r   r#   r   compiler   r   r   r   r   r   r,   r   r   r   r   r   <module>   s    ,
