
    jJh                     R   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 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Zd Zd Zd Zd Zd Zd Zd Zy)    )absolute_importN)datetime	timedelta)appdb
app_logger)get_current_utc_time)Campaign
EXOTEL_SIDEXOTEL_TOKENEXOTEL_CALL_RECORDING_API_KEYEXOTEL_CALL_RECORDING_TOKENADMIN_SERVEREXOTEL_PHONEc                     t        j                  d| ||       t               t        dd      z   }|j	                         j                  d      j                  d      }|s"t        |d         dk\  rt        |d         d	k  r	 t        j                  d
t        z   dz   t        z   dz   t        z   dz   | |ddt        |      z   ddd
t        z   dz   t        |      z   dz   t        |      z   d      }t        j                  d| t        j                  |j                                y t        j&                  ddj)                  |      | |       y # t"        $ r%}t        j$                  d|  d|        Y d }~y d }~ww xY w)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Url	TimeLimit
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	   r   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istnownow_hmrr:   s	            -/var/www/dostadmin/dostadmin/exotel_helper.py	make_callrF      s   OO>yRW
 "#ia&DDF[[]##G,2237FF1I!+F1I"0D 	  3	3
  ## " ) '>YO!&"%&0%'&&'' )n'% 	'
 %j'!A. OOS' 	VHHV		
  	abgahhyz  zA  B 	s   B"E 	E=E88E=c           
      H   	 || |ddddt         z   dz   t        |j                        z   d}t        j                  dt
        z   dz   t        z   dz   t        z   dz   |	      }t        j                  d
| t        j                  |j                               |j                  t        j                  j                  k(  rt        j                  |j                         d   d   }|d   |_        t%        j&                  |d   d      |_        |d   |_        t%        j&                  |d   d      |_        t.        j0                  j3                          y|j                  t        j                  j4                  k(  r>t6        j8                  j:                  |_        t.        j0                  j3                          yt6        j8                  j>                  |_        t.        j0                  j3                          y# t@        $ r%}t        jB                  d|  d|        Y d }~yd }~ww xY w)Nr   truer   
/livecall/)r"   Tor#   r$   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: )"r5   r4   idr0   r1   exotel_call_recording_api_keyexotel_call_recording_tokenr2   r   r+   r6   r7   r8   status_codecodesokcontentcallsidr   strptimeattempted_timestampcall_statuscall_start_timer   sessioncommit	forbiddenr
   rS   
FAILED_DNDstatusFAILED_SYSTEMr9   r:   )
user_phoneagent_phoneexotel_phonecampaignr*   rD   response_datar:   s           rE   make_live_callrp   F   s   6$"( (++
 MM+, ** -	-
   	
 	TOOAFF#	

 ==HNN---%OOAII67GHPM,U3H+3+<+<m,.A,H( $1#:H '/'8'8k*,?(H$ JJ==HNN444&oo88HOJJ"//77


 Z[eZffwx}w~	
 	s%   EG3 A$G3 5=G3 3	H!<HH!c           
         	 | |ddt        |      z   dddt        z   dz   t        |j                        z   d}t        j                  dt
        z   dz   t        z   d	z   t
        z   d
z   |      }t        j                  d| t        j                  |j                               t        j                  |j                        d   d   }|d   |_        t        j                  |d   d      |_        |d   |_        t        j                  |d   d      |_        t&        j(                  j+                          y # t,        $ r%}t        j.                  d|  d|        Y d }~y d }~ww xY w)Nr   r   r   r   r   rI   r!   r   rM   r   r)   zJExotel Helper: Exotel response while making feedback call to number %s: %srN   rO   rP   rQ   rR   rS   rT   zPExotel Helper: Exotel call exception while making feedback call to phone number rX   )r4   r5   rY   r0   r1   r2   r3   r   r+   r6   r7   r8   r_   r`   r   ra   rb   rc   rd   r   re   rf   r9   r:   )rk   rm   r>   rn   r*   rD   ro   r:   s           rE   schedule_feedback_callrr      s   *
$6YG( (++
 MM  -	-
   	
 	XOOAFF#	

 "		23CDVL(/'/'8'8-(*=(
$  -X6#+#4#4+&(;$
  	

 
^_i^jj{  }B  |C  D	
 	

s   D2D5 5	E#>EE#c                     t        j                  dt        z   dz   t        z   dz   t        z   dz   | z         }t	        j
                  |j                        d   d   }|d   S )Nr   r   r   z/Calls/rN   rO   r"   )r0   getr2   r3   r6   r7   r8   )r`   rD   calldicts      rE   get_calldetailsrv      s    
	
	 	 +		+
 	 	 			A qvv&'78@HF    c           	          t        |       }t        j                  dt        j                         |       t	        j
                  dt        ||t        |      dz   g      j                          y )Nz3Exotel Helper: Scheduling backup call at - %s to %si     )	rv   r   r+   r,   	threadingTimerrF   r/   start)r`   ri   appidr?   r=   s        rE   
retry_callr~      sO    G$EOO=tyy{E OOD)eUCJN%CDJJLrw   c                     ddl m} t        |       dkD  r| d   dk(  r| dd  } |j                  | |      }t	        | |dd       y )	Nr   )	db_helper
   r   ry      T)r?   r@   )	dostadminr   lenget_appid_for_userphonerF   )r=   rA   r   r}   s       rE   missed_callbackr      sE    #
5zB58s?ab	--eY?EeU!$7rw   c                     g }| D ]l  }t        j                  dt        z   dz   t        z   dz   t        z   dz   |z         }|j	                  t        j                  |j                        d   d          n |S )Nr   r   r   z	/Numbers/rN   Numbers)r0   rt   r2   r3   appendr6   r7   r8   )plist	phoneinfor=   rD   s       rE   	check_dndr      s    ILL  /	/
   	
 	01AB9MN  rw   c                 |    t        j                  dt        z   dz   t        z   dz   t        z   dz   t        | d       y )Nr   r   r   z/CustomerWhitelist)VirtualNumberzNumber[]r)   )r0   r1   r2   r3   rm   )phoness    rE   whitelist_numbersr      sS    MM
	
	 	 +		+
 	 	  ,@	rw   )r   F01140844944)
__future__r   r0   r6   r,   rz   r   r   r   r   r   r   utils.helpers.helpersr	   dostadmin.db_modelr
   configr2   r3   rZ   r[   r5   rm   rF   rp   rr   rv   r~   r   r   r    rw   rE   <module>r      s    &     ( ) ) 6 'ZZ%
zz.) #

+J K !jj)FG **^,zz.)0
f7t+
^M8$
rw   