o
    ȳg                     @   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_url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   INFOBIP_API_KEYr   INFOBIP_BASE_URLr   )clsr    r   a/var/www/html/chatdoc2/venv/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   )self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   r8   status_codeKeyError)r(   r-   r.   r/   r8   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)r6   r1   rG   r/   )fieldsr7   z/email/3/send)datar9   r:   r;   r/   r<   r0   r=   r>   )requests_toolbeltrI   ImportErrorr,   r&   r'   content_typer?   r   r8   r@   rA   )r(   rE   rF   rG   rH   rI   e	form_datarK   r+   rB   rC   r   r   r   _send_email_   sN   


zInfobipAPIWrapper._send_email smsr1   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)NrS   rR   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)rE   rF   rG   rH   zChannel z is not supported)
ValueErrorrD   rQ   )r(   rH   r1   r-   rG   rT   r   r   r   run   s8   zInfobipAPIWrapper.run)rR   rR   rR   rR   rS   )__name__
__module____qualname____doc__r   r   str__annotations__r   r	   model_configr
   classmethodr   r   r   r#   r$   r,   r   rD   rQ   rW   r   r   r   r   r      sb   
 

*
2r   )r[   typingr   r   r   r   r#   langchain_core.utilsr   pydanticr   r	   r
   requests.adaptersr   urllib3.utilr   r   r   r   r   r   <module>   s    