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/mobile/__pycache__/tasks.cpython-310.pyc
o

�we^�@s�ddlZddlmZddlZddlZddlZddlmZddlZddl	m
Z
ddlmZm
Z
mZddlmZddlmZddlmZddlZddlZddlZdd	lmZdd
lmZddlmZddlm Z dd
l!m"Z"ddl#m$Z$ddl%m&Z&m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z.ddl/m0Z0ddl1m2Z2ddl3m4Z4m5Z5ddl6m7Z7ddl8m9Z9dZ:dZ;dZ<ej=dd�Z>e
e.�Z?e)�Z@e?jAd-dd��ZBe?jAdd ��ZCe?jA		d.d!d"��ZDe?jAd#d$��ZEe?jAd%d&��ZFe?jAd'd(��ZGe?jAd)d*��ZHe?jAd+d,��ZIdS)/�N)�datetime)�BytesIO)�create_celery_app)�
split_name�CallStorage�InboundCallState)�	bw_client)�_try_renderer_template��send_ses_email)�Key)�db)�Lead)�Phone)�Endpoint)�Message)�format_phone_number�format_phone_number_bracket)�WebhookUtil)�current_app)�Contact)�text���Partnership�PartnershipAccount)�	load_only)�and_ii�Q��redis)�hostc	KsFtj�tj|k���}tj�tj|jk���}ddlm}m	}	|	j�|	j|j
k���}
t|�\}}d}
d}|s9|r=|dkrFt�
|j
|�\}}tj�|tjk|j
tj
k��tj�����}tj�td���|tjk|j
tj
k��tj�����}tj�ttj|ktj
|j
k����}|r�|r�|j}n|}t�d�|��|dur�tj�tj|jk��tj
|j
k���}|j}|j}|j}|j}
|j}|j}nGt |
j!dd�}t"j#�$d	d
�r�|j%|dd�}n|�%|�}t�d
�|��|s�d}t|||||j&d|j
d�}t'j(�)|�t'j(�*�t�d�|j}|�rd}|j}nd}td%id|j
�d|�d|�d|�d|�d|
�dt+�,��d|�d|j&�d|�d|�d|j�d|j-�d|�dd �d!|�d"|��}t'j(�)|�t'j(�*�tj�tj|k��tj�����}t�d#�|��tj�tj.|jk��tj
|j
k���}|�r�|j/|_/t'j(�*�|jSt�d$�|jS)&Nrr��Unknown�phonenumberz$The previous lead call number is: {}�mobile)�tn_type�DEBUGFT)�testz(The mobile call cnam lookup returned: {})�	firstname�lastname�	caller_id�
phonenumber_1�agent_id�email�partnership_account_idzThe contact has been added�outbound�inboundr.r(r*r)r-�	starttime�progress_statusr,�call_sid�	call_type�my_phone�call_source�
inbound_id�status�ringing�
contact_id�originating_numberzlatest lead: {}zno lead contact exist�)0r�query�filter�id�firstrr7�'buyercall.blueprints.partnership.modelsrrr.rr�get_last_known_namer#�order_by�
created_on�desc�optionsrr5r�log�info�formatrr:r(r)r-r8r*r�partnership_id�celery�conf�get�cnam_lookupr,r
�session�add�commitr�utcnow�
friendly_namer+�
updated_on)r7�call_id�phone_number�sip_outbound�caller_name�kwargsr0�sip_endpointrr�partner_account�
first_name�	last_namer-r2�
previous_lead�previous_operational_lead�operational_number�originating_call_number�contactr:r*�client�new_contact�	direction�lead�latest_lead�lead_contactr<r<�C/home/arjun/projects/buyercall/buyercall/blueprints/mobile/tasks.py�mobile_lead_create1s�����
��
��
���
�	

��������	�
���
�����
"
��

