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/blueprints/widgets/__pycache__/bw_tasks.cpython-310.pyc
o

�we�@s�ddlZddlZddlZddlZddlmZmZddlm	Z	ddl
mZddlm
Z
mZmZddlmZddlmZddlmZdd	lmZd
ZdZdZe	e�Ze��d
d��Zejdd��Zejdd��Z dS)�N)�url_for�current_app)�create_celery_app)�db)�CallStorage�InboundCallState�select_voice)�	bw_client)�BandwidthException)�Lead)�Agenti�Qz=https://s3.amazonaws.com/buyercall-static-sounds/dialtone.mp3z1We're sorry, there was an error calling the leed.c
Ks�tjtjdtjdd�}ddlm}m}t�d�t	j
�t	j|k��
�}|s1t�d�|��dSdd	lm}m}|j
�|j|jk��
�}|jj}	|	sP|jjj}	|	jrat�d
�|jj|j��dS|jdvrmt�d�dSt||j�}
|
��d|
_d
d�t|jj dd�d�D�}|s�t�d�dSd|_|j!d7_!t"j#�$�|jj%dr�t&|j�dS|jj%dr�t'�(|�t)|j*d�}||�}
z|
�+||j|�WdSt,y�}zt�-t.�/��d|_t"j#�$�|||j�WYd}~dSd}~ww)N�REDIS_CONFIG_URL�REDIS_CONFIG_PORT��host�port�)�BandwidthRouting�send_notify_emailzTrying to call lead again...zLead {} not available.r��Partnership�PartnershipAccountz5Partnership account {} ({}) has exceeded their quota.)�ringing�	completedz!Lead is currently in another callcSsg|]	}|jjr|j�qS�)�agent�
available_now�agent_id)�.0�asgrr�G/home/arjun/projects/buyercall/buyercall/blueprints/widgets/bw_tasks.py�
<listcomp>Is��zcall_again.<locals>.<listcomp>cSs|jS)N)�order)�xrrr �<lambda>Kszcall_again.<locals>.<lambda>)�keyz*No agents were configured for this widget.r�routeSimultaneously�
routeRandomly�voice�missed)0�redis�StrictRedis�celery�conf�routingrr�log�infor�query�filter�id�first�warning�format�'buyercall.blueprints.partnership.modelsrr�partnership_account_id�partnership_account�subscription�partnership�usage_over_limit�name�status�debugr�inc_callback_cntr�sorted�widget�assignments�
call_countr�session�commit�options�
call_parallel�random�shuffler	�partnership_id�route_sequential�	Exception�error�	traceback�
format_exc)�lead_id�kwargs�redis_db�Routingr�leadrr�partner_accountr:�storage�	agent_ids�client�r�errr �
call_again sf
�
�

�




��r\cKs�tjtjdtjdd�}ddlm}m}m}t�	d�t
j�t
j
��t
j|k���}|j
}ddlm}m}	|	j�|	j|jk���}
||�}td	t|��|sgt�d
�|j��d|_tj��|||j
�t||�}tj |_!|�"�d�|�}
d
�|�}d�|�}|�#|dt$d�|�#|dt$d�t%j�t%j�&|���'�}t�	d�t(|���d}t)|
j*d�}||�}|D]�}zi|�+d�|j��}t�	d�|��d�|�}
|�,|
dd�}t�	d�|��|�-|||�}|�.��.|j/�r|j/t|j�k�r|�0�	Wd�Wn@|�1|
d�|j|��Wd�n	1�swYWq�t2�yG}zt�t3�4��t�d�|j��WYd}~q�d}~ww|du�r^|�#|dt$d�d|_tj��|�5|
dt$�dS)Nr
rrr)rr�get_available_agent_idszParallel call task started.rrzAgent ids: z&No agents are available for widget {}.r)zLIST{}zSUCC{}z	CONNECT{}��0zCalling {} agents...r(zBUSY_{}zagent busy: {}���zThe calls are: {}z{}_{}zError calling agent {}...z-1)6r*r+r,r-r.rrr]r/r0rr1�joinrBr2r3�oner7rrr8r4�print�strr5r6r>rrErFr�State�NEW�state�clear_retry_cnt�setex�DAYSr�in_�all�lenr	rK�get�lrange�call_agent_parallel�lockr�hangup�lpushrMrOrP�expire)rQrRrSrTrr]rUrBrrrVrXrW�called_agents_key�lead_capture_key�connect_key�agents�callrYrZr�
agent_busy�callsr[rrr rHjsv
�






�������

rHc
Ks�tjtjdtjdd�}t||�}tj�tj��	tj
|k���}|j}ddlm
}m}|j�	|j
|jk���}	|j�	|j
|	jk���}
t|
j
�}|j��}z|j|jtdd�Wntyjt�t���Ynwz&|j|jj|jt d|j
|d	d
d�d|j!d
�}
|
j
|_"|
j
|_"t#j$�%�WdSty�t�t���|jj&�'dd�}t(|�\}}}|j|jjdd�|j|jjt)d|||d�YdSw)z; Play dial tone on the agent's side and call the lead.
    r
rrrr�true)�file_url�loop_enabledz bw_outbound.lead_status_callbackT�https)rQr�	_external�_scheme�POST)�from_�to�callback_url�callback_http_method�
conference_id�language�en�)r}�lead_call_error)�sentence�tag�gender�localer(N)*r*r+r,r-rrr1rarBr2r3rbr7rrr8r4rKr	�
agent_call_id�decoder{�audio�
DIAL_TONE_URLr
r/rNrOrPry�inbound�phonenumberr�
bw_conf_id�call_sidrrErFrGrnr�MSG_LEAD_CALL_ERROR)rQrrRrSrWrUrBrrrV�partnerrY�agent_call_id_decoded�	call_leadr�r�r�r(rrr �bw_call_lead�sb
�



������r�)!�loggingr/rIrOr*�flaskrr�
buyercall.appr�buyercall.extensionsr�buyercall.lib.util_twiliorrrer�buyercall.lib.util_bandwidthr	�buyercall.lib.bandwidthr
�!buyercall.blueprints.leads.modelsr�"buyercall.blueprints.agents.modelsrrjr�r�r,�taskr\rHr�rrrr �<module>s,
I
Q