增加 ntfy 通知 (#2537)

Co-authored-by: qiaoyun680 <qiaoyun680>
This commit is contained in:
qiaoyun680 2024-10-22 21:47:38 +08:00 committed by GitHub
parent 3822c37fa0
commit 185fd2ff91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 1142 additions and 4 deletions

View File

@ -20,6 +20,7 @@ export enum NotificationMode {
'feishu' = 'feishu',
'webhook' = 'webhook',
'chronocat' = 'Chronocat',
'ntfy' = 'ntfy',
}
abstract class NotificationBaseInfo {
@ -139,6 +140,11 @@ export class LarkNotification extends NotificationBaseInfo {
public larkKey = '';
}
export class NtfyNotification extends NotificationBaseInfo {
public ntfyUrl = '';
public ntfyTopic = '';
public ntfyPriority = '';
}
export interface NotificationInfo
extends GoCqHttpBotNotification,
GotifyNotification,
@ -158,5 +164,6 @@ export interface NotificationInfo
PushMeNotification,
WebhookNotification,
ChronocatNotification,
LarkNotification {}
LarkNotification,
NtfyNotification {}

View File

@ -35,6 +35,7 @@ export default class NotificationService {
['webhook', this.webhook],
['lark', this.lark],
['chronocat', this.chronocat],
['ntfy', this.ntfy],
]);
private title = '';
@ -516,7 +517,7 @@ export default class NotificationService {
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
}
}
private async pushPlus() {
const { pushPlusToken, pushPlusUser } = this.params;
@ -661,6 +662,26 @@ export default class NotificationService {
}
}
private async ntfy() {
const { ntfyUrl, ntfyTopic, ntfyPriority } = this.params;
try {
const res: any = await got
.post(`${ntfyUrl || 'https://ntfy.sh'}/${ntfyTopic}`, {
...this.gotOption,
body: `${this.title}\n${this.content}`,
headers: { 'Title': 'qinglong', 'Priority': `${ntfyPriority || '3'}` },
});
if (res.statusCode === 200) {
return true;
} else {
throw new Error(JSON.stringify(res));
}
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
}
private async chronocat() {
const { chronocatURL, chronocatQQ, chronocatToken } = this.params;
try {

View File

@ -211,7 +211,17 @@ export FSKEY=""
export QMSG_KEY=""
export QMSG_TYPE=""
## 20. 自定义通知
## 20.Ntfy
## 官方文档: https://docs.ntfy.sh
## ntfy_url 填写ntfy地址,如https://ntfy.sh
## ntfy_topic 填写ntfy的消息应用topic
## ntfy_priority 填写推送消息优先级,默认为3
export NTFY_URL=""
export NTFY_TOPIC=""
export NTFY_PRIORITY="3"
## 21. 自定义通知
## 自定义通知 接收回调的URL
export WEBHOOK_URL=""
## WEBHOOK_BODY 和 WEBHOOK_HEADERS 多个参数时,直接换行或者使用 $'\n' 连接多行字符串,比如 export dd="line 1"$'\n'"line 2"

View File

@ -95,6 +95,10 @@ const push_config = {
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
};
for (const key in push_config) {
@ -1188,6 +1192,42 @@ function webhookNotify(text, desp) {
});
}
function ntfyNotify(text, desp) {
return new Promise((resolve) => {
const { NTFY_URL, NTFY_TOPIC, NTFY_PRIORITY } = push_config;
if (NTFY_TOPIC) {
const options = {
url: NTFY_URL || `https://ntfy.sh`,
body: `${desp}\n${text}`,
headers: {
'Title': 'qinglong',
'Priority': NTFY_PRIORITY || '3'
},
timeout,
};
$.post(options, (err, resp, data) => {
try {
if (err) {
console.log('Ntfy 通知调用API失败😞\n', err);
} else {
if (data.success) {
console.log('Ntfy 发送通知消息成功🎉\n');
} else {
console.log(`Ntfy 发送通知消息异常 ${JSON.stringify(data)}`);
}
}
} catch (e) {
$.logErr(e, resp);
} finally {
resolve(data);
}
});
} else {
resolve();
}
});
}
function parseString(input, valueFormatFn) {
const regex = /(\w+):\s*((?:(?!\n\w+:).)*)/g;
const matches = {};
@ -1316,6 +1356,7 @@ async function sendNotify(text, desp, params = {}) {
chronocatNotify(text, desp), // Chronocat
webhookNotify(text, desp), // 自定义通知
qmsgNotify(text, desp), // 自定义通知
ntfyNotify(text, desp), // Ntfy
]);
}

View File

@ -117,6 +117,10 @@ push_config = {
'WEBHOOK_HEADERS': '', # 自定义通知 请求头
'WEBHOOK_METHOD': '', # 自定义通知 请求方法
'WEBHOOK_CONTENT_TYPE': '' # 自定义通知 content-type
'NTFY_URL': '', # ntfy地址,如https://ntfy.sh
'NTFY_TOPIC': '', # ntfy的消息应用topic
'NTFY_PRIORITY':'3', # 推送消息优先级,默认为3
}
# fmt: on
@ -777,6 +781,32 @@ def chronocat(title: str, content: str) -> None:
print(f"QQ群消息:{ids}推送失败!")
def ntfy(title: str, content: str) -> None:
"""
通过 Ntfy 推送消息
"""
if not push_config.get("NTFY_TOPIC"):
print("ntfy 服务的 NTFY_TOPIC 未设置!!\n取消推送")
return
print("ntfy 服务启动")
priority = '3'
if not push_config.get("NTFY_PRIORITY"):
print("ntfy 服务的NTFY_PRIORITY 未设置!!默认设置为3")
else:
priority = push_config.get("NTFY_PRIORITY")
data = (title + "\n" +content).encode(encoding='utf-8')
headers = {
"Title": "qinglong",
"Priority": priority
}
url = push_config.get("NTFY_URL") + "/" + push_config.get("NTFY_TOPIC")
response = requests.post(url, data=data, headers=headers)
if response["code"] == 200:
print("Ntfy 推送成功!")
else:
print("Ntfy 推送失败!错误信息:", response)
def parse_headers(headers):
if not headers:
return {}
@ -937,7 +967,8 @@ def add_notify_function():
notify_function.append(chronocat)
if push_config.get("WEBHOOK_URL") and push_config.get("WEBHOOK_METHOD"):
notify_function.append(custom_notify)
if push_config.get("NTFY_TOPIC"):
notify_function.append(ntfy)
if not notify_function:
print(f"无推送渠道,请检查通知变量是否正确")
return notify_function

1010
sample/notify.py.save Normal file

File diff suppressed because it is too large Load Diff

View File

@ -388,6 +388,8 @@
"SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定": "The SMTP login password may also be a special passphrase, depending on the specific email service provider's instructions",
"PushMe的Keyhttps://push.i-i.me/": "PushMe key, https://push.i-i.me/",
"自建的PushMeServer消息接口地址例如http://127.0.0.1:3010不填则使用官方消息接口": "The self built PushMeServer message interface address, for example: http://127.0.0.1:3010 If left blank, use the official message interface",
"ntfy的url地址例如 https://ntfy.sh'": "The URL address of ntfy, for example, https://ntfy.sh.",
"ntfy的消息应用topic": "The topic for ntfy's messaging application.",
"请求方法": "Request Method",
"请求头Content-Type": "Request Header Content-Type",
"请求链接以http或者https开头。url或者body中必须包含$title$content可选对应api内容的位置": "Request URL should start with http or https. URL or body must contain $title, $content is optional and corresponds to the API content position.",

View File

@ -388,6 +388,8 @@
"SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定": "SMTP 登录密码,也可能为特殊口令,视具体邮件服务商说明而定",
"PushMe的Keyhttps://push.i-i.me/": "PushMe的Keyhttps://push.i-i.me/",
"自建的PushMeServer消息接口地址例如http://127.0.0.1:3010不填则使用官方消息接口": "自建的PushMeServer消息接口地址例如http://127.0.0.1:3010不填则使用官方消息接口",
"ntfy的url地址例如 https://ntfy.sh": "ntfy的url地址例如 https://ntfy.sh",
"ntfy的消息应用topic": "ntfy的消息应用topic",
"请求方法": "请求方法",
"请求头Content-Type": "请求头Content-Type",
"请求链接以http或者https开头。url或者body中必须包含$title$content可选对应api内容的位置": "请求链接以http或者https开头。url或者body中必须包含$title$content可选对应api内容的位置",

View File

@ -84,6 +84,7 @@ export default {
},
notificationModes: [
{ value: 'gotify', label: 'Gotify' },
{ value: 'ntfy', label: 'Ntfy' },
{ value: 'goCqHttpBot', label: 'GoCqHttpBot' },
{ value: 'serverChan', label: intl.get('Server酱') },
{ value: 'pushDeer', label: 'PushDeer' },
@ -118,6 +119,19 @@ export default {
},
{ label: 'gotifyPriority', tip: intl.get('推送消息的优先级') },
],
ntfy: [
{
label: 'ntfyUrl',
tip: intl.get('ntfy的url地址例如 https://ntfy.sh'),
required: true,
},
{
label: 'ntfyTopic',
tip: intl.get('ntfy的消息应用topic'),
required: true,
},
{ label: 'ntfyPriority', tip: intl.get('推送消息的优先级') },
],
chat: [
{
label: 'chatUrl',