o
    Ó:j>9  ã                   @   sN  d dl Z d dlmZ d dlZd dlmZmZ d dlmZ d dl	m
Z
 e  e¡ZG dd„ dƒZ		dded	eeeef  d
ededB dedB deeef fdd„Zd	eeeef  deeef fdd„Zd	eeeef  deeef fdd„Z		dded
ededB dedB deeef f
dd„Zdededeeef deeef fdd„ZdS )é    N)ÚAny)ÚHTTPExceptionÚstatus)ÚSettings)ÚFreshchatAPIErrorc                   @   sÂ   e Zd Zddedededdfdd„Zd	ed
edeeef fdd„Z		dd	ed
edee dededeeef fdd„Z		dd	ed
edee dedeeef f
dd„Z
d	edeeef fdd„ZdS )ÚFreshchatClientÚbotÚbase_urlÚtokenÚ
actor_typeÚreturnNc                 C   s   |  d¡| _|| _|| _d S )Nú/)Úrstripr	   r
   r   )Úselfr	   r
   r   © r   úF/var/www/html/macrobase-freshchat-api/app/services/freshchat_client.pyÚ__init__   s   
zFreshchatClient.__init__Úconversation_idÚtextc           	   
   Ã   sX  �| j › d|› d�}dd|iigd| jdœ}d| j› �dd	œ}t d
|| j¡ z-tjdd�4 I d H š}|j|||d�I d H }W d   ƒI d H  n1 I d H sNw   Y  W n tjyk } z
t 	d¡ t
dƒ|‚d }~ww |jdk sv|jdkr�t d|j|jd d… ¡ t
d|j|jd d… d�‚|js—ddiS z| ¡ W S  ty«   d|jdœ Y S w )Nú/conversations/ú	/messagesr   ÚcontentÚnormal)Úmessage_partsÚmessage_typer   úBearer úapplication/json©ÚAuthorizationzContent-Typez?Sending Freshchat text message conversation_id=%s actor_type=%sé
   ©Útimeout©ÚheadersÚjsonz9Freshchat API connection error while sending text messageú8No fue posible conectar con Freshchat Conversations API.éÈ   é,  z@Freshchat API send message error status_code=%s response_body=%siè  z0Freshchat Conversations API respondio con error.iÐ  ©Ústatus_codeÚresponse_bodyr   Úsent©r   Úraw_response)r	   r   r
   ÚloggerÚinfoÚhttpxÚAsyncClientÚpostÚ	HTTPErrorÚ	exceptionr   r)   Úerrorr   r   r$   Ú
ValueError)	r   r   r   ÚurlÚpayloadr#   ÚclientÚresponseÚexcr   r   r   Úsend_text_message   sT   €ÿÿÿ÷
þ(ÿ€

€þýý
ÿz!FreshchatClient.send_text_messageõ   Selecciona una opciÃ³nÚsnakeÚbuttonsÚ	help_textÚdisplay_order_stylec              
   ƒ   sÎ  �dd„ |D ƒd d… }|st dƒ‚|r| ¡ nd}|sd}|dkr$dnd‰ | j› d	|› d
