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/sms/__pycache__/tw_sms_tasks.cpython-310.pyc
o

�we�?�@sddlZddlZddlmZddlmZmZddlmZddl	m
Z
ddlmZddl
mZddlmZdd	lmZmZdd
lmZmZddlmZddlmZdd
lmZee�Ze�Zejdd��Z ejdd��Z!ejdd��Z"ejdd��Z#ejdd��Z$ejdd��Z%dS)�N)�datetime)�current_app�url_for)�create_celery_app)�subaccount_client)�Message)�Contact��Phone)�PartnershipAccount�Partnership)�get_sms_routing_agents�get_agent_number)�WebhookUtil)�db)�format_phone_numbercKs&tj�tj|k���}|j}t�d�|��t	j�t	j|j
k���}tj�tj|jk���}t
|jjj|j�}	|jd}
t�d�|
��t|
�}t|�}tj�tj|k��tj
|j
k���}
|
r�|
jrmd�|
j|
j|�}n
|
jrxd�|
j|�}n|}t	j�t	j|j
k���}tj�tj|jk���}|jr�d�|j|
j�}n
td|
jdd	�}nd
}d}t�d�|��z;g}|D]4}t||
�}t|d
�}t�d�|��d�|||�}|	jj|||d�|�|j�t�d�||��q�Wnt �yt�t!�"��t�d�YdSwt#|||j
|j$d|�dS)�� This function is used to send Twilio sms messages using celery. More information on bandwidth messaging
        can be found here; https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-python
    z7The Twilio inbound number phone number for smsing is {}�defaultRouting�.The Twilio inbound number routing config is {}z{} {} {}z{} {}z{}/contacts/contact/{}�contacts.contact_lead_pageT��id�	_external�unknown�unavailable�The url for the contact is {}r�(The available agent contact number is {}zFROM:{} MSG:{} REPLY: {}��from_�to�bodyz_A Twilio sms message has been successfully sent to agent phone number: {} using phone number {}ziThe Twilio sms message could not be sent, either due to no agent to receive it or there was another errorN)%r
�query�filterr�first�phonenumber�log�info�formatr�partnership_account_idr�partnership_idr�partnership_account�subscription�twilio_subaccount_sid�routing_configr
rr�
phonenumber_1�	firstname�lastname�	caller_id�partner_urlrr�str�messages�create�append�	Exception�	traceback�
format_exc�add_tw_message_lead�provider)�
inbound_id�text�lead�message_info�kwargs�inboundr$�partner_account�partner�
twilio_client�routing�agents�
lead_phone�contact�contact_name�lead_contact_url�
agent_list�agent�agent_phone_number�sms_agent_number�mod_body�rP�G/home/arjun/projects/buyercall/buyercall/blueprints/sms/tw_sms_tasks.py�tw_forward_smssp�
�
��
���

�rRcKs�tj�tj|k���}|j}tj�tj|jk���}tj�tj|j	k���}t
|jjj
|j�}	z(|	jj|||d�}
t�d�|��t�d�|
j��t||
|j|jd|�WdStymt�t���t�d�YdSw)z� This function is used to send Twilio sms messages using celery. More information on twilio messaging
        can be found here; https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-python
    rzFA Twilio sms message has been successfully sent using phone number: {}�The message sid is {}�z;The Twilio sms message could not be sent! That was an errorN)r
r!r"rr#r$rr(rr)rr*r+r,r4r5r%r&r'�sidr:r;r7r8r9)r<rr=rKr@rAr$rBrCrD�messagerPrPrQ�tw_send_sms\s,����rWcKs6tj�tj|k���}|j}tj�tj|jk���}tj�tj|j	k���}	t
|jjj
|	j�}
t|�}tj�tj|k��tj|jk���}|r�|jrTd�|j|j|�}
n
|jr_d�|j|�}
n|}
tj�tj|jk���}tj�tj|j	k���}	|	jr�|	jdt|j�}n
td|jdd�}nd}
d}t�d	�|��t�d
�|��|jd}t�d�|��t|�}zAg}|D]:}t||�}t|d
�}d|
d|d|}t�d�|��|
jj||||d�|�|j�t�d�||��q�Wnt �y
t�t!�"��t�d�YdSwt#|||j|j$||�dS)rz
{} {} - {}z{} - {}z/contacts/contact/rTrrrr�7The Twilio inbound number phone number for mmsing is {}rrrzFROM:z MSG:z REPLY:r�rrr �	media_urlz_A Twilio mms message has been successfully sent to agent phone number: {} using phone number {}�iThe Twilio mms message could not be sent, either due to no agent to receive it or there was another errorN)%r
r!r"rr#r$rr(rr)rr*r+r,rrr.r/r'r0r1r2r3rr%r&r-r
rr4r5r6r7r8r9r:r;)r<r=r>�mediar?r@rAr$rBrCrDrGrHrIrJrErFrKrLrMrNrOrPrPrQ�tw_forward_mms{sn���

���
�r]c

