File: //home/arjun/projects/buyercall/buyercall/blueprints/sms/__pycache__/tw_sms_tasks.cpython-310.pyc
o
�we�? � @ s d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl m
Z
d dlmZ d dl
mZ d dlmZ d d lmZmZ d d
lmZmZ d dlmZ d dlmZ d d
lmZ ee�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_numberc K s& t j�t j| k��� }|j}t�d�|�� t j�t j|j
k��� }tj�tj|jk��� }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|jk��� }|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�W n t �y t�t!�"� � t�d� Y dS w t#| ||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
r r �
phonenumber_1� firstname�lastname� caller_id�partner_urlr r �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_sms sp �
�
��
���
�rR c K s� t j�t j| k��� }|j}tj�tj|jk��� }tj�tj|j k��� }t
|jjj
|j�} z(| jj|||d�}
t�d�|�� t�d�|
j�� t| |
|j|jd|� W dS tym t�t�� � t�d� Y dS w )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
r zFA 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"