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__/bw_sms_tasks.cpython-310.pyc
o

�weF=�@s4ddlmZddlZddlZddlZddlmZmZddl	m
Z
ddlmZ
ddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZmZddlmZmZdd
l m!Z!ddl"m#Z#ee�Z$e!�Z%e$j&dd��Z'e$j&ddd��Z(e$j&dd��Z)e$j&dd��Z*e$j&dd��Z+e$j&dd��Z,dS)���AgentN)�current_app�url_for)�	bw_client)�create_celery_app)�db)�Message)�Contact)�Endpoint)�Phone��PartnershipAccount�Partnership)�get_sms_routing_agents�get_agent_number)�WebhookUtil)�format_phone_numberc
Ks&tj�tj|k���}tj�tj|k���}tj�tj|jk���}tj�tj|j	k���}|j
dkr<t|j	ddd�}nt|j	d�}|j}|j
d}	t|j�}
tj�tj|
k��tj|jk���}|r�|jrpd�|j|j|
�}n
|jr{d�|j|
�}n|
}|jr�d�|j|j�}
n
td|jd	d
�}
nd}d}
t|	�}zEg}|D]>}t||	�}t|d
�}|jr�d}nd}d�||j||
�}|jj|||d�}t� d�|��|�!|j�t� d�||��q�Wnt"y�}zt� t#�$��t� d�WYd}~dSd}~wwt� d�|�r||_%t&j'�(�dSdS)�� This function is used to send Bandwidth sms messages using celery. More information on
        bandwidth messaging can be found here; https://dev.bandwidth.com/howto/sendSMSMMS.html
    �mobile�	messaging��tn_type�defaultRoutingz
{} {} - {}z{} - {}z{}/contacts/contact/{}zcontacts.contact_lead_pageT)�id�	_external�unknown�unavailabler�Yes�Noz$FROM: {} MSG: {} MEDIA: {} REPLY: {}��m_to�m_from�m_bodyzThe message responds is: {}zbA sms message has been successfully sent to agent phone number: {} using bandwidth phone number {}�lThe bandwidth sms message could not be sent, either due to no agent to receive it or there was another errorN�zUpdating message lead in DB))r	�query�filterr�firstrr�partnership_account_idr�partnership_id�type�bw_dashboard_client�phonenumber�routing_configr�from_r
�
phonenumber_1�	firstname�format�lastname�	caller_id�partner_urlrrr�str�	media_url�	body_text�messages�create�log�info�append�	Exception�	traceback�
format_exc�agent_idr�session�commit)�
inbound_id�
message_id�kwargs�msg_obj�inbound�partner_account�partner�client�phone_number�routing�
lead_phone�contact�contact_name�lead_contact_url�agents�
agent_list�agent�agent_phone_number�sms_agent_number�media_available�mod_body�send_msg�e�r[�G/home/arjun/projects/buyercall/buyercall/blueprints/sms/bw_sms_tasks.py�bw_forward_smssx�


�
����
��
�r]c
Kstj�tj|k���}tj�tj|jk���}|jdkr%t|j	ddd�}nt|j	d�}|j
}	z2|r<|jj||	||d�}
n	|jj||	|d�}
t
�d�|	��t||
|j|j|�t�|
��dd�WSty}zt
�t���t
�d	�WYd
}~d
Sd
}~ww)rrrr)r!r"r#�mediar zHA sms message has been successfully sent using bandwidth phone number {}rr%r$N)rr&r'rr(rr)r+r,r*r-r9r:r;r<r2�add_bw_outbound_message_lead�provider�json�loads�getr>r?r@)rD�to�textrSr^rFrHrIrKr-rYrZr[r[r\�bw_send_smsbsD�
����
��rfc'Ks�t|�dd��}tj�tj|k��tj|k���}	|	sM|�dd�}
|�dd�}|�dd�}t||d�}
|
r8|
|
_|r=||
_	|rB||
_
tj�
|
�tj��tj�tj|k��tj|k���}|dk�r4t|�d	���d
d��dd��dd��d
d�}|�d�}t|�g}|D]�}|�d��s2ddlm}m}|j�|j|k���}|j}|�d�d�d
d�}t|jdd�}z_|j�|�}|�dd�}t�t j!d|�}t"|d��&}d}|j#dd�D]
}|t$|�7}|�%|�q�t&�'d�(|��Wd�n1s�wYddl)m*}|||j||�} |�+t| ��Wq�t,�y1}!zt&�'d�(|!��WYd}!~!q�d}!~!wwq�nd}z�|�r�|j}"|�dd�d k�rKd!}#d"}$nd}#d#}$|�d$�|_-g}%t.d<id%|�d&|�d'|�d(d��d)|�d$��d*|�d+|�dd��d,|�d-d��d.|�d/|$�d0|�d1d2��d3|�d%d��d4|�d|#�d5|�d6|�d7|"�d8|�d��d9|%��}&tj�
|&�tj��t/�0d:|&j�|&WSWdSt,�y�}!zt&�1d;�(|!��WYd}!~!dSd}!~!ww)=�8
        Add sms lead message to the messages table
    �fromr%�
