U
    hsB                     @   sf   d dl Z ddlmZmZ ddlmZ dgdgdgg d	d
gg dgddgg dgdgdZG dd dZdS )    N   )	functionstypes   )utilsz	image/gifz	text/htmlz
audio/mpegzapplication/pdfzapplication/zipz
image/jpegz
image/webpzapplication/x-tgstickerz	video/mp4z	audio/ogg)ZgifarticleaudiocontactfilegeophotoZstickerZvenuevideovoicec                   @   s   e Zd ZdZdd Zdddddddddddddd	d
dZddddddddddd
ddZddddddddddddddddddddddZdddddddddd	ddZdddddddddd	ddZ	dS )InlineBuildera  
    Helper class to allow defining `InlineQuery
    <telethon.events.inlinequery.InlineQuery>` ``results``.

    Common arguments to all methods are
    explained here to avoid repetition:

        text (`str`, optional):
            If present, the user will send a text
            message with this text upon being clicked.

        link_preview (`bool`, optional):
            Whether to show a link preview in the sent
            text message or not.

        geo (:tl:`InputGeoPoint`, :tl:`GeoPoint`, :tl:`InputMediaVenue`, :tl:`MessageMediaVenue`, optional):
            If present, it may either be a geo point or a venue.

        period (int, optional):
            The period in seconds to be used for geo points.

        contact (:tl:`InputMediaContact`, :tl:`MessageMediaContact`, optional):
            If present, it must be the contact information to send.

        game (`bool`, optional):
            May be `True` to indicate that the game will be sent.

        buttons (`list`, `custom.Button <telethon.tl.custom.button.Button>`, :tl:`KeyboardButton`, optional):
            Same as ``buttons`` for `client.send_message()
            <telethon.client.messages.MessageMethods.send_message>`.

        parse_mode (`str`, optional):
            Same as ``parse_mode`` for `client.send_message()
            <telethon.client.messageparse.MessageParseMethods.parse_mode>`.

        id (`str`, optional):
            The string ID to use for this result. If not present, it
            will be the SHA256 hexadecimal digest of converting the
            created :tl:`InputBotInlineResult` with empty ID to ``bytes()``,
            so that the ID will be deterministic for the same input.

            .. note::

                If two inputs are exactly the same, their IDs will be the same
                too. If you send two articles with the same ID, it will raise
                ``ResultIdDuplicateError``. Consider giving them an explicit
                ID if you need to send two results that are the same.
    c                 C   s
   || _ d S )N)_client)selfclient r   D/tmp/pip-unpacked-wheel-c81u5j2r/telethon/tl/custom/inlinebuilder.py__init__F   s    zInlineBuilder.__init__Nr   T<   F)urlthumbcontentidtext
parse_modelink_previewr   periodr	   gamebuttonsc                   sZ   t j|p
dd| j|||	|
||||dI dH |||||d}|dkrVtt| |_|S )a)  
        Creates new inline result of article type.

        Args:
            title (`str`):
                The title to be shown for this result.

            description (`str`, optional):
                Further explanation of what this result means.

            url (`str`, optional):
                The URL to be shown for this result.

            thumb (:tl:`InputWebDocument`, optional):
                The thumbnail to be shown for this result.
                For now it has to be a :tl:`InputWebDocument` if present.

            content (:tl:`InputWebDocument`, optional):
                The content to be shown for this result.
                For now it has to be a :tl:`InputWebDocument` if present.

        Example:
            .. code-block:: python

                results = [
                    # Option with title and description sending a message.
                    builder.article(
                        title='First option',
                        description='This is the first option',
                        text='Text sent after clicking this option',
                    ),
                    # Option with title URL to be opened when clicked.
                    builder.article(
                        title='Second option',
                        url='https://example.com',
                        text='Text sent if the user clicks the option and not the URL',
                    ),
                    # Sending a message with buttons.
                    # You can use a list or a list of lists to include more buttons.
                    builder.article(
                        title='Third option',
                        text='Text sent with buttons below',
                        buttons=Button.url('https://example.com'),
                    ),
                ]
         r   r   r   r   r   r   r	   r   r    N)r   typesend_messagetitledescriptionr   r   r   )r   ZInputBotInlineResult_messagehashlibsha256bytes	hexdigestr   )r   r%   r&   r   r   r   r   r   r   r   r   r   r	   r   r    resultr   r   r   r   J   s,    7   
zInlineBuilder.article)
r   include_mediar   r   r   r   r   r	   r   r    c       
            s   zt |}W np tk
r~   | jj|dddI dH \}}}t|tjrN|}n,| tj	j
t |dI dH }t |j}Y nX tj|pdd|| j|pd||||||	|
|d	I dH d}|dkrtt| |_|S )	a  
        Creates a new inline result of photo type.

        Args:
            include_media (`bool`, optional):
                Whether the photo file used to display the result should be
                included in the message itself or not. By default, the photo
                is included, and the text parameter alters the caption.

            file (`obj`, optional):
                Same as ``file`` for `client.send_file()
                <telethon.client.uploads.UploadMethods.send_file>`.

        Example:
            .. code-block:: python

                results = [
                    # Sending just the photo when the user selects it.
                    builder.photo('/path/to/photo.jpg'),

                    # Including a caption with some in-memory photo.
                    photo_bytesio = ...
                    builder.photo(
                        photo_bytesio,
                        text='This will be the caption of the sent photo',
                    ),

                    # Sending just the message without including the photo.
                    builder.photo(
                        photo,
                        text='This will be a normal text message',
                        include_media=False,
                    ),
                ]
        T)allow_cacheZas_imageNmediar!   r   	r   r   r   r0   r   r   r	   r   r    )r   r#   r   r$   )r   Zget_input_photo	TypeErrorr   _file_to_media
