qinlong-js/test.js

103 lines
2.8 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.

const got = require('got');
const timeout = 15000;
const push_config = {
HITOKOTO: true, // 启用一言(随机句子)
// 官方文档http://www.pushplus.plus/
PUSH_PLUS_TOKEN: '79e01a66036642e5969d52a5952b5cb7', // push+ 微信推送的用户令牌
PUSH_PLUS_USER: '', // push+ 微信推送的群组编码
WEBHOOK_URL: '', // 自定义通知 请求地址
WEBHOOK_BODY: '', // 自定义通知 请求体
WEBHOOK_HEADERS: '', // 自定义通知 请求头
WEBHOOK_METHOD: '', // 自定义通知 请求方法
WEBHOOK_CONTENT_TYPE: '', // 自定义通知 content-type
NTFY_URL: '', // ntfy地址,如https://ntfy.sh,默认为https://ntfy.sh
NTFY_TOPIC: '', // ntfy的消息应用topic
NTFY_PRIORITY: '3', // 推送消息优先级,默认为3
};
const $ = {
post: (params, callback) => {
const { url, ...others } = params;
got.post(url, others).then(
(res) => {
let body = res.body;
try {
body = JSON.parse(body);
} catch (error) {}
callback(null, res, body);
},
(err) => {
callback(err?.response?.body || err);
}
);
},
get: (params, callback) => {
const { url, ...others } = params;
got.get(url, others).then(
(res) => {
let body = res.body;
try {
body = JSON.parse(body);
} catch (error) {}
callback(null, res, body);
},
(err) => {
callback(err?.response?.body || err);
}
);
},
logErr: console.log,
};
function pushPlusNotify(text, desp) {
return new Promise((resolve) => {
const { PUSH_PLUS_TOKEN, PUSH_PLUS_USER } = push_config;
if (PUSH_PLUS_TOKEN) {
desp = desp.replace(/[\n\r]/g, '<br>'); // 默认为html, 不支持plaintext
const body = {
token: `${PUSH_PLUS_TOKEN}`,
title: `${text}`,
content: `${desp}`,
topic: `${PUSH_PLUS_USER}`,
};
const options = {
url: `https://www.pushplus.plus/send`,
body: JSON.stringify(body),
headers: {
'Content-Type': ' application/json',
},
timeout,
};
$.post(options, (err, resp, data) => {
try {
if (err) {
console.log(`Push+ 发送${PUSH_PLUS_USER ? '一对多' : '一对一'}通知消息失败😞\n`, err);
} else {
if (data.code === 200) {
console.log(`Push+ 发送${PUSH_PLUS_USER ? '一对多' : '一对一'}通知消息完成🎉\n`);
} else {
console.log(
`Push+ 发送${PUSH_PLUS_USER ? '一对多' : '一对一'}通知消息异常 ${data.msg}\n`
);
}
}
} catch (e) {
$.logErr(e, resp);
} finally {
resolve(data);
}
});
} else {
resolve();
}
});
}
(async () => {
await pushPlusNotify('测试', '测试');
})();