Skip to content

Commit f81692d

Browse files
committed
消息正文内容支持时间变量
1 parent a5b6c20 commit f81692d

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

notifier.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
import json
22
import requests
3+
from datetime import datetime
34
from urllib.parse import urljoin
45
from models import NotifyChannel
56

67

78
class 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)

static/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ <h2>创建通知任务</h2>
118118
<div class="form-group">
119119
<label for="content">通知内容 *</label>
120120
<textarea id="content" name="content" placeholder="请输入通知内容" required></textarea>
121+
<small style="color: var(--text-muted); display: block; margin-top: 5px; line-height: 1.5;">
122+
支持变量: {{date}} 日期, {{time}} 时间, {{datetime}} 完整时间<br>
123+
{{year}} 年, {{month}} 月, {{day}} 日, {{weekday_cn}} 星期, {{timestamp}} 时间戳
124+
</small>
121125
</div>
122126

123127
<div class="form-group">
@@ -301,6 +305,10 @@ <h2>编辑任务</h2>
301305
<div class="form-group">
302306
<label for="editContent">通知内容 *</label>
303307
<textarea id="editContent" name="content" placeholder="请输入通知内容" required></textarea>
308+
<small style="color: var(--text-muted); display: block; margin-top: 5px; line-height: 1.5;">
309+
支持变量: {{date}} 日期, {{time}} 时间, {{datetime}} 完整时间<br>
310+
{{year}} 年, {{month}} 月, {{day}} 日, {{weekday_cn}} 星期, {{timestamp}} 时间戳
311+
</small>
304312
</div>
305313

306314
<div class="form-group">

version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version: 0.4.1
1+
version: 0.5.0

0 commit comments

Comments
 (0)