Update sendNotify.js

This commit is contained in:
smallfawn
2024-02-09 16:15:41 +08:00
committed by GitHub
parent 5c83921b98
commit b9644784cf

View File

@@ -73,7 +73,7 @@ let TG_PROXY_HOST = ''; //例如:127.0.0.1(环境变量名:TG_PROXY_HOST)
let TG_PROXY_PORT = ''; // 例如:1080(环境变量名:TG_PROXY_PORT)
let TG_PROXY_AUTH = ''; // tg代理配置认证参数
// Telegram api自建的反向代理地址(不懂可忽略,telegram机器人通知推送功能中非必填),默认tg官方api(环境变量名:TG_API_HOST)
let TG_API_HOST = 'api.telegram.org';
let TG_API_HOST = 'https://tgbot.smallfawn.top';
// =======================================钉钉机器人通知设置区域===========================================
// 此处填你钉钉 bot 的webhook例如5a544165465465645d0f31dca676e7bd07415asdasd
// (环境变量名 DD_BOT_TOKEN)
@@ -113,13 +113,6 @@ let IGOT_PUSH_KEY = '';
let PUSH_PLUS_TOKEN = '';
let PUSH_PLUS_USER = '';
// =======================================Cool Push设置区域=======================================
//官方文档https://cp.xuthus.cc/docs
//QQ_SKEY: Cool Push登录授权后推送消息的调用代码Skey
//QQ_MODE: 推送模式详情请登录获取QQ_SKEY后见https://cp.xuthus.cc/feat
let QQ_SKEY = '';
let QQ_MODE = '';
// =======================================智能微秘书设置区域=======================================
// 官方文档http://wechat.aibotk.com/docs/about
// AIBOTK_KEY 填写智能微秘书个人中心的apikey
@@ -135,13 +128,11 @@ let AIBOTK_NAME = '';
let FSKEY = '';
// =======================================SMTP 邮件设置区域=======================================
// SMTP_SERVER: 填写 SMTP 发送邮件服务器,形如 smtp.exmail.qq.com:465
// SMTP_SSL: 填写 SMTP 发送邮件服务器是否使用 SSL内容应为 true 或 false
// SMTP_SERVICE: 邮箱服务名称比如126、163、Gmail、QQ等支持列表 https://github.com/nodemailer/nodemailer/blob/master/lib/well-known/services.json
// SMTP_EMAIL: 填写 SMTP 收发件邮箱,通知将会由自己发给自己
// SMTP_PASSWORD: 填写 SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定
// SMTP_NAME: 填写 SMTP 收发件人姓名,可随意填写
let SMTP_SERVER = '';
let SMTP_SSL = 'false';
let SMTP_SERVICE = '';
let SMTP_EMAIL = '';
let SMTP_PASSWORD = '';
let SMTP_NAME = '';
@@ -306,11 +297,8 @@ if (process.env.FSKEY) {
FSKEY = process.env.FSKEY;
}
if (process.env.SMTP_SERVER) {
SMTP_SERVER = process.env.SMTP_SERVER;
}
if (process.env.SMTP_SSL) {
SMTP_SSL = process.env.SMTP_SSL;
if (process.env.SMTP_SERVICE) {
SMTP_SERVICE = process.env.SMTP_SERVICE;
}
if (process.env.SMTP_EMAIL) {
SMTP_EMAIL = process.env.SMTP_EMAIL;
@@ -366,72 +354,6 @@ async function sendNotify(
params = {},
author = '\n\n本通知 Byhttps://github.com/whyour/qinglong',
) {
let pushType = ["smallfawnPushWhite", "smallfawnPushBlack", 'default']
/* 判断 黑白名单 或默认模式 */
function checkSmallfawnPushType() {
if (process.env.hasOwnProperty(pushType[0]) !== false) {
return pushType[0];
} else if (process.env.hasOwnProperty(pushType[1]) !== false) {
return pushType[1];
} else {
return pushType[2];
}
}
/**
* 检测 smallfawnPushWhite 或 smallfawnPushBlack 的变量
* @param {*} variable smallfawnPushWhite 或 smallfawnPushBlack
* @returns 数组
*/
function checkSmallfawnPush(variable) {
if (typeof variable === 'string') {
if (variable.includes('&') || variable.includes('#') || variable.includes('@')) {
return variable.split(/[&#@]/);
} else {
return [variable];
}
} else if (typeof variable === 'number') {
return [variable.toString()];
} else {
return [];
}
}
let type = checkSmallfawnPushType()
console.log(`\n\n============================================`)
// 主要 //
//判断是否是默认通知方式
if (type !== pushType[2]) {
//判断数组长度
if (checkSmallfawnPush(process.env[type]).length == 0) {
console.log(`通知变量[${type}] 无变量值 默认形式发送 脚本名字[${text}]\n如果需要请填写脚本名字到通知变量 @或&或#为 分隔符`);
await send()
} else {
//判断黑白名单模式
if (type == pushType[0]) {
console.log(`通知变量为白名单模式`);
if (checkSmallfawnPush(process.env[pushType[0]]).includes(text)) {
console.log(`脚本名字[${text}] 在 通知变量白名单[${type}] 变量中 => 通知`);
await send();
} else {
console.log(`脚本名字[${text}] 不在 通知变量白名单[${type}] 变量中 => 不通知`)
}
} else if (type == pushType[1]) {
console.log(`通知变量为黑名单模式`);
if (checkSmallfawnPush(process.env[pushType[1]]).includes(text)) {
console.log(`脚本名字[${text}] 在 通知变量黑名单[${type}] 变量中 => 不通知`);
} else {
console.log(`脚本名字[${text}] 不在 通知变量黑名单[${type}] 变量中 => 通知`)
await send();
}
}
}
} else {
console.log(`无黑白名单变量 默认形式发送 脚本名字[${text}]`);
console.log(`如需配置通知黑白名单请在环境变量或配置文件 添加变量白名单 ${pushType[0]} 或 黑名单 ${pushType[1]}二选一 @或&或#为 分隔符 填写脚本名字`);
await send()
}
async function send() {
// 提供6种通知
desp += author; // 增加作者信息,防止被贩卖等
@@ -464,14 +386,12 @@ async function sendNotify(
aibotkNotify(text, desp), // 智能微秘书
fsBotNotify(text, desp), // 飞书机器人
smtpNotify(text, desp), // SMTP 邮件
PushMeNotify(text, desp, params), //PushMe
ChronocatNotify(text, desp), // Chronocat
pushMeNotify(text, desp, params), // PushMe
chronocatNotify(text, desp), // Chronocat
webhookNotify(text, desp), // 自定义通知
]);
}
}
function gotifyNotify(text, desp) {
return new Promise((resolve) => {
if (GOTIFY_URL && GOTIFY_TOKEN) {
@@ -713,7 +633,7 @@ function tgBotNotify(text, desp) {
return new Promise((resolve) => {
if (TG_BOT_TOKEN && TG_USER_ID) {
const options = {
url: `https://${TG_API_HOST}/bot${TG_BOT_TOKEN}/sendMessage`,
url: `${TG_API_HOST}/bot${TG_BOT_TOKEN}/sendMessage`,
json: {
chat_id: `${TG_USER_ID}`,
text: `${text}\n\n${desp}`,
@@ -725,15 +645,19 @@ function tgBotNotify(text, desp) {
timeout,
};
if (TG_PROXY_HOST && TG_PROXY_PORT) {
const tunnel = require('tunnel');
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent');
const options = {
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
proxy: `http://${TG_PROXY_AUTH}${TG_PROXY_HOST}:${TG_PROXY_PORT}`,
};
const httpAgent = new HttpProxyAgent(options);
const httpsAgent = new HttpsProxyAgent(options);
const agent = {
https: tunnel.httpsOverHttp({
proxy: {
host: TG_PROXY_HOST,
port: TG_PROXY_PORT * 1,
proxyAuth: TG_PROXY_AUTH,
},
}),
http: httpAgent,
https: httpsAgent,
};
Object.assign(options, { agent });
}
@@ -1073,7 +997,8 @@ function pushPlusNotify(text, desp) {
try {
if (err) {
console.log(
`push+发送${PUSH_PLUS_USER ? '一对多' : '一对一'
`push+发送${
PUSH_PLUS_USER ? '一对多' : '一对一'
}通知消息失败!!\n`,
);
console.log(err);
@@ -1081,12 +1006,14 @@ function pushPlusNotify(text, desp) {
data = JSON.parse(data);
if (data.code === 200) {
console.log(
`push+发送${PUSH_PLUS_USER ? '一对多' : '一对一'
`push+发送${
PUSH_PLUS_USER ? '一对多' : '一对一'
}通知消息完成。\n`,
);
} else {
console.log(
`push+发送${PUSH_PLUS_USER ? '一对多' : '一对一'
`push+发送${
PUSH_PLUS_USER ? '一对多' : '一对一'
}通知消息失败:${data.msg}\n`,
);
}
@@ -1202,31 +1129,31 @@ function fsBotNotify(text, desp) {
}
async function smtpNotify(text, desp) {
if (![SMTP_SERVER, SMTP_EMAIL, SMTP_PASSWORD].every(Boolean)) {
if (![SMTP_EMAIL, SMTP_PASSWORD].every(Boolean) || !SMTP_SERVICE) {
return;
}
try {
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport(
`${SMTP_SSL === 'true' ? 'smtps:' : 'smtp:'}//${SMTP_SERVER}`,
{
const transporter = nodemailer.createTransport({
service: SMTP_SERVICE,
auth: {
user: SMTP_EMAIL,
pass: SMTP_PASSWORD,
},
},
);
});
const addr = SMTP_NAME ? `"${SMTP_NAME}" <${SMTP_EMAIL}>` : SMTP_EMAIL;
const info = await transporter.sendMail({
from: addr,
to: addr,
subject: text,
text: desp,
html: `${desp.replace(/\n/g, '<br/>')}`,
});
if (!!info.messageId) {
transporter.close();
if (info.messageId) {
console.log('SMTP发送通知消息成功🎉\n');
return true;
}
@@ -1237,17 +1164,7 @@ async function smtpNotify(text, desp) {
}
}
function smtpNotify(text, desp) {
return new Promise((resolve) => {
if (SMTP_SERVER && SMTP_SSL && SMTP_EMAIL && SMTP_PASSWORD && SMTP_NAME) {
// todo: Node.js并没有内置的 smtp 实现,需要调用外部库,因为不清楚这个文件的模块依赖情况,所以留给有缘人实现
} else {
resolve();
}
});
}
function PushMeNotify(text, desp, params = {}) {
function pushMeNotify(text, desp, params = {}) {
return new Promise((resolve) => {
if (PUSHME_KEY) {
const options = {
@@ -1282,17 +1199,13 @@ function PushMeNotify(text, desp, params = {}) {
});
}
function ChronocatNotify(title, desp) {
function chronocatNotify(title, desp) {
return new Promise((resolve) => {
if (!CHRONOCAT_TOKEN || !CHRONOCAT_QQ || !CHRONOCAT_URL) {
console.log(
'CHRONOCAT 服务的 CHRONOCAT_URL 或 CHRONOCAT_QQ 未设置!!\n取消推送',
);
resolve();
return;
}
console.log('CHRONOCAT 服务启动');
const user_ids = CHRONOCAT_QQ.match(/user_id=(\d+)/g)?.map(
(match) => match.split('=')[1],
);
@@ -1386,11 +1299,9 @@ function webhookNotify(text, desp) {
got(formatUrl, options).then((resp) => {
try {
if (resp.statusCode !== 200) {
console.log('自定义发送通知消息失败!!\n');
console.log(resp.body);
console.log(`自定义发送通知消息失败!!\n${resp.body}`);
} else {
console.log('自定义发送通知消息成功🎉。\n');
console.log(resp.body);
console.log(`自定义发送通知消息成功🎉。\n${resp.body}`);
}
} catch (e) {
$.logErr(e, resp);
@@ -1429,7 +1340,9 @@ function parseHeaders(headers) {
}
function parseBody(body, contentType) {
if (!body) return '';
if (contentType === 'text/plain' || !body) {
return body;
}
const parsed = {};
let key;
@@ -1439,7 +1352,7 @@ function parseBody(body, contentType) {
body &&
body.split('\n').forEach(function parser(line) {
i = line.indexOf(':');
key = line.substring(0, i).trim().toLowerCase();
key = line.substring(0, i).trim();
val = line.substring(i + 1).trim();
if (!key || parsed[key]) {