
    Ng                     ~    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	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                   &   e Zd ZU dZdZee         ed<   dZee         ed<    e	d          Z
 ed	          ed
edefd                        Zdej        fdZdedee         dedefdZdededededef
dZ	 	 	 	 	 ddedededededef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                 V    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   s     a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utilities/infobip.pyvalidate_environmentz&InfobipAPIWrapper.validate_environment   sI     %9%'8%
 %
 ! &:&(:&
 &
!"     c                     t          ddg d          }t          |          }t          j                    }|                    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sessions       r   _get_requests_sessionz'InfobipAPIWrapper._get_requests_session"   s     %666!
 !
 !

  +~FFF"$$j'***!>(<!>!>; 	
 	
 	
 r   senderdestination_phone_numberstextc                    dd |D             ||dgi}|                                  }|j                            ddi           |                    | j         d|          }|                                }	 |j        dk    r|d	         d
         d         S n# t          $ r Y dS w xY w	 |d         d         d         S # t          $ r 	 Y dS w xY w)zSend an SMS message.messagesc                     g | ]}d |iS )to ).0destinations     r   
<listcomp>z/InfobipAPIWrapper._send_sms.<locals>.<listcomp><   s*     % % %0;{+% % %r   )destinationsfromr3   Content-Typezapplication/jsonz/sms/2/text/advanced)json   requestErrorserviceExceptionr3   Failed to send messager   	messageIdECould not get message ID from response, message was sent successfully)r0   r*   r+   postr   r?   status_codeKeyError)r,   r1   r2   r3   r?   r/   responseresponse_jsons           r   	_send_smszInfobipAPIWrapper._send_sms5   sM   
 % %?X% % % #  

 %)$>$>$@$@ 2	
 	
 	
 '.ll$::: '3 '
 '

 'mmoo	,#s**$^45GHPP + 	, 	, 	,+++	,	 ,Q/<< 	 	 	W  	s$   8B 
B&%B&*B> >CC
from_emailto_emailsubjectbodyc                    	 ddl m} n"# t          $ r}t          d          |d}~ww xY w||||d} ||          }|                                 }	|	j                            d|j        i           |	                    | j         d|	          }
|
	                                }	 |
j
        d
k    r|d         d         d         S n# t          $ r Y dS w xY w	 |d         d         d         S # t          $ r 	 Y dS w xY w)zSend an email message.r   )MultipartEncoderz^Unable to import requests_toolbelt, please install it with `pip install -U requests-toolbelt`.N)r=   r7   rN   r3   )fieldsr>   z/email/3/send)datar@   rA   rB   r3   rC   r5   rD   rE   )requests_toolbeltrQ   ImportErrorr0   r*   r+   content_typerF   r   r?   rG   rH   )r,   rL   rM   rN   rO   rQ   e	form_datarS   r/   rI   rJ   s               r   _send_emailzInfobipAPIWrapper._send_email_   s   
	::::::: 	 	 	6  	 	
 
	  y111$($>$>$@$@ 1	
 	
 	
 '.ll$333 '3 '
 '

 'mmoo	,#s**$^45GHPP + 	, 	, 	,+++	,	 ,Q/<< 	 	 	W  	s4   	 
(#('C 
CCC- -C<;C< smsr7   channelc                    |dk    rX|dk    rt          d          |dk    rt          d          |dk    rt          d          |                     ||g|          S |dk    rm|dk    rt          d          |dk    rt          d	          |dk    rt          d
          |dk    rt          d          |                     ||||          S t          d| d          )Nr[   rZ   z)Sender must be specified for SMS messagesz.Destination must be specified for SMS messagesz'Body must be specified for SMS messages)r1   r2   r3   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)rL   rM   rN   rO   zChannel z is not supported)
ValueErrorrK   rY   )r,   rO   r7   r1   rN   r\   s         r   runzInfobipAPIWrapper.run   s/    e|| !LMMMRxx !QRRRrzz !JKKK>>+-$ "   
 || !NOOORxx !STTT"}} !OPPPrzz !LMMM##!	 $    BBBBCCCr   )rZ   rZ   rZ   rZ   r[   )__name__
__module____qualname____doc__r   r   str__annotations__r   r
   model_configr   classmethodr   r   r   r'   r(   r0   r   rK   rY   r`   r8   r   r   r   r      s        00%)OXc])))&?hsm???:  L _(###$ 3    [ $#x'7    &((6:3i(GJ(	( ( ( (T..),.7:.BE.	. . . .d +D +D+D +D 	+D
 +D +D 
+D +D +D +D +D +Dr   r   )rd   typingr   r   r   r   r'   langchain_core.utilsr   pydanticr	   r
   r   requests.adaptersr   urllib3.utilr   r   r8   r   r   <module>rn      s    + + , , , , , , , , , , , ,  5 5 5 5 5 5 ; ; ; ; ; ; ; ; ; ; ) ) ) ) ) )      nD nD nD nD nD	 nD nD nD nD nDr   