Kstj�tj|k���}|j}t�d�|��t�d�|��t	j�t	j|j
k���}tj�tj|jk���}	t
|jjj|	j�}
z)|
jj||||d�}t�d�|��t�d�|j��t|||j
|j||�WdSty�}zt�t���t�d�WYd}~dSd}~ww)z� This function is used to send Twilio mms messages using celery. More information on Twilio messaging
        can be found here; https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-python
    rXzthe media is {}rYzFA Twilio mms message has been successfully sent using phone number: {}rSr[N)r
r!r"rr#r$r%r&r'rr(rr)rr*r+r,r4r5rUr:r;r7r8r9)
r<rr=r\rKr@rAr$rBrCrDrV�erPrPrQ�tw_send_mms�s4�����r_cKs�d}|jdkrd}d}n|j}|dkrd}t|j�}	nt|j�}	tj�tj|	k��tj|k��	�}
|
sIt|	|d�}t
j�|�t
j�
�t�d�tj�tj|	k��tj|k��	�}t|j�}
|}t|j�}t�d	�|��z`|r�|j}|
|_|r~d
}|}nd}d}|s�g}t|||j|j|j||j||||||||d�}t
j�|�t
j�
�|dkr�t�d
|j�WdS|dkr�t�d|j�WdSWdSt�d�WdSty�}zt�d�WYd}~dSd}~ww)z8
        Add sms lead message to the messages table
    rTzoutbound-api�outbound�sentrA�received)r.r(z"The message contact has been addedzThe media_url is: {}�mms�sms)�typer;�provider_message_id�provider_message_daterr�	body_textrZ�status�	directionr<r(�
contact_id�originating_number�agent_id�operational_send_message�operational_receive_messagezThere no new contactz!Error: Unable to add message leadN)rjrrrrr!r"r.r(r#r�session�add�commitr%r&r3�date_createdr'r�
updated_onrrUr �	webhooker�trigger_generic_webhook�errorr7)r<r?r(r;r\rKr@�
msg_status�direct�format_phonerH�
contact_entry�new_contact�daterZ�formatted_pn�new_contact_id�eventype�urlrVr^rPrPrQr:�s�

���

��

�
���r:cKs�ddlm}ddlm}ddlm}ddlm}|j�	|j
|k���}|j�	|j|j
ko1|kn���}	|	rT|	D]}
t|
j�}t�d�|
j��||krR|
j}q;nd}|j�	|j
|k���}
t�d�|
j��tj�	tj|k��	tj|jk���}|s�d	�|�d
fSz3||j|j|j|j|jt��d|j|||d|j
d
�
}|��d}d||d�gi}|�|j
||�WdSty�}zt� d�|��t� t!�"��WYd}~dSd}~ww)Nr)�Leadr	)�Agent)�connect_lead_to_agentzThe phonenumber is {}znew leadzIs the agent available now? {}z-Contact lead with phone nummber: {} not foundi�r`�ringing)
r(r/r0r$�email�	starttime�	call_type�my_phoner<rm�progress_statusrirk�phonerF)r�contactUsingz_An outbound call to agent and lead can not be made based on the sms rule for the inbound id: {}rT)#�!buyercall.blueprints.leads.modelsr��(buyercall.blueprints.phonenumbers.modelsr
�"buyercall.blueprints.agents.modelsr��'buyercall.blueprints.phonenumbers.tasksr�r!r"rr#r(r<�allrr$r%r&r'r��
available_nowrr.r/r0r�r�utcnow�save�delayr7rwr8r9)r<�lead_numberrmr@r�r
r�r�rA�all_inbound_lead�row�lead_nor�rL�contact_leadr>�
contact_using�
call_settingsr^rPrPrQ�sms_rule_call_lead4st��
��
��������r�)&r8�loggingr%r�flaskrr�
buyercall.appr�buyercall.lib.util_twilior�buyercall.blueprints.sms.modelsr�$buyercall.blueprints.contacts.modelsrr�r
�'buyercall.blueprints.partnership.modelsrr�)buyercall.blueprints.phonenumbers.routingr
r�buyercall.lib.util_webhooksr�buyercall.extensionsr�buyercall.blueprints.filtersr�celeryru�taskrRrWr]r_r:r�rPrPrPrQ�<module>s8
D

E
"
O