a
    bg                     @   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eed
ddZejdddZeee eedddZeeeeedddZdeeeeeed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   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/infobip.pyvalidate_environment   s    z&InfobipAPIWrapper.validate_environment)r   c                 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textr   c                 C   s   ddd |D ||dgi}|   }|jddi |j| j d|d}| }z |jd	krp|d
 d d W S W n ty   Y dS 0 z|d d d W S  ty   Y dS 0 dS )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 successfullyN)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bodyr   c              
   C   s   zddl m} W n. ty> } ztd|W Y d}~n
d}~0 0 ||||d}||d}|  }	|	jd|ji |	j| j d|d	}
|
	 }z |
j
d
kr|d d d W S W n ty   Y dS 0 z|d d d W S  ty   Y dS 0 dS )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_   sF    


zInfobipAPIWrapper._send_email sms)rD   r-   r)   rC   channelr   c                 C   s   |dkrJ|dkrt d|dkr(t d|dkr8t d| j||g|dS |dkr|dkrbt d|dkrrt d	|dkrt d
|dkrt d| j||||dS t d| dd S )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      s:   


+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   