U
    XHg                     @   s   d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dlm	Z	m
Z
mZ d dlmZ e	jd Ze	jd Ze	jd Ze	jd	 Ze	jd
 Ze	jd ZdddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )    )absolute_importN)datetime	timedelta)appdb
app_logger)Campaign
EXOTEL_SIDEXOTEL_TOKENEXOTEL_CALL_RECORDING_API_KEYEXOTEL_CALL_RECORDING_TOKENADMIN_SERVEREXOTEL_PHONEF01140844944c           	      C   s,  t d| || t tddd }| dd}|s`t|d dkrt|d d	krztt	j
d
t d t d t d | |ddt| ddd
t d t| d t| dd}t d| t|j W n: tk
r } zt d|  d|  W 5 d }~X Y nX nt dd|| | d S )Nz4Exotel Helper: Calling %s with appid %s and trial %d      )hoursminutesz%H:%M:r         https://@twilix.exotel.in/v1/Accounts//Calls/connecttrans!http://my.exotel.com/exoml/start/1800z/autocallciff//FromCallerIdCallTypeUrlZ	TimeLimitZ
MaxRetriesStatusCallbackdatazAExotel Helper: Exotel response while making call to number %s: %szOExotel Helper: Exception occurred from Exotel call while making call to number z. Error messege: zHExotel Helper: Out of time range Error: %s. Not calling %s with appid %s)r   infor   utcnowr   timestrftimesplitintrequestspost
exotel_sidexotel_tokenstradmin_server_ip	xmltodictparsetext	Exceptionerrorwarningjoin)	phone	exo_appidtrialignore_check	exo_phoneistnowZnow_hmrr7    rA   -/var/www/dostadmin/dostadmin/exotel_helper.py	make_call   st       (

rC   c              
   C   sV  z|| |ddddt  d t|j d}tjdt d t d t d |d	}t	d
| t
|j |jtjjkrt
|jd d }|d |_t|d d|_|d |_t|d d|_tj  W dS |jtjjkrtjj|_tj  W dS tjj|_tj  W dS  t k
rP } z t!d|  d|  W Y dS d }~X Y nX d S )Nr   truer   
/livecall/)r    Tor!   r"   ZRecordRecordingUrlr$   r   @api.exotel.com/v1/Accounts/r   r%   zFExotel Helper: Exotel response while making live call to number %s: %sTwilioResponseCallSidDateUpdated%Y-%m-%d %H:%M:%SStatus	StartTime)T )FDND)FFAILEDzLExotel Helper: Exotel call exception while making live call to phone number . Error message: )"r2   r1   idr-   r.   exotel_call_recording_api_keyexotel_call_recording_tokenr/   r   r'   r3   r4   r5   status_codecodesokcontentcallsidr   strptimeattempted_timestampcall_statuscall_start_timer   sessioncommit	forbiddenr   rN   
FAILED_DNDstatusFAILED_SYSTEMr6   r7   )
user_phoneagent_phoneexotel_phonecampaignr&   r@   response_datar7   rA   rA   rB   make_live_callE   s|    


 
 




rk   c              
   C   s  z| |ddt | dddt d t |j d}tjdt d t d	 t d
 |d}td| t	
|j t	
|jd d }|d |_t|d d|_|d |_t|d d|_tj  W n8 tk
r } ztd|  d|  W 5 d }~X Y nX d S )Nr   r   r   r   r   rE   r   r   rH   r   r%   zJExotel Helper: Exotel response while making feedback call to number %s: %srI   rJ   rK   rL   rM   rN   rO   zPExotel Helper: Exotel call exception while making feedback call to phone number rS   )r1   r2   rT   r-   r.   r/   r0   r   r'   r3   r4   r5   rZ   r[   r   r\   r]   r^   r_   r   r`   ra   r6   r7   )rf   rh   r;   ri   r&   r@   rj   r7   rA   rA   rB   schedule_feedback_call   sh    



 
 rl   c                 C   sB   t dt d t d t d |  }t|jd d }|d S )Nr   r   r   z/Calls/rI   rJ   r    )r-   getr/   r0   r3   r4   r5   )r[   r@   ZcalldictrA   rA   rB   get_calldetails   s&    
rn   c                 C   s>   t | }tdt | tdt||t|d g  d S )Nz3Exotel Helper: Scheduling backup call at - %s to %si     )	rn   r   r'   r)   	threadingTimerrC   r,   start)r[   rd   appidr<   r:   rA   rA   rB   
retry_call   s      rt   c                 C   sP   ddl m} t| dkr0| d dkr0| dd  } || |}t| |ddd d S )	Nr   )	db_helper
   r   ro      T)r<   r=   )	dostadminru   lenget_appid_for_userphonerC   )r:   r>   ru   rs   rA   rA   rB   missed_callback   s
    r{   c                 C   sR   g }| D ]D}t dt d t d t d | }|t|jd d  q|S )Nr   r   r   z	/Numbers/rI   ZNumbers)r-   rm   r/   r0   appendr3   r4   r5   )plist	phoneinfor:   r@   rA   rA   rB   	check_dnd   s*    
r   c                 C   s0   t jdt d t d t d t| dd d S )Nr   r   r   z/CustomerWhitelist)ZVirtualNumberzNumber[]r%   )r-   r.   r/   r0   rh   )phonesrA   rA   rB   whitelist_numbers   s     r   )r   Fr   )
__future__r   r-   r3   r)   rp   r   r   rx   r   r   r   dostadmin.db_modelr   configr/   r0   rU   rV   r2   rh   rC   rk   rl   rn   rt   r{   r   r   rA   rA   rA   rB   <module>   s*   






3:/
