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_twilio.cpython-310.pyc
o

�we�7�@sddlZddlZddlZddlZddlmZddlmZddl	m
Z
mZddlm
Z
e�e�ZdZdZdd	�Zd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGd d!�d!e�Z dS)"�N)�current_app)�Client)�	AESCipher�
AESCipherDome)�dbii�Qc	Csvddlm}|}tjd}|�|�}t|�}|r9|�|j�}|�|j�}tj�	dd�r2t
||�}|St
||�}|SdS)Nr��PartnershipCpaasProviders�CRYPTO_SECRET_KEY�USE_TEST_APIFT)�'buyercall.blueprints.partnership.modelsr�app�config�partnership_twilio_credentialsr�decrypt�cpaas_account_id�cpaas_api_token�getr)	�partnership_idr�partner�encrypt_key�
partner_cpaas�cipher�decrypted_user_id�decrypted_token�
twilio_client�r�;/home/arjun/projects/buyercall/buyercall/lib/util_twilio.py�account_clients(

�
��rc
Cs^ddlm}|}tjd}|�|�}t|�}|r-|�|j�}|�|j�}t	|||d�}	|	SdS)Nrrr	)�account_sidT)
rrrr
rrrrrr)
�subaccount_sidrrrrrrrrrrrr�subaccount_client/s

�r cCstddlm}ddlm}tjd}|}|�|�}t|�}|r8|�|j	�}|�|j
�}|�|j�}	||||	�}
|
SdS)Nr��	Bandwidthrr	T)�buyercall.lib.bandwidthr"rrrr
�!partnership_bandwidth_credentialsrrr�cpaas_api_secret�
cpaas_user_id)rr"rr�partnershiprrr�decrypted_secretr�clientrrr�	bw_clientEs"

�r*cCs||}ddlm}ddlm}tjd}|�|�}t|�}|r<|�|j	�}|�|j
�}|�|j�}	|j}
|||	||
�}|SdS)Nrrr!r	T)
rr�buyercall.lib.bandwidth_api_v2r"rr
r$rrr�cpaas_api_username�cpaas_api_password�
cpaas_site_id)rr'rr"rrr�decrypted_account_id�decrypted_api_username�decrypted_api_password�site_idr)rrr�bw_dashboard_client^s&

�r3cCs(d}|dkrd\}}nd\}}|||fS)N�female�es)�es_mx�	esperanza)�en_US�susanr)�language�gender�locale�voicerrr�select_voicexs


r>cCs�ddlm}d�||�}|dkr$t��}|�||�|�|dt�dS|dkrOt��}t|�|�p3d�}|rQ|j	�
|j|k��|j
||i�tj��dSdSdS)Nr)�Leadz
TIME_{}_{}�ringing�zin-progress)�!buyercall.blueprints.leads.modelsr?�format�time�setnx�expire�DAYS�floatr�query�filter�id�update�response_time_secondsr�session�commit)�call_status�agent_id�lead_id�redis_dbr?�time_key�
timestamp_now�timestamp_thenrrr�update_response_time�s 
��rWcCs2|pd}|d}}d|vr|�dd�\}}||fS)z6 Split a name into (first name, last name) tuple.
    �Unknown�� ���split)�name�	full_name�
first_name�	last_namerrr�
split_name�s

rbc@s4eZdZdZedd��Zedd��Zedd��ZdS)	�
BusyAgentszE Track the agents that are currently busy in a call with a user.
    cs,tjtjdtjdd���fdd�|D�S)zv Return only those agents from `all_agents` who are not busy.

        :param all_agents: a list of agent ids
        �REDIS_CONFIG_URL�REDIS_CONFIG_PORT��host�portcs g|]}��d�|��s|�qS)�BUSY_{})rrC)�.0rQ�rSrr�
