Files
sendNotify-jdpro/gotifytest.js
2022-03-02 00:45:11 +08:00

68 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let GOTIFY_URL = 'https://pushplus.mcraft.cn';
let GOTIFY_TOKEN = 'jdprotoken213';
let GOTIFY_PRIORITY = 0;
sendNotify('mytitle', 'despLine1\nSecond Line');
/**
* sendNotify 推送通知功能
* @param text 通知头
* @param desp 通知体
* @param params 某些推送通知方式点击弹窗可跳转, 例:{ url: 'https://abc.com' }
* @param author 作者仓库等信息 例:`本通知 Byhttps://github.com/whyour/qinglong`
* @returns {Promise<unknown>}
*/
async function sendNotify(
text,
desp,
params = {},
author = '\n\n本通知 Byhttps://github.com/whyour/qinglong',
) {
//提供6种通知
desp += author; //增加作者信息,防止被贩卖等
await Promise.all([
pushPlusNotify(text, desp), //pushplus(推送加)
]);
//由于上述两种微信通知需点击进去才能查看到详情故text(标题内容)携带了账号序号以及昵称信息,方便不点击也可知道是哪个京东哪个活动
text = text.match(/.*?(?=\s?-)/g) ? text.match(/.*?(?=\s?-)/g)[0] : text;
await Promise.all([
gotifyNotify(text, desp), //gotify
]);
}
function gotifyNotify(text, desp) {
return new Promise((resolve) => {
if (GOTIFY_URL && GOTIFY_TOKEN) {
const options = {
url: `${GOTIFY_URL}/message?token=${GOTIFY_TOKEN}`,
body: `title=${encodeURIComponent(text)}&message=${encodeURIComponent(
desp,
)}&priority=${GOTIFY_PRIORITY}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
};
$.post(options, (err, resp, data) => {
try {
if (err) {
console.log('gotify发送通知调用API失败\n');
console.log(err);
} else {
data = JSON.parse(data);
if (data.id) {
console.log('gotify发送通知消息成功🎉\n');
} else {
console.log(`${data.message}\n`);
}
}
} catch (e) {
$.logErr(e, resp);
} finally {
resolve();
}
});
} else {
resolve();
}
});
}