U
    2i                     @   sb  d Z ddlZddlZddlZddlZddlZddlmZm	Z	m
Z
 dZedddZee	eef e	ee
f edd	d
Zdeeee dddZdeeee dddZdeeee dddZd eeee dddZedkr^ddlZeejdk red ed ed ejd Zejd Zeejdkr@ejd ndZeeeeZede  dS )!z
Notification module - Sends messages to Discord and Telegram channels.
Uses urllib (built-in) instead of requests to avoid SSL/URL parsing issues.
    N)OptionalDictAnyz5/home/ubuntu/.openclaw/workspace/config/notifier.json)returnc               
   C   sX   t jtr2tt} t| W  5 Q R  S Q R X t dt dddt didS )z Load notification configuration.ZDISCORD_BOT_TOKENZDISCORD_WEBHOOK_URL)	bot_tokenZwebhook_urlr   ZTELEGRAM_BOT_TOKEN)discordtelegram)ospathexistsCONFIG_PATHopenjsonloadgetenv)f r   0/home/ubuntu/.openclaw/workspace/tools/notify.pyload_config   s    
 r   )urlheadersdatar   c              
   C   sd   d|d< t jj| t|d|dd}t jj|dd"}t| 	dW  5 Q R  S Q R X dS )	z$Make HTTP POST request using urllib.z2Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36z
User-Agentzutf-8POST)r   r   method
   )timeoutN)
urllibZrequestZRequestr   dumpsencodeZurlopenloadsreaddecode)r   r   r   ZreqZresponser   r   r   	http_post!   s    r"   )
channel_idmessagementionc           
      C   s   t  }|di d}|s$tdd|  d}|}|rHd| d| }|dg id	}d
ddd|dd| dddt|g}tj|dddd}	|	jdkrtd|	j	 t
|	jS )zRSend message to Discord channel via bot API using curl (Cloudflare blocks urllib).r   r   z Discord bot token not configuredz%https://discord.com/api/v10/channels/z	/messages@ Zparse)contentZallowed_mentionsZcurlz-sz-Xr   z-HzAuthorization: Bot zContent-Type: application/jsonz-dTr   )Zcapture_outputtextr   r   zcurl failed: )r   get
ValueErrorr   r   
subprocessrun
returncodeRuntimeErrorstderrr   stdout)
r#   r$   r%   configtokenr   r(   payloadcmdresultr   r   r   send_discord_message0   s6           
r7   )chat_idr$   r%   c           	      C   sh   t  }|di d}|s$tdd| d}|}|rHd| d| }| |dd	}d
di}t|||S )z*Send message to Telegram chat via bot API.r   r   z!Telegram bot token not configuredzhttps://api.telegram.org/botz/sendMessager&   r'   ZMarkdown)r8   r)   Z
parse_modezContent-Typezapplication/json)r   r*   r+   r"   )	r8   r$   r%   r2   r3   r   r)   r4   r   r   r   r   send_telegram_messageR   s    r9   channelr$   r%   c                 C   sV   |  dr"| dd}t|||S |  drD| dd}t|||S td|  dS )z{
    Send notification to specified channel.
    
    Channel format:
    - discord:123456789
    - telegram:123456789
    zdiscord: z	telegram:zUnknown channel format: N)
startswithreplacer7   r9   r+   )r;   r$   r%   r#   r8   r   r   r   send_notificationi   s    

r?   c                 C   s   t | ||S )z
    Send a resume command that will trigger the agent to continue work.
    This is the same as send_notification but named clearly for intent.
    )r?   r:   r   r   r   send_resume_command|   s    r@   __main__   z.Usage: notify.py <channel> <message> [mention]z9Example: notify.py discord:123456789 'Hello world' taro83      zSent: )N)N)N)N)__doc__r   r	   Zurllib.requestr   Zurllib.errorr,   typingr   r   r   r   dictr   strr"   r7   r9   r?   r@   __name__syslenargvprintexitr;   r$   r%   r6   r   r   r   r   <module>   s0   $"



