
    x=j                         d Z ddlmZmZ ddlmZ ddlZddlmZ ddlm	Z	  e	e
      Z e       Z G d d      Z e       Zy)	z3Notification service for email and Telegram alerts.    )datetimetimezone)OptionalN)get_settings)
get_loggerc                   4   e Zd ZdZ	 	 	 	 	 d#dedededededz  dedz  d	edz  d
edz  dedz  ddfdZ	 	 	 d$deded
ededz  dedz  dedz  ddfdZdeddfdZd%dZ	deddfdZ
d
eddfdZdededdfdZdededededdf
dZdedefdZd ed!edefd"Zy)&NotificationServicez9Sends alerts for trading events, errors, and risk limits.Nsymbolsidepricesize	stop_losstake_profit
confidencereasonmodereturnc
           
        K   d| d| d| dd| dg}
|r|
j                  d| d       |r|
j                  d| d       ||
j                  d| d	       |	r|
j                  d
|	 d       |r|
j                  d|        | j                  dd| d| dj                  |
      ||||d       d {    y 7 w)Nu   🟢 <b>Trade Opened</b> —  zPrice: <code></code>zSize: <code>zStop Loss: <code>zTake Profit: <code>zConfidence: <b>z/100</b>	Mode: <i></i>Reason: trade_openedzTrade Opened: 
)r
   r   r   r   )append_notify_alljoin)selfr
   r   r   r   r   r   r   r   r   liness              KC:\xampp\htdocs\tradingbinance\backend\app\services\notification_service.pysend_trade_openedz%NotificationService.send_trade_opened   s      5TF!F8DE7'*4&(

 LL,YKw?@LL.{m7CD!LL?:,h?@LL9TF$/0LL8F8,-TF!F8,IIeteTJ	
 	
 	
s   B7C9B?:Cpnlentry_price
exit_pricec           	      x  K   	 t        |      }|dk\  rdnd}| d| d| dd| g}	|r|	j                  d	| d
       |r|	j                  d| d
       |r|	j                  d| d       | j	                  dd| dj                  |	      |||d       d {    y # t        t        f$ r d}Y w xY w7 w)Ng        r   u   📈u   📉u    <b>Trade Closed</b> — zPnL: <b>z</b>r   zEntry: <code>r   zExit: <code>r   r   trade_closedzTrade Closed: r   )r
   r#   r   )float	TypeError
ValueErrorr   r   r   )
r   r
   r#   r   r$   r%   r   	pnl_valueemojir    s
             r!   send_trade_closedz%NotificationService.send_trade_closed4   s     	c
I !*QLg1&:se4 vh

 LL=W=>LL<
|7;<LL9TF$/0VH%IIecV<	
 	
 	
 :& 	I		
s4   B:B! BB:B8B:!B52B:4B55B:loss_pctc                 T   K   | j                  ddd| dd|i       d {    y 7 w)N
daily_losszDaily Loss Limit WarningzDaily loss reached z%. Trading may be locked.r.   r   )r   r.   s     r!   send_daily_loss_alertz)NotificationService.send_daily_loss_alertT   s9     &!(+DE"	
 	
 	
s   (&(c                 H   K   | j                  dddi        d {    y 7 w)Nconnection_lostzBinance Connection LostzAConnection to Binance API has been lost. Attempting reconnection.r1   )r   s    r!   send_connection_lostz(NotificationService.send_connection_lost\   s*     %O	
 	
 	
s   " "errorc                 R   K   | j                  ddd| d|i       d {    y 7 w)Ncritical_errorzCritical ErrorzA critical error occurred: r6   r1   )r   r6   s     r!   send_critical_errorz'NotificationService.send_critical_errord   s6     )%1e	
 	
 	
   '%'c                 R   K   | j                  ddd| d|i       d {    y 7 w)Nbot_stoppedzTrading Bot Stoppedz*The trading bot has been stopped. Reason: r   r1   )r   r   s     r!   send_bot_stoppedz$NotificationService.send_bot_stoppedl   s6     !8Av	
 	
 	
r:   detailsc                 `   K   | j                  dd| d| d| ||d       d {    y 7 w)NanomalyzAnomaly Detected: zUnusual market behavior on z: )r
   r>   r1   )r   r
   r>   s      r!   send_anomalyz NotificationService.send_anomalyt   sC      ))&G9='2	
 	
 	
s   $.,.
event_typetitlemessagepayloadc                 .  K   t         j                  d||       t        j                  r)t        j                  r| j                  |       d {    t        j                  r,t        j                  r| j                  ||       d {    y y y 7 A7 	w)Nnotification)rB   rC   )	loggerinfosettingstelegram_bot_tokentelegram_chat_id_send_telegram	smtp_host	smtp_from_send_email)r   rB   rC   rD   rE   s        r!   r   zNotificationService._notify_all|   su     NzG&&8+D+D%%g...("4"4""5'222 #5 /2s$   ABB9B	B
BBc                    K   dt         j                   d}	 t        j                         4 d {   }|j	                  |t         j
                  |dd      4 d {   }|j                  dk(  cd d d       d {    cd d d       d {    S 7 c7 77 7 # 1 d {  7  sw Y   nxY wd d d       d {  7   y # 1 d {  7  sw Y   y xY w# t        $ r*}t        j                  dt        |             Y d }~y	d }~ww xY ww)
Nzhttps://api.telegram.org/botz/sendMessageHTML)chat_idtext
parse_mode)json   telegram_send_failedr6   F)rJ   rK   aiohttpClientSessionpostrL   status	ExceptionrH   r6   str)r   rD   urlsessionrespes         r!   rM   z"NotificationService._send_telegram   s     ,X-H-H,IV		,,..'"<<%-%>%>_ef (   ;;#-	   /..  /   /....  	LL/s1vL>	s   DC BC *CBC"B1C=B>CC BC DC CCC B.	"B%#B.	*C1C <B?=C DC	C
CC DC 	D! DDDDsubjectbodyc                   K   	 dd l }ddlm}  ||      }||d<   t        j                  |d<   t        j
                  |d<   |j                  |t        j                  t        j                  t        j
                  t        j                  d       d {    y7 # t        $ r*}t        j                  dt        |      	       Y d }~y
d }~ww xY ww)Nr   )MIMETextSubjectFromToT)hostnameportusernamepassword	start_tlsemail_send_failedrY   F)
aiosmtplibemail.mime.textrg   rJ   rO   	smtp_usersendrN   	smtp_portsmtp_passwordr^   rH   r6   r_   )r   rd   re   rq   rg   msgrc   s          r!   rP   zNotificationService._send_email   s     	04.C$C	N",,CK **CI//!++''!++!// "      	LL,CFL;	sA   CBB BB CB 	C$ C	C	CC)NNNNN)NNN)r   N)__name__
__module____qualname____doc__r_   intr"   r-   r2   r5   r9   r=   rA   dictr   boolrM   rP        r!   r	   r	      s   C !%"&!%! 
 
  
 	 

  
 : 
 4Z 
 $J 
 d
 
 Dj 
 
 
N #'!%

 
 	

 4Z
 $J
 Dj
 

@
C 
D 


s 
t 

S 
T 

 
s 
t 
3C 3 3c 3TX 3]a 3C D  C D r   r	   )r{   r   r   typingr   rZ   app.core.configr   app.core.loggingr   rx   rH   rJ   r	   notification_servicer   r   r!   <module>r      s@    9 '   ( '	H	>V Vr +, r   