<listcomp>�s��z*BusyAgents.filter_from.<locals>.<listcomp>)�redis�StrictRedisrr
)�
all_agentsrrkr�filter_from�s
�zBusyAgents.filter_fromcCs2tjtjdtjdd�}|�d�|�dd�dS)z Mark an agent as busy.rdrerfriiXr[N)rmrnrr
�setexrC�rQrSrrr�add�szBusyAgents.addcCs.tjtjdtjdd�}|�d�|��dS)z Mark an agent as available.rdrerfriN)rmrnrr
�deleterCrrrrr�remove�szBusyAgents.removeN)�__name__�
__module__�__qualname__�__doc__�staticmethodrprsrurrrrrc�s

	rcc@s$eZdZdd�Zdd�Zdd�ZdS)�	RedisAttrcCs
||_dS�N)�_RedisAttr__name��selfr^rrr�__init__�s
zRedisAttr.__init__cCsJt�d�|j|��|dur|j�|j|j�dS|j�|j|j|�dS)Nz{} -> {})�log�debugrCr}rS�hdel�	_lead_key�hset)r�instance�valuerrr�__set__�szRedisAttr.__set__cCs|j�|j|j�Sr|)rS�hgetr�r})rr��ownerrrr�__get__��zRedisAttr.__get__N)rvrwrxr�r�r�rrrrr{�sr{cs(eZdZ�fdd�Z�fdd�Z�ZS)�BoolAttrcs&t|�rdnd}tt|��||�dS)N�xrY)�bool�superr�r�)rr�r��redis_value��	__class__rrr��szBoolAttr.__set__cstt|��||�}t|�Sr|)r�r�r�r�)rr�r�r�r�rrr��szBoolAttr.__get__)rvrwrxr�r��
__classcell__rrr�rr��sr�cs4eZdZ�fdd�Z�fdd�Z�fdd�Z�ZS)�JsonAttrcstt|��|�dSr|)r�r�r�r~r�rrr��szJsonAttr.__init__cs t�|�}tt|��||�dSr|)�json�dumpsr�r�r�)rr��dict_r�r�rrr��s
zJsonAttr.__set__cs6tt|��||�}|s
dSt|t�r|��}t�|�Sr|)r�r�r��
isinstance�bytes�decoder��loads)rr�r�r�r�rrr��s

zJsonAttr.__get__)rvrwrxr�r�r�r�rrr�rr��sr�c@s�eZdZdZed�Zed�Zed�Zed�Z	ed�Z
ed�Zed�Zed	�Z
ed
�Zed�Zed�Zed
�Zed�Zed�Zed�Zed�Zed�Zed�Zed�Zed�Zed�Zed�Zed�Zed�Zed�Zed�Ze d�Z!e d�Z"ed�Z#ed�Z$d d!�Z%d"d#�Z&e'd$d%��Z(e'd&d'��Z)d(d)�Z*d*d+�Z+d,d-�Z,d.d/�Z-d?d1d2�Z.d3d4�Z/e'd5d6��Z0d7d8�Z1d9d:�Z2d;d<�Z3d=d>�Z4d0S)@�CallStoragezK A wrapper over a Redis database for storing call-related information.
    �state�
