From 56bc2f0b1d36d847e2250248e7010ad61b8a2303 Mon Sep 17 00:00:00 2001 From: qiaoyun680 <549653222@qq.com> Date: Wed, 23 Oct 2024 16:38:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bntfy=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=E4=B8=AD=E6=96=87=E6=A0=87=E9=A2=98=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=BC=96=E7=A0=81=E5=90=8E=E5=8F=91=E9=80=81=20(#2541?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 1.改进ntfy通知,将中文标题进行编码后发送,可以直接展示中文标题;2.改进notify.py和notify.js中ntfy的推送; --------- Co-authored-by: qiaoyun680 --- back/services/notify.ts | 10 ++++++++-- sample/notify.js | 14 ++++++++++---- sample/notify.py | 24 +++++++++++++++++------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/back/services/notify.ts b/back/services/notify.ts index 64201278..e76ea30f 100644 --- a/back/services/notify.ts +++ b/back/services/notify.ts @@ -666,12 +666,18 @@ export default class NotificationService { private async ntfy() { const { ntfyUrl, ntfyTopic, ntfyPriority } = this.params; + // 编码函数 + const encodeRfc2047 = (text: string, charset: string = 'UTF-8'): string => { + const encodedText = Buffer.from(text).toString('base64'); + return `=?${charset}?B?${encodedText}?=`; + }; try { + const encodedTitle = encodeRfc2047(this.title); const res: any = await got .post(`${ntfyUrl || 'https://ntfy.sh'}/${ntfyTopic}`, { ...this.gotOption, - body: `${this.title}\n${this.content}`, - headers: { 'Title': 'qinglong', 'Priority': `${ntfyPriority || '3'}` }, + body: `${this.content}`, + headers: { 'Title': encodedTitle, 'Priority': `${ntfyPriority || '3'}` }, }); if (res.statusCode === 200) { return true; diff --git a/sample/notify.js b/sample/notify.js index 54f651b8..012610c9 100644 --- a/sample/notify.js +++ b/sample/notify.js @@ -1195,14 +1195,19 @@ function webhookNotify(text, desp) { } function ntfyNotify(text, desp) { + function encodeRFC2047(text) { + const encodedBase64 = Buffer.from(text).toString('base64'); + return `=?utf-8?B?${encodedBase64}?=`; + } + return new Promise((resolve) => { const { NTFY_URL, NTFY_TOPIC, NTFY_PRIORITY } = push_config; if (NTFY_TOPIC) { const options = { - url: NTFY_URL || `https://ntfy.sh`, - body: `${desp}\n${text}`, + url: `${NTFY_URL || 'https://ntfy.sh'}/${NTFY_TOPIC}`, + body: `${desp}`, headers: { - 'Title': 'qinglong', + 'Title': `${encodeRFC2047(text)}`, 'Priority': NTFY_PRIORITY || '3' }, timeout, @@ -1212,7 +1217,7 @@ function ntfyNotify(text, desp) { if (err) { console.log('Ntfy 通知调用API失败😞\n', err); } else { - if (data.success) { + if (data.id) { console.log('Ntfy 发送通知消息成功🎉\n'); } else { console.log(`Ntfy 发送通知消息异常 ${JSON.stringify(data)}`); @@ -1230,6 +1235,7 @@ function ntfyNotify(text, desp) { }); } + function parseString(input, valueFormatFn) { const regex = /(\w+):\s*((?:(?!\n\w+:).)*)/g; const matches = {}; diff --git a/sample/notify.py b/sample/notify.py index a16da51b..a6cfaa1a 100644 --- a/sample/notify.py +++ b/sample/notify.py @@ -116,7 +116,7 @@ push_config = { 'WEBHOOK_BODY': '', # 自定义通知 请求体 'WEBHOOK_HEADERS': '', # 自定义通知 请求头 'WEBHOOK_METHOD': '', # 自定义通知 请求方法 - 'WEBHOOK_CONTENT_TYPE': '' # 自定义通知 content-type + 'WEBHOOK_CONTENT_TYPE': '', # 自定义通知 content-type 'NTFY_URL': '', # ntfy地址,如https://ntfy.sh 'NTFY_TOPIC': '', # ntfy的消息应用topic @@ -789,6 +789,12 @@ def ntfy(title: str, content: str) -> None: """ 通过 Ntfy 推送消息 """ + def encode_rfc2047(text: str) -> str: + """将文本编码为符合 RFC 2047 标准的格式""" + encoded_bytes = base64.b64encode(text.encode('utf-8')) + encoded_str = encoded_bytes.decode('utf-8') + return f'=?utf-8?B?{encoded_str}?=' + if not push_config.get("NTFY_TOPIC"): print("ntfy 服务的 NTFY_TOPIC 未设置!!\n取消推送") return @@ -798,18 +804,22 @@ def ntfy(title: str, content: str) -> None: print("ntfy 服务的NTFY_PRIORITY 未设置!!默认设置为3") else: priority = push_config.get("NTFY_PRIORITY") - data = (title + "\n" +content).encode(encoding='utf-8') + + # 使用 RFC 2047 编码 title + encoded_title = encode_rfc2047(title) + + data = content.encode(encoding='utf-8') headers = { - "Title": "qinglong", + "Title": encoded_title, # 使用编码后的 title "Priority": priority } + url = push_config.get("NTFY_URL") + "/" + push_config.get("NTFY_TOPIC") response = requests.post(url, data=data, headers=headers) - - if response["code"] == 200: + if response.status_code == 200: # 使用 response.status_code 进行检查 print("Ntfy 推送成功!") else: - print("Ntfy 推送失败!错误信息:", response) + print("Ntfy 推送失败!错误信息:", response.text) def parse_headers(headers): if not headers: @@ -1014,4 +1024,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file