�rjcKsTtjtjdtjdd�}t||�}t�d�|j��|�	dd�}t�d�|��|�	dd�}|�	d	d�}t
j�t
j
|k���}|r�t|j�|_||_d
|_tj|_||_tj��|jr�|r�tj�tj
|jk���}ddlm}	|	j�|	j
|jk���}
|r�|
r�|j|
j
kr�|j|_|
j|_tj��dd
lm }||�dSt�!d�|��dS)N�REDIS_CONFIG_URL�REDIS_CONFIG_PORT)r �portzAgent id is: {}�	to_numberr!zTHE to number IS: {}�transfer_call_id�connect_timezin-progressr��Agent��send_agent_push_notificationz!No lead was found for lead id: {})"r�StrictRedisrKrLrrGrHrIr,rMrr=r>r?r@�intror8�State�ANSWERED�staterpr
rOrQrr:�"buyercall.blueprints.agents.modelsrr�	full_name�agent_assigned�!buyercall.blueprints.mobile.utilsrt�error)�lead_idrY�redis_db�storagernrorprfrbrr�agentrtr<r<ri�mobile_update_lead�s8



�r�cKsRt�d�|��t�d�|��tj�tj|k���}	t�d�|	��|}|}ddi}
d}z>|rK|||	j|	j	|	j
d�}tj|t
d�}
t�d	�|
��n||	j|	j	|	j
|||||d
d�
}tj|t
d�}
t�d	�|
��Wnty�}zt�dt
t��d
��WYd}~nd}~wwtj||
|
d�}t�d�|j��t�d�|j����|S)NzThis got hit {}zThe message is {}zThe endpoint is {}�Content-Typezapplication/jsonz(https://pnm.cloudsoftphone.com/pnm2/send)�verbr�DeviceToken�Selector�AppId)�defaultzThe json payload looks like {}r�)
r�r�r�r��UserDisplayNamer�ContentType�Id�Badge�Soundz,The push notification was not sent because: r)�data�headerszThe status code is: {}zThe response is: {})rGrHrIrr=r>�sip_usernamer@�device_token�selector�app_id�json�dumps�str�	Exception�debug�sys�exc_info�requests�post�status_code�content�decode)r��	push_type�message�sms_id�message_preview�badge�user_display_name�content_typerY�endpointr��url�payload�	json_data�e�rr<r<ri�push_notification�sP���&��r�c6Ks"t�d�|��|d�dd�}t|�dd��}tj�td���	t
tj|ktj|k���
tj�����}tj�	t
tj|ktj|k����}|rM|rM|j}	n|}	t�d�|��tj�	tj|k��	tj|k���}
|
rrt�d�|
��|
s�t||d	�}tj�|�tj��t�d
�tj�	tj|k��	tj|k���}|�r|j}
|�dd�dkr�d
}d}nd}d}|�dd�|_|�dd��r�d}t|�d���dd��dd��dd��dd�}|�d�}t|�g}|D]�}|� d��s�ddl!m"}m#}|j�	|j|k���}|j$}|�%d�d�dd�}t&|j'ddd�}zb|j(j)|d�}|�d d!�}t*�+t,j-d"|�}t.|d#��'}d}|j/d$d%�D]} |t0| �7}|�1| ��q<t�d&�|��Wd'�n	1�s^wYdd(l2m3}!|!||j||�}"|�4t|"��Wq�t�d)�tt5�6�d���Yq�q�dd*l7m8}#d+t|j�d,t|�}$|#d-gd.d/|$d0�nd}d1}g}%t9j�	t9j:|k���}&|&�r�|&j;�r�dd2l<m=}'|'j�	|'j|&j;k���}(|(�r�|%�4|(j�tdSid3|�d4|�d5|�d6d��d7|�dd��d8|d�d8d��d|�dd��d9|�d:d��d;|�d<|�d=|d�d>d?��d@|d�d3d��dA|d�dBd��d|�dC|�dD|�dE|
�dF|	�dG|%��})tj�|)�tj��|�dd�dk�r�t>�?dH|)j�|(�r�|�r�|(j|j;k�r�|(j|_;|(j@|_Atj��ddIlBmC}*|*|�gdJ�}+|�d:d�},|,�D�}-|-|+v�r�tj�	tj|
k���}.|.�r�dK|._Etj��n
t�FdL�|�dd�|��t�dM�dSt9j�	t9j:|k���}/|)jGdk�r�dN}0|)jH}1ndO}0|)jI}1|
�r�|
jJ�r�|
jK}2n|
�r�|
jJ�s�|
j}2n	|�r�|j}2nd}2dP}3|)jL}4dQ}5tM|/jN|5|4|1|3|2|0dR�|)jSd'S)Tz?
        Add mobile sms lead message to the messages table
    zThe message args look like: {}rr�r!�from�from_z The formatted phone number is {}z The existing contact info is: {})r+r.z"The message contact has been addedre�inr0�received�unknown�time�media�mms�[�]� �'�,z.xmlr)rrzmedia/�	messagingr$)�request_typer%)�
