HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall/buyercall/lib/__pycache__/util_webhooks.cpython-310.pyc
o

�we�4�@s~ddlZddlZddlZddlmZddlmZddlmZddl	m
Z
ddlmZddl
mZe�e�ZGdd	�d	e�ZdS)
�N)�ceil)�and_)�Lead)�Message)�Webhook)�dbc@s\eZdZgd�Zgd�ZddiZdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�ZdS)�WebhookUtil)�operational_start_call�operational_agent_call�operational_end_call�mobile_start_call�mobile_end_call)�operational_send_message�operational_receive_message�mobile_send_message�mobile_receive_messagezContent-Typezapplication/jsonc	Cs�d}d}d}d}d}d}d}	|jr+t|j�tkr%t�d�|j��|j}n|j�d�}|jr?t|j�tkr9|j}n|j�d�}||jv�rk|j	dur[t|j	�tkrU|j	}n|j	�d�}|j
durqt|j
�tkrk|j
}n|j
�d�}|dkr�ddlm}
|
j
�|
j|jk���}|j}n|j}|jdur�t|j
�tkr�|jd}
nttt|j�d	��}
t�d
�|j��t�d�t|j���t�d�|
��n|j}
|jr�|�|j�r�d
}|jr�|jj�dd�r�d
}|�r|j�rddlm}||j�}|�r|d�r|d�rddlm}||d|dd�}	id|j�d|�d|�d|�d|j �d|j!�d|j"�d|j#�d|j�d|j$�d|j%�d|
�d |�d!|�d"|j&�d#|�d$|	�|j|j'|j(||j)|j*d%d&��}t�d'�||j#��|S||j+v�r�id|j�d|�d|�d|�,|j��d|j#�d(|j-�d)|j.�d*|j/�d+|j0�d,|j(�d-|j1�d.|j2�d/|j3�d|j�d0|j)�d1|�d2d%�}t�d'�||j#��|St�4d3|d4�|S)5zm
        Creates the JSON payload object based on webhook event type.

        :return: JSON Payload
        NFzThe date is {}z%Y-%m-%d %H:%M:%Srr)�Endpoint�<gN@zThe duration is {}zThe duration type is {}z The call duration is: {} minutesT�recordCalls�)�get_recording_url_details�key�bucket)�generate_presigned_aws_url�id�
created_on�
updated_on�agent_id�call_source�progress_status�	caller_id�partnership_account_id�phonenumber_id�phonenumber�my_phone�duration�	starttime�endtime�	call_type�recording_enabled�
recording_url�phone)�widget_guid�
call_count�status�
event_type�originating_number�missed_call_cause�typezIA webhook was sent for the following event: {} and partner account id: {}�to�from�	body_text�	media_urlr.�
delivery_code�delivery_description�	directionr0r/r2zUnknown event type "z." provided. Cannot build webhook JSON payload.)5rr2�str�log�info�format�strftimer�
call_typesr&r'�"buyercall.blueprints.mobile.modelsr�query�filter�
inbound_id�firstrr%�intr�float�is_recordedr,�widget�options�getr*�buyercall.lib.util_boto3_s3rrrrrr r!r#r$r(r-r.r0r1�
message_types�	get_agentr3�from_r5r6r7r8r9�error)�selfr/�contact_object�payloadrrr&r'r)r*r�endpointr�duration_minr�recording_detailsr�rV�=/home/arjun/projects/buyercall/buyercall/lib/util_webhooks.py�populate_payloads



��������	�
���
������!
����������	�
��
������
��zWebhookUtil.populate_payloadcC�tj�tj|k���}|S�N)rrArBrrD)rP�lead_id�lead_objrVrVrW�get_lead��zWebhookUtil.get_leadcCrYrZ)rrArBrrD)rP�
message_id�message_objrVrVrW�get_message�r^zWebhookUtil.get_messagecCsNddlm}|j�|j|k���}|r%|j�d�r%|j�d�}|dur%dSdS)Nr��PhonerTF)�(buyercall.blueprints.phonenumbers.modelsrcrArBrrD�routing_configrJ)rPr"rcr+�recordedrVrVrWrG�szWebhookUtil.is_recordedcCs�ddlm}d}|j�|j|k���}|rW|j�d�dkr%|j�d�g}n|j�d�dkr4|j�d�}nd	S|rW|D]}|�d
t��D]}|drU|drUt	|d�}nqCq:|S)Nrrb����routingType�default�defaultRouting�digit�
digitRoutingsr�agentsr)
rdrcrArBrrDrerJ�listrE)rPr"rc�result_agent_idr+�routings�r�agentrVrVrWrM�s"�zWebhookUtil.get_agentcCsbddlm}|j�|j|k���}|r/|jr/tj�t	��t
t	j|jkt	jdkt	jdk����}|S)Nr)�PartnershipAccountrF)
�'buyercall.blueprints.partnership.modelsrsrArBrrD�partnership_idr�sessionrr�webhook_url�is_deactivated)rPr!rs�partnership_account�retrieved_webhookrVrVrW�get_webhook�s �
���zWebhookUtil.get_webhookc

Cs>d}d}d}z|||jvr|dur|�|�}n||jvr$|dur$|�|�}|durz|r}|�|j�}|r�|jrm|j}t�d�	|j
��|�||�}|jrP|j|j
d<t�d|�dt�|����tj|t�|�|j
d�}WdSt�d	�	||j��WdSWdSWdSWdSty�}	zt�d
�	||	��WYd}	~	dSd}	~	ww)zb
        Executes webhook based on event_type. operational_end_call

        :return: N/A
        rNTz*The medium triggered created on date is {}zSecurity-Tokenz
end_point z
 , data : )�url�data�headerszKWebhook "{}" event found for partnership account {} but no webhook URL set.z-Error executing webhook event "{}". Error: {})r?r]rLrar{r!rwr;r<r=rrX�security_token�header_content�json�dumps�requests�postrO�	Exception)
rPr/�	medium_id�post_end_point�medium_triggered�
post_eventrzrRrq�erVrVrW�trigger_generic_webhook�s8


��
 ��z#WebhookUtil.trigger_generic_webhookN)
�__name__�
__module__�__qualname__r?rLr�rXr]rarGrMr{r�rVrVrVrWrsr)�loggingr�r��mathr�
sqlalchemyr�!buyercall.blueprints.leads.modelsr�buyercall.blueprints.sms.modelsr�$buyercall.blueprints.webhooks.modelsr�buyercall.extensionsr�	getLoggerr�r;�objectrrVrVrVrW�<module>s