�}dd|iig}	|	 ‡ fdd„t|dd�D ƒ¡ dd|iigdd|	iigd| jdœ}
d| j› �ddœ}t d|| j|
¡ z-t	j
dd�4 I d H š}|j|||
d�I d H }W d   ƒI d H  n1 I d H s‘w   Y  W n t	jy® } z
t d¡ t dƒ|‚d }~ww |jdk s¹|jd krËt d!|j|j¡ t d"|j|jd#�‚|jsÒd$d%iS z| ¡ W S  tyæ   d%|jd&œ Y S w )'Nc                 S   ó    g | ]}|r|  ¡ r|  ¡ ‘qS r   ©Ústrip)Ú.0Úbuttonr   r   r   Ú
<listcomp>J   ó     z<FreshchatClient.send_quick_reply_message.<locals>.<listcomp>é   z1Debe enviar al menos un boton para quick replies.r=   ÚcamelÚdisplayOrderÚdisplay_orderr   r   r@   r   c                 3   s$   � | ]\}}d d|ˆ |iiV  qdS )Úquick_reply_buttonÚlabelNr   )rE   ÚindexrF   ©Ú	order_keyr   r   Ú	<genexpr>Z   s   € ûþÿ
ÿz;FreshchatClient.send_quick_reply_message.<locals>.<genexpr>é   ©Ústartr   Ú
collectionÚ	sub_partsr   ©r   Úreply_partsr   r   r   r   r   zQSending Freshchat quick reply message conversation_id=%s actor_type=%s payload=%sr   r    r"   z:Freshchat API connection error while sending quick repliesr%   r&   r'   zAFreshchat API quick replies error status_code=%s response_body=%szHFreshchat Conversations API respondio con error al enviar quick replies.r(   r   r+   r,   )r   rD   r	   ÚextendÚ	enumerater   r
   r.   r/   r0   r1   r2   r3   r4   r)   r5   r   r   r$   r6   )r   r   r   r?   r@   rA   Úclean_buttonsÚclean_help_textr7   rW   r8   r#   r9   r:   r;   r   rP   r   Úsend_quick_reply_messageB   s†   €ÿÿÿ
ùÿÿÿ	ÿÿÿð
þü(ÿ€

€þýý
ÿz(FreshchatClient.send_quick_reply_messageúSelecciona una opcionÚoptionsc              
   Ã   s¤  �dd„ |D ƒ}|st dƒ‚|r| ¡ nd}|sd}| j› d|› d�}dd|iigd	d
|dd„ t|dd�D ƒdœgdœigd| jdœ}d| j› �ddœ}	t d|| j|¡ z-tj	dd�4 I d H š}
|
j
||	|d�I d H }W d   ƒI d H  n1 I d H s|w   Y  W n tjy™ } z
t d¡ t dƒ|‚d }~ww |jdk s¤|jdkr¶t d|j|j¡ t d|j|jd�‚|js½d d!iS z| ¡ W S  tyÑ   d!|jd"œ Y S w )#Nc                 S   rB   r   rC   )rE   Úoptionr   r   r   rG   ¤   rH   z9FreshchatClient.send_dropdown_message.<locals>.<listcomp>z1Debe enviar al menos una opcion para el selector.r_   r   r   r   r   Útemplate_contentÚquick_reply_dropdownc                 S   s"   g | ]\}}d ||||dœi‘qS )rM   ©rN   r8   Úcustom_reply_textrL   r   ©rE   rO   ra   r   r   r   rG   »   s    	ùüÿÿrS   rT   ©ÚnameÚparts©ÚtypeÚsectionsr   rX   r   r   r   zNSending Freshchat dropdown message conversation_id=%s actor_type=%s payload=%sr   r    r"   z5Freshchat API connection error while sending dropdownr%   r&   r'   z<Freshchat API dropdown error status_code=%s response_body=%szCFreshchat Conversations API respondio con error al enviar selector.r(   r   r+   r,   )r   rD   r	   r[   r   r
   r.   r/   r0   r1   r2   r3   r4   r)   r5   r   r   r$   r6   )r   r   r   r`   r@   Úclean_optionsr]   r7   r8   r#   r9   r:   r;   r   r   r   Úsend_dropdown_message�   s€   €ÿÿÿ	
	÷þÿþÿÿà
#þü(ÿ€

€þýý
ÿz%FreshchatClient.send_dropdown_messagec              
   Ã   s2  �| j › d|› �}ddi}d| j› �ddœ}t d|¡ z-tjdd	�4 I d H š}|j|||d
�I d H }W d   ƒI d H  n1 I d H sCw   Y  W n tjy` } z
t d¡ t	dƒ|‚d }~ww |j
dk sk|j
dkr}t d|j
|j¡ t	d|j
|jd�‚|js„ddiS z| ¡ W S  ty˜   d|jdœ Y S w )Nr   r   Úresolvedr   r   r   z3Resolving Freshchat conversation conversation_id=%sr   r    r"   z;Freshchat API connection error while resolving conversationr%   r&   r'   zHFreshchat API resolve conversation error status_code=%s response_body=%szJFreshchat Conversations API respondio con error al cerrar la conversacion.r(   r,   )r	   r
   r.   r/   r0   r1   Úputr3   r4   r   r)   r5   r   r   r$   r6   )r   r   r7   r8   r#   r9   r:   r;   r   r   r   Úresolve_conversationõ   sF   €
