11import json
22import requests
3+ from datetime import datetime
34from urllib .parse import urljoin
45from models import NotifyChannel
56
67
78class NotificationSender :
89 """通知发送器基类"""
910
11+ @staticmethod
12+ def _process_template (text : str ) -> str :
13+ """处理文本中的变量模板"""
14+ if not text :
15+ return text
16+
17+ now = datetime .now ()
18+ weekdays = ['Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' , 'Sunday' ]
19+ weekdays_cn = ['星期一' , '星期二' , '星期三' , '星期四' , '星期五' , '星期六' , '星期日' ]
20+
21+ replacements = {
22+ '{{date}}' : now .strftime ('%Y-%m-%d' ),
23+ '{{time}}' : now .strftime ('%H:%M:%S' ),
24+ '{{datetime}}' : now .strftime ('%Y-%m-%d %H:%M:%S' ),
25+ '{{year}}' : now .strftime ('%Y' ),
26+ '{{month}}' : now .strftime ('%m' ),
27+ '{{day}}' : now .strftime ('%d' ),
28+ '{{hour}}' : now .strftime ('%H' ),
29+ '{{minute}}' : now .strftime ('%M' ),
30+ '{{second}}' : now .strftime ('%S' ),
31+ '{{timestamp}}' : str (int (now .timestamp ())),
32+ '{{weekday}}' : weekdays [now .weekday ()],
33+ '{{weekday_cn}}' : weekdays_cn [now .weekday ()]
34+ }
35+
36+ for key , value in replacements .items ():
37+ text = text .replace (key , value )
38+
39+ return text
40+
1041 @staticmethod
1142 def send (channel : NotifyChannel , config : dict , title : str , content : str ):
1243 """
@@ -21,6 +52,10 @@ def send(channel: NotifyChannel, config: dict, title: str, content: str):
2152 Returns:
2253 bool: 发送是否成功
2354 """
55+ # 处理变量替换
56+ title = NotificationSender ._process_template (title )
57+ content = NotificationSender ._process_template (content )
58+
2459 try :
2560 if channel == NotifyChannel .WECOM :
2661 return NotificationSender ._send_wecom (config , title , content )
0 commit comments