bark 推送改为 post 请求

This commit is contained in:
whyour 2024-06-07 10:54:58 +08:00
parent c3908e956f
commit 69d9307be9
3 changed files with 56 additions and 36 deletions

View File

@ -27,7 +27,7 @@ export default class NotificationService {
['aibotk', this.aibotk], ['aibotk', this.aibotk],
['iGot', this.iGot], ['iGot', this.iGot],
['pushPlus', this.pushPlus], ['pushPlus', this.pushPlus],
['wePlusBot',this.wePlusBot], ['wePlusBot', this.wePlusBot],
['email', this.email], ['email', this.email],
['pushMe', this.pushMe], ['pushMe', this.pushMe],
['webhook', this.webhook], ['webhook', this.webhook],
@ -209,16 +209,23 @@ export default class NotificationService {
if (!barkPush.startsWith('http')) { if (!barkPush.startsWith('http')) {
barkPush = `https://api.day.app/${barkPush}`; barkPush = `https://api.day.app/${barkPush}`;
} }
const url = `${barkPush}/${encodeURIComponent( const url = `${barkPush}`;
this.title, const body = {
)}/${encodeURIComponent( title: this.title,
this.content, body: this.content,
)}?icon=${barkIcon}&sound=${barkSound}&group=${barkGroup}&level=${barkLevel}&url=${barkUrl}&isArchive=${barkArchive}`; icon: barkIcon,
sound: barkSound,
group: barkGroup,
isArchive: barkArchive,
level: barkLevel,
url: barkUrl,
};
try { try {
const res: any = await got const res: any = await got
.get(url, { .post(url, {
...this.gotOption, ...this.gotOption,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, json: body,
headers: { 'Content-Type': 'application/json' },
}) })
.json(); .json();
if (res.code === 200) { if (res.code === 200) {
@ -519,7 +526,7 @@ export default class NotificationService {
let content = this.content; let content = this.content;
let template = 'txt'; let template = 'txt';
if(this.content.length>800){ if (this.content.length > 800) {
template = 'html'; template = 'html';
content = content.replace(/[\n\r]/g, '<br>'); content = content.replace(/[\n\r]/g, '<br>');
} }
@ -612,9 +619,7 @@ export default class NotificationService {
private async pushMe() { private async pushMe() {
const { pushMeKey, pushMeUrl } = this.params; const { pushMeKey, pushMeUrl } = this.params;
try { try {
const res: any = await got.post( const res: any = await got.post(pushMeUrl || 'https://push.i-i.me/', {
pushMeUrl || 'https://push.i-i.me/',
{
...this.gotOption, ...this.gotOption,
json: { json: {
push_key: pushMeKey, push_key: pushMeKey,
@ -622,8 +627,7 @@ export default class NotificationService {
content: this.content, content: this.content,
}, },
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}, });
);
if (res.body === 'success') { if (res.body === 'success') {
return true; return true;
} else { } else {

View File

@ -358,17 +358,24 @@ function barkNotify(text, desp, params = {}) {
BARK_PUSH = `https://api.day.app/${BARK_PUSH}`; BARK_PUSH = `https://api.day.app/${BARK_PUSH}`;
} }
const options = { const options = {
url: `${BARK_PUSH}/${encodeURIComponent(text)}/${encodeURIComponent( url: `${BARK_PUSH}`,
desp, json: {
)}?icon=${BARK_ICON}&sound=${BARK_SOUND}&group=${BARK_GROUP}&isArchive=${BARK_ARCHIVE}&level=${BARK_LEVEL}&url=${BARK_URL}&${querystring.stringify( title: text,
params, body: desp,
)}`, icon: BARK_ICON,
sound: BARK_SOUND,
group: BARK_GROUP,
isArchive: BARK_ARCHIVE,
level: BARK_LEVEL,
url: BARK_URL,
...params,
},
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/json',
}, },
timeout, timeout,
}; };
$.get(options, (err, resp, data) => { $.post(options, (err, resp, data) => {
try { try {
if (err) { if (err) {
console.log('Bark APP 发送通知调用API失败😞\n', err); console.log('Bark APP 发送通知调用API失败😞\n', err);

View File

@ -137,9 +137,9 @@ def bark(title: str, content: str) -> None:
print("bark 服务启动") print("bark 服务启动")
if push_config.get("BARK_PUSH").startswith("http"): 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: 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_params = {
"BARK_ARCHIVE": "isArchive", "BARK_ARCHIVE": "isArchive",
@ -149,7 +149,10 @@ def bark(title: str, content: str) -> None:
"BARK_LEVEL": "level", "BARK_LEVEL": "level",
"BARK_URL": "url", "BARK_URL": "url",
} }
params = "" data = {
"title": title,
"body": content,
}
for pair in filter( for pair in filter(
lambda pairs: pairs[0].startswith("BARK_") lambda pairs: pairs[0].startswith("BARK_")
and pairs[0] != "BARK_PUSH" and pairs[0] != "BARK_PUSH"
@ -157,10 +160,11 @@ def bark(title: str, content: str) -> None:
and bark_params.get(pairs[0]), and bark_params.get(pairs[0]),
push_config.items(), push_config.items(),
): ):
params += f"{bark_params.get(pair[0])}={pair[1]}&" data[bark_params.get(pair[0])] = pair[1]
if params: headers = {"Content-Type": "application/json;charset=utf-8"}
url = url + "?" + params.rstrip("&") response = requests.post(
response = requests.get(url).json() url=url, data=json.dumps(data), headers=headers, timeout=15
).json()
if response["code"] == 200: if response["code"] == 200:
print("bark 推送成功!") print("bark 推送成功!")
@ -385,6 +389,7 @@ def pushplus_bot(title: str, content: str) -> None:
else: else:
print("PUSHPLUS 推送失败!") print("PUSHPLUS 推送失败!")
def weplus_bot(title: str, content: str) -> None: def weplus_bot(title: str, content: str) -> None:
""" """
通过 微加机器人 推送消息 通过 微加机器人 推送消息
@ -704,7 +709,11 @@ def pushme(title: str, content: str) -> None:
return return
print("PushMe 服务启动") 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 = { data = {
"push_key": push_config.get("PUSHME_KEY"), "push_key": push_config.get("PUSHME_KEY"),
"title": title, "title": title,