适配自定义webhook推送

This commit is contained in:
kyle208 2023-05-29 08:32:46 +08:00
parent 86e3d8736b
commit aa31501cac
3 changed files with 77 additions and 4 deletions

View File

@ -164,4 +164,7 @@ export SMTP_PASSWORD=""
## smtp_name 填写 SMTP 收发件人姓名,可随意填写
export SMTP_NAME=""
## 15. webhook
export WEBHOOK_URL=""
## 其他需要的变量,脚本中需要的变量使用 export 变量名= 声明即可

View File

@ -137,6 +137,9 @@ let SMTP_EMAIL = '';
let SMTP_PASSWORD = '';
let SMTP_NAME = '';
// =======================================webhook设置区域=======================================
let WEBHOOK_URL = '';
//==========================云端环境变量的判断与接收=========================
if (process.env.GOTIFY_URL) {
GOTIFY_URL = process.env.GOTIFY_URL;
@ -278,6 +281,10 @@ if (process.env.SMTP_PASSWORD) {
if (process.env.SMTP_NAME) {
SMTP_NAME = process.env.SMTP_NAME;
}
if (process.env.WEBHOOK_URL) {
WEBHOOK_URL = process.env.WEBHOOK_URL;
}
//==========================云端环境变量的判断与接收=========================
/**
@ -288,6 +295,7 @@ if (process.env.SMTP_NAME) {
* @param author 作者仓库等信息 `本通知 Byhttps://github.com/whyour/qinglong`
* @returns {Promise<unknown>}
*/
async function sendNotify(
text,
desp,
@ -326,7 +334,9 @@ async function sendNotify(
aibotkNotify(text, desp), //智能微秘书
fsBotNotify(text, desp), //飞书机器人
smtpNotify(text, desp), //SMTP 邮件
webhookNotify(text, desp), //webhook
]);
}
function gotifyNotify(text, desp) {
@ -1107,6 +1117,46 @@ function smtpNotify(text, desp) {
});
}
function webhookNotify(text, desp) {
console.log(text);
return new Promise((resolve) => {
console.log(WEBHOOK_URL);
if (WEBHOOK_URL) {
const options = {
url: `${WEBHOOK_URL}`,
body: `title=${encodeURIComponent(text)}&message=${encodeURIComponent(
desp,
)}&priority=${''}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
};
$.post(options, (err, resp, data) => {
console.log(options);
try {
if (err) {
console.log('webhook发送通知调用API失败\n');
console.log(err);
} else {
data = JSON.parse(data);
if (data.id) {
console.log('webhook发送通知消息成功🎉\n');
} else {
console.log(`${data.message}\n`);
}
}
} catch (e) {
$.logErr(e, resp);
} finally {
resolve();
}
});
} else {
resolve();
}
});
}
module.exports = {
sendNotify,
BARK_PUSH,

View File

@ -96,6 +96,8 @@ push_config = {
'SMTP_EMAIL': '', # SMTP 收发件邮箱,通知将会由自己发给自己
'SMTP_PASSWORD': '', # SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定
'SMTP_NAME': '', # SMTP 收发件人姓名,可随意填写
'WEBHOOK_URL': '', # webhook自定义通知 接收回调的URL
}
notify_function = []
# fmt: on
@ -604,6 +606,23 @@ def smtp(title: str, content: str) -> None:
except Exception as e:
print(f'SMTP 邮件 推送失败!{e}')
def webhook_notify(title: str, content: str) -> None:
"""
通过 WEBHOOK 推送消息
"""
if not push_config.get("WEBHOOK_URL"):
print("WEBHOOK 服务的 WEBHOOK_URL 未设置!!\n取消推送")
return
print("WEBHOOK服务启动")
url = f"{push_config.get('WEBHOOK_URL')}"
headers = {"Content-Type": "application/json;charset=utf-8"}
data = {"title": f"{title}", "content": f"{content}"}
response = requests.post(
url=url, data=json.dumps(data), headers=headers, timeout=15
).json()
print("WEBHOOK 推送成功!")
def one() -> str:
"""
@ -649,7 +668,8 @@ if push_config.get("AIBOTK_KEY") and push_config.get("AIBOTK_TYPE") and push_con
notify_function.append(aibotk)
if push_config.get("SMTP_SERVER") and push_config.get("SMTP_SSL") and push_config.get("SMTP_EMAIL") and push_config.get("SMTP_PASSWORD") and push_config.get("SMTP_NAME"):
notify_function.append(smtp)
if push_config.get("WEBHOOK_URL"):
notify_function.append(webhook_notify)
def send(title: str, content: str) -> None:
if not content: