Skip to content

Commit

Permalink
bark 推送改为 post 请求
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Jun 7, 2024
1 parent c3908e9 commit 69d9307
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
44 changes: 24 additions & 20 deletions back/services/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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, '<br>');
}
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 14 additions & 7 deletions sample/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 18 additions & 9 deletions sample/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -149,18 +149,22 @@ 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"
and pairs[1]
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 推送成功!")
Expand Down Expand Up @@ -385,6 +389,7 @@ def pushplus_bot(title: str, content: str) -> None:
else:
print("PUSHPLUS 推送失败!")


def weplus_bot(title: str, content: str) -> None:
"""
通过 微加机器人 推送消息。
Expand All @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 69d9307

Please sign in to comment.