o
    Rh*                     @   s<  d 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mZmZmZmZmZmZmZmZmZ ddlmZmZmZ ddlmZ ddlmZ eed	r\d
d	 Zndd	 ZeddedG dd de Z!eddedG dd de Z"eddedG dd de Z#eddedG dd de Z$dS )zlinebot.webhook module.    N   )InvalidSignatureError)MessageEventFollowEventUnfollowEvent	JoinEvent
LeaveEventPostbackEventBeaconEventAccountLinkEventMemberJoinedEventMemberLeftEventThingsEventUnsendEventVideoPlayCompleteEventUnknownEvent)LOGGERPY3safe_compare_digest)
deprecated)LineBotSdkDeprecatedIn30compare_digestc                 C   s   t | |S ae  compare_digest function.

        If hmac module has compare_digest function, use it.
        Or not, use linebot.utils.safe_compare_digest.

        :param val1: string or bytes for compare
        :type val1: str | bytes
        :param val2: string or bytes for compare
        :type val2: str | bytes
        :rtype: bool
        :return: result
        )hmacr   val1val2 r   Q/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/linebot/webhook.pyr   1   s   c                 C   s
   t | |S r   )r   r   r   r   r   r   @   s   
zUse 'from linebot.v3.webhook import SignatureValidator' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.z3.0.0)reasonversioncategoryc                   @   s    e Zd ZdZdd Zdd ZdS )SignatureValidatorzkSignature validator.

    https://developers.line.biz/en/reference/messaging-api/#signature-validation
    c                 C   s   | d| _dS )V__init__ method.

        :param str channel_secret: Channel secret (as text)
        utf-8N)encodechannel_secretselfr&   r   r   r   __init__W   s   zSignatureValidator.__init__c                 C   s2   t | j|dtj }t|dt	|S )zCheck signature.

        :param str body: Request body (as text)
        :param str signature: X-Line-Signature value (as text)
        :rtype: bool
        r$   )
r   newr&   r%   hashlibsha256digestr   base64	b64encode)r(   body	signaturegen_signaturer   r   r   validate^   s   zSignatureValidator.validateN)__name__
__module____qualname____doc__r)   r3   r   r   r   r   r"   P   s    r"   zUse 'from linebot.v3.webhook import WebhookPayload' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.c                   @   s   e Zd ZdZdddZdS )WebhookPayloadz_Webhook Payload.

    https://developers.line.biz/en/reference/messaging-api/#request-body
    Nc                 C   s   || _ || _dS )z__init__ method.

        :param events: Information about the events.
        :type events: list[T <= :py:class:`linebot.models.events.Event`]
        :param str destination: User ID of a bot that should receive webhook events.
        Neventsdestination)r(   r:   r;   r   r   r   r)   w   s   
zWebhookPayload.__init__)NN)r4   r5   r6   r7   r)   r   r   r   r   r8   p   s    r8   zUse 'from linebot.v3.webhook import WebhookParser' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.c                   @   s"   e Zd ZdZdd ZdddZdS )	WebhookParserzWebhook Parser.c                 C   s   t || _dS r#   N)r"   signature_validatorr'   r   r   r   r)      s   zWebhookParser.__init__Fc           	      C   s  | j ||std| t|}g }|d D ]}|d }|dkr-|tj||d q|dkr:|t| q|dkrG|t	| q|dkrT|t
| q|d	kra|t| q|d
krn|t| q|dkr{|t| q|dkr|t| q|dkr|t| q|dkr|t| q|dkr|t| q|dkr|t| q|dkr|t| qtd|  |t| q|rt||ddS |S )a  Parse webhook request body as text.

        :param str body: Webhook request body (as text)
        :param str signature: X-Line-Signature value (as text)
        :param bool as_payload: (optional) True to return WebhookPayload object.
        :rtype: list[T <= :py:class:`linebot.models.events.Event`]
            | :py:class:`linebot.webhook.WebhookPayload`
        :param bool use_raw_message: Using original Message key as attribute
        :return: Events list, or WebhookPayload instance
        zInvalid signature. signature=r:   typemessage)use_raw_messagefollowunfollowjoinleavepostbackbeaconaccountLinkmemberJoined
