From 69d9307be97ab3998109bd6669dd36a4ed2bc3a3 Mon Sep 17 00:00:00 2001 From: whyour Date: Fri, 7 Jun 2024 10:54:58 +0800 Subject: [PATCH] =?UTF-8?q?bark=20=E6=8E=A8=E9=80=81=E6=94=B9=E4=B8=BA=20p?= =?UTF-8?q?ost=20=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/notify.ts | 44 ++++++++++++++++++++++------------------- sample/notify.js | 21 +++++++++++++------- sample/notify.py | 27 ++++++++++++++++--------- 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/back/services/notify.ts b/back/services/notify.ts index 528c5285..0f220aad 100644 --- a/back/services/notify.ts +++ b/back/services/notify.ts @@ -27,7 +27,7 @@ export default class NotificationService { ['aibotk', this.aibotk], ['iGot', this.iGot], ['pushPlus', this.pushPlus], - ['wePlusBot',this.wePlusBot], + ['wePlusBot', this.wePlusBot], ['email', this.email], ['pushMe', this.pushMe], ['webhook', this.webhook], @@ -209,16 +209,23 @@ export default class NotificationService { if (!barkPush.startsWith('http')) { barkPush = `https://api.day.app/${barkPush}`; } - const url = `${barkPush}/${encodeURIComponent( - this.title, - )}/${encodeURIComponent( - this.content, - )}?icon=${barkIcon}&sound=${barkSound}&group=${barkGroup}&level=${barkLevel}&url=${barkUrl}&isArchive=${barkArchive}`; + const url = `${barkPush}`; + const body = { + title: this.title, + body: this.content, + icon: barkIcon, + sound: barkSound, + group: barkGroup, + isArchive: barkArchive, + level: barkLevel, + url: barkUrl, + }; try { const res: any = await got - .get(url, { + .post(url, { ...this.gotOption, - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + json: body, + headers: { 'Content-Type': 'application/json' }, }) .json(); if (res.code === 200) { @@ -519,7 +526,7 @@ export default class NotificationService { let content = this.content; let template = 'txt'; - if(this.content.length>800){ + if (this.content.length > 800) { template = 'html'; content = content.replace(/[\n\r]/g, '
'); } @@ -612,18 +619,15 @@ export default class NotificationService { private async pushMe() { const { pushMeKey, pushMeUrl } = this.params; try { - const res: any = await got.post( - pushMeUrl || 'https://push.i-i.me/', - { - ...this.gotOption, - json: { - push_key: pushMeKey, - title: this.title, - content: this.content, - }, - headers: { 'Content-Type': 'application/json' }, + const res: any = await got.post(pushMeUrl || 'https://push.i-i.me/', { + ...this.gotOption, + json: { + push_key: pushMeKey, + title: this.title, + content: this.content, }, - ); + headers: { 'Content-Type': 'application/json' }, + }); if (res.body === 'success') { return true; } else { diff --git a/sample/notify.js b/sample/notify.js index 966673fa..0379fba6 100644 --- a/sample/notify.js +++ b/sample/notify.js @@ -358,17 +358,24 @@ function barkNotify(text, desp, params = {}) { BARK_PUSH = `https://api.day.app/${BARK_PUSH}`; } const options = { - url: `${BARK_PUSH}/${encodeURIComponent(text)}/${encodeURIComponent( - desp, - )}?icon=${BARK_ICON}&sound=${BARK_SOUND}&group=${BARK_GROUP}&isArchive=${BARK_ARCHIVE}&level=${BARK_LEVEL}&url=${BARK_URL}&${querystring.stringify( - params, - )}`, + url: `${BARK_PUSH}`, + json: { + title: text, + body: desp, + icon: BARK_ICON, + sound: BARK_SOUND, + group: BARK_GROUP, + isArchive: BARK_ARCHIVE, + level: BARK_LEVEL, + url: BARK_URL, + ...params, + }, headers: { - 'Content-Type': 'application/x-www-form-urlencoded', + 'Content-Type': 'application/json', }, timeout, }; - $.get(options, (err, resp, data) => { + $.post(options, (err, resp, data) => { try { if (err) { console.log('Bark APP 发送通知调用API失败😞\n', err); diff --git a/sample/notify.py b/sample/notify.py index 82fbee76..fe3bb53e 100644 --- a/sample/notify.py +++ b/sample/notify.py @@ -137,9 +137,9 @@ def bark(title: str, content: str) -> None: print("bark 服务启动") if push_config.get("BARK_PUSH").startswith("http"): - url = f'{push_config.get("BARK_PUSH")}/{urllib.parse.quote_plus(title)}/{urllib.parse.quote_plus(content)}' + url = f'{push_config.get("BARK_PUSH")}' else: - url = f'https://api.day.app/{push_config.get("BARK_PUSH")}/{urllib.parse.quote_plus(title)}/{urllib.parse.quote_plus(content)}' + url = f'https://api.day.app/{push_config.get("BARK_PUSH")}' bark_params = { "BARK_ARCHIVE": "isArchive", @@ -149,7 +149,10 @@ def bark(title: str, content: str) -> None: "BARK_LEVEL": "level", "BARK_URL": "url", } - params = "" + data = { + "title": title, + "body": content, + } for pair in filter( lambda pairs: pairs[0].startswith("BARK_") and pairs[0] != "BARK_PUSH" @@ -157,10 +160,11 @@ def bark(title: str, content: str) -> None: and bark_params.get(pairs[0]), push_config.items(), ): - params += f"{bark_params.get(pair[0])}={pair[1]}&" - if params: - url = url + "?" + params.rstrip("&") - response = requests.get(url).json() + data[bark_params.get(pair[0])] = pair[1] + headers = {"Content-Type": "application/json;charset=utf-8"} + response = requests.post( + url=url, data=json.dumps(data), headers=headers, timeout=15 + ).json() if response["code"] == 200: print("bark 推送成功!") @@ -385,6 +389,7 @@ def pushplus_bot(title: str, content: str) -> None: else: print("PUSHPLUS 推送失败!") + def weplus_bot(title: str, content: str) -> None: """ 通过 微加机器人 推送消息。 @@ -396,7 +401,7 @@ def weplus_bot(title: str, content: str) -> None: template = "txt" if len(content) > 800: - template = "html" + template = "html" url = "https://www.weplusbot.com/send" data = { @@ -704,7 +709,11 @@ def pushme(title: str, content: str) -> None: return print("PushMe 服务启动") - url = push_config.get("PUSHME_URL") if push_config.get("PUSHME_URL") else "https://push.i-i.me/" + url = ( + push_config.get("PUSHME_URL") + if push_config.get("PUSHME_URL") + else "https://push.i-i.me/" + ) data = { "push_key": push_config.get("PUSHME_KEY"), "title": title,