þ(ÿ€

€þýý
ÿz$FreshchatClient.resolve_conversation)r   )r=   r>   )r_   )Ú__name__Ú
__module__Ú__qualname__Ústrr   Údictr   r<   Úlistr^   rn   rq   r   r   r   r   r      s>    4úþýüûú

ù`ûþýüû

úXr   r   r`   r   Úactor_idÚuser_idr   c                 C   sT   t |ƒdkrt|ƒgnt|ƒg}d|dd| iig|dœ}|r"||d< |r(||d< |S )NrI   r   r   r   )r   r   r   rY   rx   ry   )ÚlenÚ_build_dropdown_reply_partÚ_build_buttons_reply_part)r   r`   r   rx   ry   rY   r8   r   r   r   Ú!build_quick_reply_message_payload  s    ür}   c                 C   s   dddd„ t | dd�D ƒiiS )NrV   rW   c                 S   ó2   g | ]\}}d |d |d |d t |ƒdœi‘qS ©rM   ÚtitleÚidrd   ©ru   rf   r   r   r   rG   5  ó    	ùüÿÿz-_build_buttons_reply_part.<locals>.<listcomp>rS   rT   ©r[   ©r`   r   r   r   r|   2  s   
	÷ÿÿr|   c                 C   s(   ddddd„ t | dd�D ƒdœgd	œiS )
Nrb   rc   ÚOpcionesc                 S   r~   r   r‚   rf   r   r   r   rG   K  rƒ   z._build_dropdown_reply_part.<locals>.<listcomp>rS   rT   rg   rj   r„   r…   r   r   r   r{   D  s   
	÷þÿþÿr{   c                 C   s2   d|dd| iigdœ}|r||d< |r||d< |S )Nr   r   r   )r   r   r   rx   ry   r   )r   r   rx   ry   r8   r   r   r   Úbuild_text_message_payload\  s   ýr‡   Úsettingsr   r8   c           	   
   C   s4  | j r| jsttjdd�‚| j  d¡}|› d|› d�}d| j› �ddœ}| jr,d	|d
< nd|d
< t d|¡ z t	j
dd��}|j|||d�}W d   ƒ n1 sPw   Y  W n t	jyp } zt d¡ ttjdd�|‚d }~ww |jdkr�t d|j|jd d… ¡ ttjd|j› d�d�‚|js–ddiS | ¡ S )NzlFaltan FRESHWORKS_API_BASE_URL o FRESHWORKS_API_TOKEN para enviar mensajes interactivos desde Freshchat API.)r)   Údetailr   z/v2/conversations/r   r   r   r   ÚtruezASSUME-IDENTITYÚfalsez8Sending Freshchat interactive message conversation_id=%sé   r    r"   zFreshchat API connection errorz7No fue posible conectar con Freshchat Conversation API.i�  z%Freshchat API error status=%s body=%siô  u/   Freshchat Conversation API respondiÃ³ con HTTP Ú.r   r+   )Úfreshworks_api_base_urlÚfreshworks_api_tokenr   r   ÚHTTP_500_INTERNAL_SERVER_ERRORr   Úfreshworks_assume_identityr.   r/   r0   ÚClientr2   r3   r4   ÚHTTP_502_BAD_GATEWAYr)   r5   r   r   r$   )	rˆ   r   r8   r	   r7   r#   r9   r:   r;   r   r   r   Úsend_conversation_messagep  sT   ý
þ
ÿ€
þý€þ
ýþr”   )NN)ÚloggingÚtypingr   r0   Úfastapir   r   Úapp.core.configr   Úapp.services.exceptionsr   Ú	getLoggerrr   r.   r   ru   rw   rv   r}   r|   r{   r‡   r”   r   r   r   r   Ú<module>   sb    
  ûÿþýüû

ú&&üÿþýü

ûÿþ
ý
ü