From 53bdc7fd5da18c618ab31e0f42d94ffd2e469be8 Mon Sep 17 00:00:00 2001 From: NekoMio Date: Mon, 28 Feb 2022 20:03:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0pushdeer=20=E7=9A=84=20sample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/notify.ts | 4 ++- sample/config.sample.sh | 4 +++ sample/notify.js | 55 +++++++++++++++++++++++++++++++++++++++++ sample/notify.py | 22 +++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/back/services/notify.ts b/back/services/notify.ts index dd5adb76..a8884f7b 100644 --- a/back/services/notify.ts +++ b/back/services/notify.ts @@ -129,7 +129,9 @@ export default class NotificationService { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, }) .json(); - return res.content.result.length > 0; + return ( + res.content.result.length !== undefined && res.content.result.length > 0 + ); } private async bark() { diff --git a/sample/config.sample.sh b/sample/config.sample.sh index b70ebd31..eb36d85b 100644 --- a/sample/config.sample.sh +++ b/sample/config.sample.sh @@ -125,4 +125,8 @@ export GOTIFY_URL="" export GOTIFY_TOKEN="" export GOTIFY_PRIORITY=0 +## 11. PushDeer +## deer_key 填写PushDeer的key +export DEER_KEY="" + ## 其他需要的变量,脚本中需要的变量使用 export 变量名= 声明即可 diff --git a/sample/notify.js b/sample/notify.js index 18dfc989..133a3d2d 100644 --- a/sample/notify.js +++ b/sample/notify.js @@ -34,6 +34,11 @@ let GOBOT_QQ = ''; // 如果GOBOT_URL设置 /send_private_msg 则需要填入 us //(环境变量名 PUSH_KEY) let SCKEY = ''; +// =======================================PushDeer通知设置区域=========================================== +//此处填你申请的PushDeer KEY. +//(环境变量名 DEER_KEY) +let PUSHDEER_KEY = ''; + // =======================================Bark App通知设置区域=========================================== //此处填你BarkAPP的信息(IP/设备码,例如:https://api.day.app/XXXXXXXX) let BARK_PUSH = ''; @@ -118,6 +123,10 @@ if (process.env.PUSH_KEY) { SCKEY = process.env.PUSH_KEY; } +if (process.env.DEER_KEY) { + PUSHDEER_KEY = process.env.DEER_KEY; +} + if (process.env.QQ_SKEY) { QQ_SKEY = process.env.QQ_SKEY; } @@ -354,6 +363,52 @@ function serverNotify(text, desp, time = 2100) { }); } +function PushDeerNotify(text, desp, time = 2100) { + return new Promise((resolve) => { + if (PUSHDEER_KEY) { + // PushDeer 建议对消息内容进行 urlencode + desp = encodeURI(desp); + const options = { + url: `https://api2.pushdeer.com/message/push`, + body: `pushkey=${PUSHDEER_KEY}&text=${text}&desp=${desp}&type=markdown`, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + timeout, + }; + setTimeout(() => { + $.post(options, (err, resp, data) => { + try { + if (err) { + console.log('发送通知调用API失败!!\n'); + console.log(err); + } else { + data = JSON.parse(data); + // 通过反悔的result的长度来判断是否成功 + if ( + data.content.result.length !== undefined && + data.content.result.length > 0 + ) { + console.log('PushDeer发送通知消息成功🎉\n'); + } else { + console.log( + `PushDeer发送通知消息异常\n${JSON.stringify(data)}`, + ); + } + } + } catch (e) { + $.logErr(e, resp); + } finally { + resolve(data); + } + }); + }, time); + } else { + resolve(); + } + }); +} + function CoolPush(text, desp) { return new Promise((resolve) => { if (QQ_SKEY) { diff --git a/sample/notify.py b/sample/notify.py index 75c3bbd2..7f2ec25d 100644 --- a/sample/notify.py +++ b/sample/notify.py @@ -60,6 +60,8 @@ push_config = { 'PUSH_KEY': '', # server 酱的 PUSH_KEY,兼容旧版与 Turbo 版 + 'DEER_KEY': '', # PushDeer 的 PUSHDEER_KEY + 'PUSH_PLUS_TOKEN': '', # push+ 微信推送的用户令牌 'PUSH_PLUS_USER': '', # push+ 微信推送的群组编码 @@ -261,6 +263,24 @@ def serverJ(title: str, content: str) -> None: print(f'serverJ 推送失败!错误码:{response["message"]}') +def pushdeer(title: str, content: str) -> None: + """ + 通过PushDeer 推送消息 + """ + if not push_config.get("DEER_KEY"): + print("PushDeer 服务的 DEER_KEY 未设置!!\n取消推送") + return + print("PushDeer 服务启动") + data = {"text": title, "desp": urllib.parse.urlencode({"text": content})} + url = 'https://api2.pushdeer.com/message/push' + response = requests.post(url, data=data).json() + + if len(response.get("content").get("result")) > 0: + print("PushDeer 推送成功!") + else: + print("PushDeer 推送失败!错误信息:", response) + + def pushplus_bot(title: str, content: str) -> None: """ 通过 push+ 推送消息。 @@ -505,6 +525,8 @@ if push_config.get("IGOT_PUSH_KEY"): notify_function.append(iGot) if push_config.get("PUSH_KEY"): notify_function.append(serverJ) +if push_config.get("DEER_KEY"): + notify_function.append(pushdeer) if push_config.get("PUSH_PLUS_TOKEN"): notify_function.append(pushplus_bot) if push_config.get("QMSG_KEY") and push_config.get("QMSG_TYPE"):