first_nameN�	last_name�
email_address�r0r)�mmsr^�[�]� �'�,z.xml�r
zmedia/r)�request_type�/�-�
UPLOAD_FOLDER�wbri)�
chunk_sizezRead {} bytes.)�upload_mms_imagezexception is: {}�	direction�inrH�receivedr�timer+r`�provider_message_idr�provider_message_daterdr/r8rer7�status�
delivery_code�deliveryCode���
delivery_type�delivery_descriptionrDr)�
contact_id�originating_numberrA�operational_receive_messagezRThere is no contact for message to add new message to message table. Exception: {}r[)2rrcr
r&r'r0r)r(r1r3�emailrrB�addrCr6�replace�split�print�endswith�partnership.modelsrrr�name�
rpartitionr,r*r^�	get_media�path�joinr�config�open�iter_content�len�writer;r<r2�buyercall.blueprints.sms.tasksrzr=r>�
updated_onr	�	webhooker�trigger_generic_webhook�error)'rD�
inbound_tn�message_type�message_infor�r)r`rF�format_phonerOrirjrk�
contact_entry�new_contact�
media_args�media_links�
media_list�irrrI�partner_name�media_file_namerKr��converted_media_file_name�	file_path�f�count�crzr7rZ�new_contact_id�
msg_direction�
msg_statusrS�messager[r[r\�add_bw_message_lead�s����
��
.
����������
�����	�
���
����
��
�%���r�cKs�t�|�}t�d�|��t�|�dd��tt|�dd�d��}tj	�
tj|k��
tj|k��
�}|sKt||d�}	tj�|	�tj��t�d�tj	�
tj|k��
tj|k��
�}
t�d�|
j��z�|
�rO|
j}|�d	d�d
kryd}d}
nd}d
}
|�dd�r�d}nd}|�dd�|
_|s�g}t|||�dd�|�dd�||�dd�|�dd�|�dd�|
|||||�dd�|d�}tj�|�tj��tj	�
tj|k��
�}|du�r:|jdk�r:t�d|j�tj	�
tj|k��
�}|�r.|j�r1ddlm}|j	�
|j|jk��
�}|�r4|
j|jk�r7|j|
_|j|
_tj��ddl m!}||
�WdSWdSWdSWdSWdS|du�rR|jdk�rUt�d|j�WdSWdSWdSWdSt"�yp}zt�#d�WYd}~dSd}~ww)rgzThe send msg is: {}rer%rdrrlz"The message contact has been addedz+The contact exist and the contact id is: {}r{�out�outbound�sentrr^rm�smsr~rrh)r+r`rr�rdr/r8r7r�r{rDr)r�r�rANr�mobile_send_messager)�send_agent_push_notification�tracking�operational_send_messagezCThere is no contact for message to add new message to message table)$rarbr;r<r2rcrr6r
r&r'r0r)r(rrBr�rCrr�r	rr+r�r�rrDrA�"buyercall.blueprints.agents.modelsr�	full_name�agent_assigned�!buyercall.blueprints.mobile.utilsr�r>r�)rDr�r)r`rSrF�msg_dictr�rOr�r�r�r�r��msg_typer��phone�sip_endpointrrTr�rZr[r[r\r_�s�
���

��





�


������6�����r_c
Ks�d}t|d�}z|jj|||d�}t�d�|��t�d�|��WdStyB}zt�t���t�d�WYd}~dSd}~ww)N�rr zThe msg response looks like: {}zQA two factor auth code has been successfully sent using bandwidth phone number {}zBThe bandwidth two factor authentication sms code could not be sent)	r,r9r:r;r<r2r>r?r@)�	to_number�from_number�bodyrF�
partner_idrK�msgrZr[r[r\�app_send_smsNs(
�
����r�c
Ksztj�tj|k���}z|r||_||_||_tj	�
�WdSWdSty<}zt�
d�|��WYd}~dSd}~ww)Nz,Unable to update outbound message record: {})r	r&r'rr(r�r�r�rrBrCr>r;r<r2)�	bw_msg_idr�r�r�rFr�rZr[r[r\�update_msg_recordcs���r�)N)-r�rr?�loggingr;ra�flaskrr�os.pathr��buyercall.lib.util_bandwidthrr,�
buyercall.appr�buyercall.extensionsr�buyercall.blueprints.sms.modelsr	�$buyercall.blueprints.contacts.modelsr
�"buyercall.blueprints.mobile.modelsr�(buyercall.blueprints.phonenumbers.modelsr�'buyercall.blueprints.partnership.modelsrr�)buyercall.blueprints.phonenumbers.routingrr�buyercall.lib.util_webhooksr�buyercall.blueprints.filtersr�celeryr��taskr]rfr�r_r�r�r[r[r[r\�<module>s>
F)
m
S