media_name�/�-�
UPLOAD_FOLDER�wbi)�
chunk_sizezRead {} bytes.N)�upload_mms_imagezexception is: {}r
zThe contact id is: z and partnership account id: zadmin@buyercall.com�z)Encryption failed for list value - mobile)�
recipients�p_id�subjectr�smsrq�type�provider�provider_message_idr?�provider_message_date�to�	body_textr�	media_urlr8�
delivery_code�deliveryCode���
delivery_type�delivery_description�descriptionr7r.r:r;r,�mobile_receive_messagers)�stopz stopzstop �unsubscribez unsubscribezunsubscribe zun-subscribe�unsubcribedTz�A contact id was not found for the message being sent from: {} using phone number {}and we were unable to unsubscribe the contact from receiving messageszEMessage not sent due to unsubscribe keyword in received message text.z$application/x-acro-filetransfer+jsonz
text/plain�1�NotifyTextMessage)r�r�r�r�r�r�r�r<)OrGrHrIrMrrr=rFrr>rr�r.rCrDrEr@rr#r�rr+r
rOrPrQr?rTr��replace�split�print�endswith�partnership.modelsrr�name�
rpartitionrrJr��	get_media�path�joinrKrL�open�iter_content�len�write�buyercall.blueprints.sms.tasksr��appendr�r��buyercall.lib.util_ses_emailrrr7r,rzrr�	webhooker�trigger_generic_webhookr{r|r}rt�lower�is_unsubscriber~r�r�r�r(�
user_fullnamer�r�r�)6r7�message_infor.r�rY�message_object�format_phone�previous_messager`�originating_msg_numberrb�
contact_entryrd�new_contact_id�
msg_direction�
msg_status�message_type�
media_args�media_links�
media_list�irrr[�partner_name�media_file_namercr��converted_media_file_name�	file_path�f�count�cr�r�r�
email_message�
agent_listrZrrr�r�rt�keyword_list�sms_msg_text�lowercase_text_body�sms_contactr�r�r�r�r�r�r�r<r<ri�add_mobile_message_leadsn�����

�.
��� �����������	�
���
������


�

���rc
Ks2tjdtjjddd�}tj�tj|k���}t	j
dr*d|d|d|d	}nd|d|d|}t�d
�
|��|�|�|jdd�|jd
dd�}	t�}
|	�|
d�|
��}z�t�t	j
dt	j
d�}t	j
d}
|�|
�}t�d�
|��t|�}t�d�
|��t��j}t�d�
|��t�d�
|��t�d�
|��t�d�
|��zd�
||||�}t�d�
|��Wnty�}zt�d�WYd}~nd}~ww||_|�dd�|�|�|� d �t�d!�
|j��|j!d"d#d$�WSt�y}zt"|j#�t"|j$�WYd}~dSd}~ww)%z/ Upload the qr code to S3 to get data.
        r��r)�version�error_correction�box_size�border�MOBILE_APP_TEST_MODEzcsc:�:�@�*z!The content for the qr code is {}T)�fit�black�white)�
fill_color�
back_color�PNG�AMAZON_ACCESS_KEY�AMAZON_SECRET_KEY�	QR_BUCKET�The bucket is {}�
The key is {}zthe hex value {}zthe partner account name {}zthe partner account id {}zthe sip username {}z{}_{}/{}_qrcode_{}zThe qr code key is {}z"Could not set key for the qr imageNr�z	image/pngzpublic-readzUploaded file {}rF)�
expires_in�
query_auth)%�qrcode�QRCode�	constants�ERROR_CORRECT_Lrr=r>r�r@rKrLrGrHrI�add_data�make�
make_imager�save�getvalue�boto�
connect_s3�
get_bucketr�uuid�uuid4�hexr�r��key�set_metadata�set_contents_from_string�set_acl�generate_urlr��__doc__r�)r��sip_passwordr.�partner_account_name�cloud_idrY�qrr�r��img�qr_transform�qr_image�s3�bucket_name�bucket�k�	hex_valuer4r�r<r<ri�qr_code�sb�




���


��rFc
Kst|�}t�d�|��|�dd��dd�\}}|�dd�}t�d�|��z>t�tj	d	tj	d
�}tj	d}|�
|�}t�d�|��t|�}	t�d
�|	��||	_|�
|	�t�d�|	j��WdSty�}
zt|
j�t|
j�WYd}
~
dSd}
~
ww)z) DELETE QR code image from S3 bucket
    z
The url is {}zhttps://r!r�r�z%20r�zthe file name is {}rrr r!r"zDELETED file {}N)r�rGrHrIr�r�r.r/rKrLr0rr4�
delete_keyr�r�r9r�)�qr_urlrY�
old_qr_url�domain�	file_name�formatted_file_namerArBrCrDr�r<r<ri�delete_qr_code�s*



��rMcKs�tj�tj|k���}tj�tj|jk���}tj�ttj|ktj	|ktj
�d�����}|j}|j
}	|j|||	d�}
tdddi|
��}t|g|jd|d�dS)	NF)rFr#�company�partner_logo�mail/mobile_app_setup�ext�htmlz'BuyerCall Mobile App Setup Notification)r�r�r�rR)rP)rr=r>r?r@rrJrrr.�is_deactivated�is_r��logorHr	r)�sip_idr#r-r.rYr[�partnerrZ�partnership_name�partnership_logo�ctx�qr_email_templater<r<ri�send_mobile_app_info_emails&
���
�r\cKs�tj�tj|k���}tj�tj|jk���}|dur$|jdvr$|j}nt|j	�}t
j�t
j|jk���}|rEd|}t|j
d|d�dSdS)N)r!r�zNew voicemail from �NotifyGenericTextMessage)r�r�r�)rr=r>r?r@rr:r�rr#rr7r�r�)rrYrfrb�contact_detailrZ�vm_push_msgr<r<ri�send_vm_push_notification+s

��r`)N)NNNNNN)J�loggingrGrr�r�r�iorr��
buyercall.appr�buyercall.lib.util_twiliorrrrw�buyercall.lib.util_bandwidthr�buyercall.lib.flask_mailplusr	r�rr%r.r1�boto.s3.keyr�buyercall.extensionsr
�!buyercall.blueprints.leads.modelsr�(buyercall.blueprints.phonenumbers.modelsr�"buyercall.blueprints.mobile.modelsr�buyercall.blueprints.sms.modelsr�buyercall.blueprints.filtersrr�buyercall.lib.util_webhooksr�os.pathr��flaskr�app�$buyercall.blueprints.contacts.modelsr�sqlalchemy.sqlrr�rr�sqlalchemy.ormr�
sqlalchemyr�HOURS�DAYS�
AGENT_TIMEOUTrur�rKr��taskrjr�r�rrFrMr\r`r<r<r<ri�<module>snz
$�1
4
;