memberLeftthingsunsendvideoPlayCompletezUnknown event type. type=r;   r9   )r>   r3   r   jsonloadsappendr   new_from_json_dictr   r   r   r   r	   r
   r   r   r   r   r   r   r   infor   r8   get)	r(   r0   r1   
as_payloadrA   	body_jsonr:   event
event_typer   r   r   parse   sR   


zWebhookParser.parseN)FF)r4   r5   r6   r7   r)   rX   r   r   r   r   r<      s    r<   zUse 'from linebot.v3.webhook import WebhookHandler' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.c                   @   sd   e Zd ZdZdd ZdddZdd Zdd
dZdddZe	dd Z
edd ZedddZdS )WebhookHandlerzaWebhook Handler.

    Please read https://github.com/line/line-bot-sdk-python#webhookhandler
    c                 C   s   t || _i | _d| _dS r=   )r<   parser	_handlers_defaultr'   r   r   r   r)      s   

zWebhookHandler.__init__Nc                    s    fdd}|S )a  Add handler method.

        :param event: Specify a kind of Event which you want to handle
        :type event: T <= :py:class:`linebot.models.events.Event` class
        :param message: (optional) If event is MessageEvent,
            specify kind of Messages which you want to handle
        :type: message: T <= :py:class:`linebot.models.messages.Message` class
        :rtype: func
        :return: decorator
        c                    s@   t ttfrD ]
}j|  |d q	| S j|  d | S N)r@   )
isinstancelisttuple_WebhookHandler__add_handler)funcitrV   r@   r(   r   r   	decorator   s   z%WebhookHandler.add.<locals>.decoratorr   )r(   rV   r@   re   r   rd   r   add   s   	zWebhookHandler.addc                    s    fdd}|S )zUSet default handler method.

        :rtype: func
        :return: decorator
        c                    s
   |  _ | S N)r\   )rb   r(   r   r   re      s   z)WebhookHandler.default.<locals>.decoratorr   )r(   re   r   rh   r   default   s   zWebhookHandler.defaultFc                 C   s   | j j||d|d}|jD ]H}d}d}t|tr(| |j|jj}| j	|d}|du r9| |j}| j	|d}|du r@| j
}|du rNtd| d  q| ||| qdS )zHandle webhook.

        :param str body: Webhook request body (as text)
        :param str signature: X-Line-Signature value (as text)
        :param bool use_raw_message: Using original Message key as attribute
        T)rT   rA   NzNo handler of z and no default handler)rZ   rX   r:   r^   r    _WebhookHandler__get_handler_key	__class__r@   r[   rS   r\   r   rR   _WebhookHandler__invoke_func)r(   r0   r1   rA   payloadrV   rb   keyr   r   r   handle   s(   


zWebhookHandler.handlec                 C   s   | j ||d}|| j|< d S r]   )rj   r[   )r(   rb   rV   r@   rn   r   r   r   __add_handler  s   zWebhookHandler.__add_handlerc                 C   sH   |  |\}}|s|dkr|||j d S |dkr|| d S |  d S )N   r   )_WebhookHandler__get_args_countr;   )clsrb   rV   rm   has_varargs
args_countr   r   r   __invoke_func  s   
zWebhookHandler.__invoke_funcc                 C   s@   t rt| }|jd ut|jfS t| }|jd ut|jfS rg   )r   inspectgetfullargspecvarargslenargs
getargspec)rb   arg_specr   r   r   __get_args_count&  s
   

zWebhookHandler.__get_args_countc                 C   s   |d u r| j S | j d |j  S )N_)r4   )rV   r@   r   r   r   __get_handler_key/  s   z WebhookHandler.__get_handler_keyrg   )F)r4   r5   r6   r7   r)   rf   ri   ro   ra   classmethodrl   staticmethodrr   rj   r   r   r   r   rY      s    
	


	
rY   )%r7   r.   r+   r   rw   rN   
exceptionsr   models.eventsr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   utilsr   r   r   r   deprecationsr   hasattrr   objectr"   r8   r<   rY   r   r   r   r   <module>   s,   @

B