call_order�agent_outbound_call�
agent_call_id�call_sid�bridge_call_sid_a�bridge_call_sid_b�caller_name�
caller_numberr�prompt_taskrQ�manual_call�agent_disconnect_webhook_sent�routing_config�routing�
bw_conf_id�bw_conf_member_id�inbound�
start_time�connect_time�first_call_agent_id�
call_cause�cause_description�
agent_list�
pressed_digit�is_group�
is_call_retry�sip_endpoint_id�sip_call_directioncCsd||_||_d�|j�|_d�|j�|_d�|j�|_d�|j�|_d�|j�|_d�|j�|_dS)NzLEAD:{}zCALLBACK_CNT_{}zRETRY_{}zLOCK_{}z	AGENTS_{}zCALLS_{})	rSrRrCr��_callback_cnt_key�
_retry_key�	_lock_key�_agents_key�_agent_calls_key)rrSrRrrrr�EszCallStorage.__init__cCs,|j�|jd|j�|j�|jdt�dS)NrR�)rSr�r�rRrFrG�rrrr�initPszCallStorage.initcCsdd�|j�|jdd�D�S)NcSsg|]}t|��qSr��int�rjr�rrrrlVsz.CallStorage.agents_to_call.<locals>.<listcomp>r���)rS�lranger�r�rrr�agents_to_callTszCallStorage.agents_to_callcs$dd���fdd�|j�|j�D�S)NcSs|�d�}t|d�|dfS)N�_rr[)r]r�)�item�tplrrr�to_agent_call_tupleZs
z<CallStorage.current_agent_calls.<locals>.to_agent_call_tuplecsg|]}�|��qSrrr��r�rrrl]s��z3CallStorage.current_agent_calls.<locals>.<listcomp>)rS�smembersr�r�rr�r�current_agent_callsXs
�zCallStorage.current_agent_callscCs&|r|jj|jgt|��R�dSdSr|)rS�lpushr��reversed)r�	agent_idsrrr�set_agents_to_callbs�zCallStorage.set_agents_to_callcCs|j�|j�}|r
t|�SdSr|)rS�lpopr�r�)rr�rrr�next_agent_to_callfszCallStorage.next_agent_to_callcCs4d�||�}|j�|j|�|j�|jdt�dS)N�{}_{}r�)rCrS�saddr�rFrG�rrQr�r�rrr�push_agent_calljszCallStorage.push_agent_callcCsVd�||�}|���|j�|j|�|j�|j�Wd�S1s$wYdS)Nr�)rC�lockrS�sremr��scardr�rrr�remove_agent_callos

$�zCallStorage.remove_agent_callNcs�|���?|j�|j�}dd�|D�}�fdd�dd�|D�D�}|r;|jj|jgdd�|D��R�Wd�|SWd�|S1sFwY|S)z� Delete from Redis, and return a list of (agent_id, call_sid) pairs,
        representing the in-progress agent call attempts for the current lead.
        css&�|]}t|t�r
|��n|VqdSr|)r�r�r�)rjr�rrr�	<genexpr>{s�$z0CallStorage.clear_agent_calls.<locals>.<genexpr>cs:g|]}�r�t|d�|d�rt|d�|df�qS)rr[r�)rj�lst��	predicaterrrl|s���z1CallStorage.clear_agent_calls.<locals>.<listcomp>css�|]}|�d�VqdS)r�Nr\r�rrrr�~s�cSsg|]
\}}d�||��qS)r�)rC)rjrQr�rrrrl�s
��N)r�rSr�r�r�)rr��values�
agent_sidsrr�r�clear_agent_callsus"

��
��
��zCallStorage.clear_agent_callscC�|j�|jdtd�dS)Nr�r)rSrqr�rGr�rrr�clear_callback_cnt��zCallStorage.clear_callback_cntcCst|j�|j��Sr|)r�rSrr�r�rrr�callback_cnt�szCallStorage.callback_cntcC�|j�|j�Sr|)rS�incrr�r�rrr�inc_callback_cnt��zCallStorage.inc_callback_cntcCr�)NrAr)rSrqr��HOURSr�rrr�clear_retry_cnt�r�zCallStorage.clear_retry_cntcCr�r|)rSr�r�r�rrr�
inc_retry_cnt�r�zCallStorage.inc_retry_cntcCs|jj|jdd�S)N�
)�timeout)rSr�r�r�rrrr��r�zCallStorage.lockr|)5rvrwrxryr�r�r{r�r�r�r�r�r�r�r�rr�rQr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��propertyr�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr��sd

	

r�c@sDeZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZ
dZd
ZdZdS)�InboundCallState�NEW�CALLBACK�LEAD_ONHOLD�AGENT_ONHOLD�CALLBACK_PROMPT�ANSWERED�ONGOING�MISSED�BLOCKED�ERROR�MACHINE�CAPTURED�CALL_ME_BACK�DIGIT_MISSEDN)rvrwrxr�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrr��sr�)!r��loggingrDrm�flaskrr�twilio.restr�buyercall.lib.util_cryptorr�buyercall.extensionsr�	getLoggerrvr�r�rGrr r*r3r>rWrb�objectrcr{r�r�r�r�rrrr�<module>s2


$
1