
    iWi                         d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZ  e j        e          Z G d d          Z e            ZdS )    N)Optional)BotUpdate)ApplicationCommandHandlerContextTypes)settingsc                   |    e Zd ZdZd Zdee         fdZdefdZ	d Z
d Zd	 Zd
efdZd Zedefd            ZdS )TelegramServicezd
    Sends trade alerts to a Telegram chat.
    Also handles /status, /pause, /resume commands.
    c                 0    d | _         d | _        d| _        d S )NT)_bot_app_bot_runningselfs    A/var/www/html/crypto-bot/backend/app/services/telegram_service.py__init__zTelegramService.__init__   s    #'	+/	     returnc                 x    t           j        sd S | j        t          t           j                  | _        | j        S )N)token)r	   TELEGRAM_BOT_TOKENr   r   r   s    r   _get_botzTelegramService._get_bot   s7    * 	49("=>>>DIyr   textc                 B  K   |                                  }|t          j        st                              d           dS 	 |                    t          j        |d           d{V  dS # t          $ r(}t                              d|            Y d}~dS d}~ww xY w)z:Send a plain-text message to the configured Telegram chat.Nz)Telegram not configured, skipping messageHTML)chat_idr   
parse_modezTelegram send error: )r   r	   TELEGRAM_CHAT_IDloggerdebugsend_message	Exceptionerror)r   r   botes       r   r"   zTelegramService.send_message   s      mmoo;h7;LLDEEEF	6"" 1! #           
  	6 	6 	6LL444555555555	6s   (A, ,
B6BBc                   K   d|j          d|j         d|j         d|j         d|j         d|j        r|j        d         nd d	t          |j                  d
k    r|j        d
         nd }|                     |           d {V  d S )Nz<b>Trade Opened</b>
Pair: 	
Signal: z
Mode: 
Entry: z
SL: z
TP1: r   zN/Az
TP2:    )pairsignalmodeentry_price	stop_losstake_profit_levelslenr"   r   trader   s      r   notify_trade_openedz#TelegramService.notify_trade_opened,   s      bZb b|b b Zb b '	b b
 ?b b 493KVE,Q//QVb b 47u7O3P3PST3T3TE,Q//Z_b b 	 %%%%%%%%%%%r   c                    K   |j         dk    rdnd}d| d|j         d|j         d|j         d|j         d|j        d	d
}|                     |           d {V  d S )NWINLOSSu   <b>Trade Closed — z</b>
Pair: r(   r)   z
Exit: 
PnL: .2f%)resultr+   r,   r.   current_pricepnl_percentager"   )r   r3   emojir   s       r   notify_trade_closedz#TelegramService.notify_trade_closed9   s      ..F05 0 0Z0 0|0 0 '0 0 (	0 0
 (/0 0 0 	 %%%%%%%%%%%r   c                    K   d|j          d|j         d|j         d|j         d|j        dd}|                     |           d {V  d S )Nz<b>Stop Loss Hit</b>
Pair:  | Signal: r)   z | SL: r8   r9   r:   )r+   r,   r.   r/   r=   r"   r2   s      r   notify_sl_hitzTelegramService.notify_sl_hitE   s      0Z0 0,1L0 0'0 0050 0 (/0 0 0 	 %%%%%%%%%%%r   tp_indexc                    K   d|dz    d|j          d|j         d|dz    d|j        |          
}|                     |           d {V  d S )Nz<b>Take Profit r*   z Hit</b>
Pair: rA   z
TPz: )r+   r,   r0   r"   )r   r3   rC   r   s       r   notify_tp_hitzTelegramService.notify_tp_hitN   s      Dhl D DZD D,1LD D!D D$7AD D 	
 %%%%%%%%%%%r   c                 <    t           j        st                              d           dS dt          dt
          j        f fddt          dt
          j        f fddt          dt
          j        f fd fd}t          j         |                       dS )	zw
        Start the Telegram bot command listener in the background.
        Supports: /status, /pause, /resume
        z4Telegram token not set, command listener not startedNupdatecontextc                 h   K   j         rdnd}| j                            d|            d {V  d S )NRUNNINGPAUSEDzBot status: r   message
reply_text)rG   rH   stater   s      r   status_handlerz>TelegramService.start_command_listener.<locals>.status_handler_   sO      !%!2@IIE.++,B5,B,BCCCCCCCCCCCr   c                 Z   K   d_         | j                            d           d {V  d S )NFz*Bot PAUSED. New trades will not be opened.rL   rG   rH   r   s     r   pause_handlerz=TelegramService.start_command_listener.<locals>.pause_handlerc   s>       %D.++,XYYYYYYYYYYYr   c                 Z   K   d_         | j                            d           d {V  d S )NTzBot RESUMED. Trading active.rL   rR   s     r   resume_handlerz>TelegramService.start_command_listener.<locals>.resume_handlerg   s>       $D.++,JKKKKKKKKKKKr   c                    K   t          j                                        t          j                                                  } |                     t          d                     |                     t          d                     |                     t          d                     | _        | 	                                 d {V  | 
                                 d {V  | j                                         d {V  d S )Nstatuspauseresume)r   builderr   r	   r   buildadd_handlerr   r   
initializestartupdaterstart_polling)apprS   rU   r   rP   s    r   runz3TelegramService.start_command_listener.<locals>.runk   s      %''--h.IJJPPRRCOON8^DDEEEOON7MBBCCCOON8^DDEEEDI.."""""""""))+++++-----------r   )	r	   r   r    infor   r   DEFAULT_TYPEasynciocreate_task)r   rb   rS   rU   rP   s   ` @@@r   start_command_listenerz&TelegramService.start_command_listenerV   s   
 * 	KKNOOOF	D 	D,:S 	D 	D 	D 	D 	D 	D	Z 	Z9R 	Z 	Z 	Z 	Z 	Z 	Z	L 	L,:S 	L 	L 	L 	L 	L 	L	. 	. 	. 	. 	. 	. 	. 	. 	CCEE"""""r   c                     | j         S )N)r   r   s    r   
is_runningzTelegramService.is_runningw   s      r   N)__name__
__module____qualname____doc__r   r   r   r   strr"   r4   r?   rB   intrE   rg   propertyboolri    r   r   r   r      s         
! ! !
(3-    6s 6 6 6 6& & &
& 
& 
&& & &&3 & & & &# # #B !D ! ! ! X! ! !r   r   )loggingre   typingr   telegramr   r   telegram.extr   r   r   
app.configr	   	getLoggerrj   r    r   telegram_servicerr   r   r   <module>rz      s                            B B B B B B B B B B      		8	$	$n! n! n! n! n! n! n! n!b #?$$   r   