isinstancer   Z
InputPhotor   messagesUploadMediaRequestInputPeerSelfr   ZInputBotInlineResultPhotor'   r(   r)   r*   r+   r   )r   r
   r   r-   r   r   r   r   r   r	   r   r    fh_r0   rr,   r   r   r   r      sD    (  
 
zInlineBuilder.photo)r&   r#   	mime_type
attributesforce_document
voice_note
video_note	use_cacher   r   r   r   r   r   r	   r   r    r-   c                   s4  |dkrN|rd}n0|rBt  D ]"\}}|D ]}||kr*|} qq*q|dkrNd}zt|}W nx tk
r   | jj||||||	|
dI dH \}}}t|tj	r|}n,| t
jjt |dI dH }t|j}Y nX tj|pd||| j|pd||||||||d	I dH ||d}|dkr0tt| |_|S )	a	  
        Creates a new inline result of document type.

        `use_cache`, `mime_type`, `attributes`, `force_document`,
        `voice_note` and `video_note` are described in `client.send_file
        <telethon.client.uploads.UploadMethods.send_file>`.

        Args:
            file (`obj`):
                Same as ``file`` for `client.send_file()
                <telethon.client.uploads.UploadMethods.send_file>`.

            title (`str`, optional):
                The title to be shown for this result.

            description (`str`, optional):
                Further explanation of what this result means.

            type (`str`, optional):
                The type of the document. May be one of: article, audio,
                contact, file, geo, gif, photo, sticker, venue, video, voice.
                It will be automatically set if ``mime_type`` is specified,
                and default to ``'file'`` if no matching mime type is found.
                you may need to pass ``attributes`` in order to use ``type``
                effectively.

            attributes (`list`, optional):
                Optional attributes that override the inferred ones, like
                :tl:`DocumentAttributeFilename` and so on.

            include_media (`bool`, optional):
                Whether the document file used to display the result should be
                included in the message itself or not. By default, the document
                is included, and the text parameter alters the caption.

        Example:
            .. code-block:: python

                results = [
                    # Sending just the file when the user selects it.
                    builder.document('/path/to/file.pdf'),

                    # Including a caption with some in-memory file.
                    file_bytesio = ...
                    builder.document(
                        file_bytesio,
                        text='This will be the caption of the sent file',
                    ),

                    # Sending just the message without including the file.
                    builder.document(
                        photo,
                        text='This will be a normal text message',
                        include_media=False,
                    ),
                ]
        Nr   r
   )r;   r<   r=   r>   r?   r.   r/   r!   r1   )r   r#   documentr$   r%   r&   )_TYPE_TO_MIMESitemsr   Zget_input_documentr2   r   r3   r4   r   ZInputDocumentr   r5   r6   r7   rA   ZInputBotInlineResultDocumentr'   r(   r)   r*   r+   r   )r   r
   r%   r&   r#   r;   r<   r=   r>   r?   r@   r   r   r   r   r   r   r	   r   r    r-   tyZmimesmimer8   r9   r0   r:   r,   r   r   r   rA      sf    A	
 

zInlineBuilder.document)	r   r   r   r   r   r   r	   r   r    c       	            sP   t j|p
d|| j|||||||	|
dI dH d}|dkrLtt| |_|S )z
        Creates a new inline result of game type.

        Args:
            short_name (`str`):
                The short name of the game to use.
        r!   r"   N)r   
short_namer$   )r   ZInputBotInlineResultGamer'   r(   r)   r*   r+   r   )r   rF   r   r   r   r   r   r   r	   r   r    r,   r   r   r   r   `  s"       
zInlineBuilder.gamer1   c       	      	      s|  |dkrdn||||f}
t dd |
D dkrZtdddd td	 |
D pTd
| jj|	dd}|d k	r| j||I d H \}}|rt	j
|||dS t	j|| ||dS nt|t	jt	jfrt	jt|||dS t|t	jt	jfr.t|t	jr|j}n|j}t	j||j|j|j|j|j|dS t|t	jt	jfr^t	j|j|j |j!|j"|dS |rpt	j#|dS tdd S )Nr!    c                 s   s"   | ]}|d k	r|dk	rdV  qd S )NF   r   .0xr   r   r   	<genexpr>  s       z)InlineBuilder._message.<locals>.<genexpr>rH   z;Must set exactly one of text, geo, contact or game (set {})z, c                 s   s   | ]}|d  r|d V  qdS )rH   r   Nr   rI   r   r   r   rL     s    ztext geo contact gamenoneT)Zinline_only)messageentitiesreply_markup)rN   Z
no_webpagerO   rP   )	geo_pointr   rP   )rQ   r%   addressprovidervenue_id
venue_typerP   )phone_number
first_name	last_namevcardrP   )rP   z+No text, game or valid geo or contact given)$sum
ValueErrorformatjoinzipsplitr   Zbuild_reply_markupZ_parse_message_textr   ZInputBotInlineMessageMediaAutoZInputBotInlineMessageTextr4   ZInputGeoPointZGeoPointZInputBotInlineMessageMediaGeor   Zget_input_geoZInputMediaVenueZMessageMediaVenuerQ   r   ZInputBotInlineMessageMediaVenuer%   rR   rS   rT   rU   ZInputMediaContactZMessageMediaContactZ!InputBotInlineMessageMediaContactrV   rW   rX   rY   ZInputBotInlineMessageGame)r   r   r   r   r0   r   r   r	   r   r    argsZmarkupZmsg_entitiesrQ   r   r   r   r'   |  s     
 	 zInlineBuilder._message)N)N)
__name__
__module____qualname____doc__r   r   r   rA   r   r'   r   r   r   r   r      s   0          N       M             ~             r   )r(   r!   r   r   r   rB   r   r   r   r   r   <module>   s   