o
    Zh                     @   sp   d Z ddlmZmZmZmZ ddlZddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ G dd	 d	e
ZdS )
z%Util that sends messages via Infobip.    )AnyDictListOptionalNget_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)HTTPAdapter)Retryc                   @   s   e Zd ZU dZdZee ed< dZee ed< e	ddZ
edd	ed
edefddZdejfddZdedee dedefddZdededededef
ddZ					d!dedededededefdd ZdS )"InfobipAPIWrapperz&Wrapper for Infobip API for messaging.Ninfobip_api_keyzhttps://api.infobip.cominfobip_base_urlZforbid)extrabefore)modevaluesreturnc                 C   s$   t |dd|d< t |dd|d< |S )z,Validate that api key exists in environment.r   ZINFOBIP_API_KEYr   ZINFOBIP_BASE_URLr   )clsr    r   \/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/utilities/infobip.pyvalidate_environment   s   z&InfobipAPIWrapper.validate_environmentc                 C   sN   t ddg dd}t|d}t }|d| |jd| j dd	 |S )
z0Get a requests session with the correct headers.      )i  i  i  i  i  )totalbackoff_factorstatus_forcelist)max_retrieszhttps://zApp zinfobip-langchain-community)Authorizationz
User-Agent)r   r   requestsSessionmountheadersupdater   )selfZretry_strategyadaptersessionr   r   r   _get_requests_session"   s   

z'InfobipAPIWrapper._get_requests_sessionsenderdestination_phone_numberstextc                 C   s   ddd |D ||dgi}|   }|jddi |j| j d|d}| }z|jd	kr8|d
 d d W S W n
 tyC   Y dS w z	|d d d W S  tyX   	 Y dS w )zSend an SMS message.messagesc                 S   s   g | ]}d |iqS )tor   ).0destinationr   r   r   
<listcomp><   s    z/InfobipAPIWrapper._send_sms.<locals>.<listcomp>)destinationsfromr+   Content-Typezapplication/jsonz/sms/2/text/advanced)json   requestErrorserviceExceptionr+   Failed to send messager   	messageIdECould not get message ID from response, message was sent successfully)r(   r#   r$   postr   r4   status_codeKeyError)r%   r)   r*   r+   r4   r'   responseresponse_jsonr   r   r   	_send_sms5   s@   

zInfobipAPIWrapper._send_sms
from_emailto_emailsubjectbodyc              
   C   s   zddl m} W n ty } ztd|d}~ww ||||d}||d}|  }	|	jd|ji |	j| j d|d	}
|
	 }z|
j
d
krQ|d d d W S W n
 ty\   Y dS w z	|d d d W S  tyq   	 Y dS w )zSend an email message.r   )MultipartEncoderz^Unable to import requests_toolbelt, please install it with `pip install -U requests-toolbelt`.N)r2   r-   rC   r+   )fieldsr3   z/email/3/send)datar5   r6   r7   r+   r8   r,   r9   r:   )Zrequests_toolbeltrE   ImportErrorr(   r#   r$   content_typer;   r   r4   r<   r=   )r%   rA   rB   rC   rD   rE   eZ	form_datarG   r'   r>   r?   r   r   r   _send_email_   sN   


zInfobipAPIWrapper._send_email smsr-   channelc                 C   s   |dkr%|dkrt d|dkrt d|dkrt d| j||g|dS |dkrR|dkr1t d|dkr9t d	|dkrAt d
|dkrIt d| j||||dS t d| d)NrM   rL   z)Sender must be specified for SMS messagesz.Destination must be specified for SMS messagesz'Body must be specified for SMS messages)r)   r*   r+   emailz+Sender must be specified for email messagesz0Destination must be specified for email messagesz,Subject must be specified for email messagesz)Body must be specified for email messages)rA   rB   rC   rD   zChannel z is not supported)
ValueErrorr@   rK   )r%   rD   r-   r)   rC   rN   r   r   r   run   s8   zInfobipAPIWrapper.run)rL   rL   rL   rL   rM   )__name__
__module____qualname____doc__r   r   str__annotations__r   r	   Zmodel_configr
   classmethodr   r   r   r    r!   r(   r   r@   rK   rQ   r   r   r   r   r      sb   
 

*
2r   )rU   typingr   r   r   r   r    Zlangchain_core.utilsr   Zpydanticr   r	   r
   Zrequests.adaptersr   Zurllib3.utilr   r   r   r   r   r   <module>   s    