支持同时使用多个通知渠道

This commit is contained in:
Mizore 2023-02-16 18:43:34 +08:00
parent 011e1928e8
commit 496fafaf02
2 changed files with 226 additions and 243 deletions

View File

@ -1,5 +1,6 @@
return {
-- 通知类型 telegram, pushdeer, bark, dingtalk, feishu, wecom, pushover, next-smtp-proxy
-- 通知类型, 支持配置多个
-- NOTIFY_TYPE = {"telegram", "pushdeer", "bark", "dingtalk", "feishu", "wecom", "pushover", "next-smtp-proxy"},
NOTIFY_TYPE = "pushdeer",
--
-- telegram 通知配置, https://github.com/0wQ/telegram-notify

View File

@ -13,10 +13,11 @@ local function urlencodeTab(params)
return table.concat(msg)
end
local notify = {
-- 发送到 telegram
local function notifyToTelegram(msg)
["telegram"] = function(msg)
if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then
log.error("util_notify.notifyToTelegram", "未配置 `config.TELEGRAM_PROXY_API`")
log.error("util_notify", "未配置 `config.TELEGRAM_PROXY_API`")
return
end
@ -27,18 +28,17 @@ local function notifyToTelegram(msg)
["x-token"] = config.TELEGRAM_TOKEN or ""
}
log.info("util_notify.notifyToTelegram", "POST", config.TELEGRAM_PROXY_API)
log.info("util_notify", "POST", config.TELEGRAM_PROXY_API)
return util_http.fetch(nil, "POST", config.TELEGRAM_PROXY_API, header, msg)
end
end,
-- 发送到 pushdeer
local function notifyToPushDeer(msg)
["pushdeer"] = function(msg)
if config.PUSHDEER_API == nil or config.PUSHDEER_API == "" then
log.error("util_notify.notifyToPushDeer", "未配置 `config.PUSHDEER_API`")
log.error("util_notify", "未配置 `config.PUSHDEER_API`")
return
end
if config.PUSHDEER_KEY == nil or config.PUSHDEER_KEY == "" then
log.error("util_notify.notifyToPushDeer", "未配置 `config.PUSHDEER_KEY`")
log.error("util_notify", "未配置 `config.PUSHDEER_KEY`")
return
end
@ -51,18 +51,17 @@ local function notifyToPushDeer(msg)
text = msg
}
log.info("util_notify.notifyToPushDeer", "POST", config.PUSHDEER_API)
log.info("util_notify", "POST", config.PUSHDEER_API)
return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body))
end
end,
-- 发送到 bark
local function notifyToBark(msg)
["bark"] = function(msg)
if config.BARK_API == nil or config.BARK_API == "" then
log.error("util_notify.notifyToBark", "未配置 `config.BARK_API`")
log.error("util_notify", "未配置 `config.BARK_API`")
return
end
if config.BARK_KEY == nil or config.BARK_KEY == "" then
log.error("util_notify.notifyToBark", "未配置 `config.BARK_KEY`")
log.error("util_notify", "未配置 `config.BARK_KEY`")
return
end
@ -74,14 +73,13 @@ local function notifyToBark(msg)
}
local url = config.BARK_API .. "/" .. config.BARK_KEY
log.info("util_notify.notifyToBark", "POST", url)
log.info("util_notify", "POST", url)
return util_http.fetch(nil, "POST", url, header, urlencodeTab(body))
end
end,
-- 发送到 dingtalk
local function notifyToDingTalk(msg)
["dingtalk"] = function(msg)
if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then
log.error("util_notify.notifyToDingTalk", "未配置 `config.DINGTALK_WEBHOOK`")
log.error("util_notify", "未配置 `config.DINGTALK_WEBHOOK`")
return
end
@ -98,14 +96,13 @@ local function notifyToDingTalk(msg)
-- LuatOS Bug, json.encode 会将 \n 转换为 \b
json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify.notifyToDingTalk", "POST", config.DINGTALK_WEBHOOK)
log.info("util_notify", "POST", config.DINGTALK_WEBHOOK)
return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data)
end
end,
-- 发送到 feishu
local function notifyToFeishu(msg)
["feishu"] = function(msg)
if config.FEISHU_WEBHOOK == nil or config.FEISHU_WEBHOOK == "" then
log.error("util_notify.notifyToFeishu", "未配置 `config.FEISHU_WEBHOOK`")
log.error("util_notify", "未配置 `config.FEISHU_WEBHOOK`")
return
end
@ -122,14 +119,13 @@ local function notifyToFeishu(msg)
-- LuatOS Bug, json.encode 会将 \n 转换为 \b
json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify.notifyToFeishu", "POST", config.FEISHU_WEBHOOK)
log.info("util_notify", "POST", config.FEISHU_WEBHOOK)
return util_http.fetch(nil, "POST", config.FEISHU_WEBHOOK, header, json_data)
end
end,
-- 发送到 wecom
local function notifyToWeCom(msg)
["wecom"] = function(msg)
if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then
log.error("util_notify.notifyToWeCom", "未配置 `config.WECOM_WEBHOOK`")
log.error("util_notify", "未配置 `config.WECOM_WEBHOOK`")
return
end
@ -146,19 +142,17 @@ local function notifyToWeCom(msg)
-- LuatOS Bug, json.encode 会将 \n 转换为 \b
json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify.notifyToWeCom", "POST", config.WECOM_WEBHOOK)
log.info("util_notify", "POST", config.WECOM_WEBHOOK)
return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data)
end
end,
-- 发送到 pushover
local function notifyToPushover(msg)
["pushover"] = function(msg)
if config.PUSHOVER_API_TOKEN == nil or config.PUSHOVER_API_TOKEN == "" then
log.error("util_notify.notifyToPushover", "未配置 `config.PUSHOVER_API_TOKEN`")
log.error("util_notify", "未配置 `config.PUSHOVER_API_TOKEN`")
return
end
if config.PUSHOVER_USER_KEY == nil or config.PUSHOVER_USER_KEY == "" then
log.error("util_notify.notifyToPushover", "未配置 `config.PUSHOVER_USER_KEY`")
log.error("util_notify", "未配置 `config.PUSHOVER_USER_KEY`")
return
end
@ -177,35 +171,33 @@ local function notifyToPushover(msg)
local url = "https://api.pushover.net/1/messages.json"
log.info("util_notify.notifyToPushover", "POST", config.PUSHOVER_API_TOKEN)
log.info("util_notify", "POST", url)
return util_http.fetch(nil, "POST", url, header, json_data)
end
end,
-- 发送到 next-smtp-proxy
local function notifyToNextSmtpProxy(msg)
["next-smtp-proxy"] = function(msg)
if config.NEXT_SMTP_PROXY_API == nil or config.NEXT_SMTP_PROXY_API == "" then
log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_API`")
log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_API`")
return
end
if config.NEXT_SMTP_PROXY_USER == nil or config.NEXT_SMTP_PROXY_USER == "" then
log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_USER`")
log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_USER`")
return
end
if config.NEXT_SMTP_PROXY_PASSWORD == nil or config.NEXT_SMTP_PROXY_PASSWORD == "" then
log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_PASSWORD`")
log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_PASSWORD`")
return
end
if config.NEXT_SMTP_PROXY_HOST == nil or config.NEXT_SMTP_PROXY_HOST == "" then
log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_HOST`")
log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_HOST`")
return
end
if config.NEXT_SMTP_PROXY_PORT == nil or config.NEXT_SMTP_PROXY_PORT == "" then
log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_PORT`")
log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_PORT`")
return
end
if config.NEXT_SMTP_PROXY_TO_EMAIL == nil or config.NEXT_SMTP_PROXY_TO_EMAIL == "" then
log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_TO_EMAIL`")
log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_TO_EMAIL`")
return
end
@ -223,9 +215,10 @@ local function notifyToNextSmtpProxy(msg)
text = msg
}
log.info("util_notify.notifyToNextSmtpProxy", "POST", config.NEXT_SMTP_PROXY_API)
log.info("util_notify", "POST", config.NEXT_SMTP_PROXY_API)
return util_http.fetch(nil, "POST", config.NEXT_SMTP_PROXY_API, header, urlencodeTab(body))
end
}
local function append()
local msg = "\n"
@ -277,13 +270,12 @@ end
--- 发送通知
-- @param msg 消息内容
-- @param channel 通知渠道
-- @return true: 无需重发, false: 需要重发
function util_notify.send(msg)
log.info("util_notify.send", "发送通知", config.NOTIFY_TYPE)
function util_notify.send(msg, channel)
log.info("util_notify.send", "发送通知", channel)
if type(msg) == "table" then
msg = table.concat(msg, "\n")
end
-- 判断消息内容 msg
if type(msg) ~= "string" then
log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
return true
@ -293,34 +285,19 @@ function util_notify.send(msg)
return true
end
-- 判断通知渠道 channel
if channel and notify[channel] == nil then
log.error("util_notify.send", "发送通知失败", "未知通知渠道", channel)
return true
end
-- 通知内容追加更多信息
if config.NOTIFY_APPEND_MORE_INFO then
msg = msg .. append()
end
-- 判断通知类型
local notify
if config.NOTIFY_TYPE == "telegram" then
notify = notifyToTelegram
elseif config.NOTIFY_TYPE == "pushdeer" then
notify = notifyToPushDeer
elseif config.NOTIFY_TYPE == "bark" then
notify = notifyToBark
elseif config.NOTIFY_TYPE == "dingtalk" then
notify = notifyToDingTalk
elseif config.NOTIFY_TYPE == "feishu" then
notify = notifyToFeishu
elseif config.NOTIFY_TYPE == "wecom" then
notify = notifyToWeCom
elseif config.NOTIFY_TYPE == "pushover" then
notify = notifyToPushover
elseif config.NOTIFY_TYPE == "next-smtp-proxy" then
notify = notifyToNextSmtpProxy
else
log.error("util_notify.send", "发送通知失败", "未配置 `config.NOTIFY_TYPE`")
return true
end
local code, headers, body = notify(msg)
-- 发送通知
local code, headers, body = notify[channel](msg)
if code == nil then
log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
return true
@ -330,19 +307,11 @@ function util_notify.send(msg)
log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
return true
end
if code >= 200 and code < 300 then
if code >= 200 and code < 500 then
-- http 2xx 成功
log.info("util_notify.send", "发送通知成功", "code:", code, "body:", body)
return true
end
if code >= 300 and code < 400 then
-- http 3xx 重定向, 重发也不会成功
log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
return true
end
if code >= 400 and code < 500 then
-- http 4xx 客户端错误, 重发也不会成功
log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
log.info("util_notify.send", "发送通知成功", "code:", code, "body:", body)
return true
end
log.error("util_notify.send", "发送通知失败, 等待重发", "code:", code, "body:", body)
@ -351,8 +320,21 @@ end
--- 添加到消息队列
-- @param msg 消息内容
function util_notify.add(msg)
table.insert(msg_queue, {msg = msg, retry = 0})
-- @param channels 通知渠道
function util_notify.add(msg, channels)
if type(msg) == "table" then
msg = table.concat(msg, "\n")
end
channels = channels or config.NOTIFY_TYPE
if type(channels) ~= "table" then
channels = {channels}
end
for _, channel in ipairs(channels) do
table.insert(msg_queue, {channel = channel, msg = msg, retry = 0})
end
sys.publish("NEW_MSG")
log.debug("util_notify.add", "添加到消息队列, 当前队列长度:", #msg_queue)
end
@ -373,7 +355,7 @@ local function poll()
if item.retry > (config.NOTIFY_RETRY_MAX or 100) then
log.error("util_notify.poll", "超过最大重发次数", "msg:", item.msg)
else
result = util_notify.send(item.msg)
result = util_notify.send(item.msg, item.channel)
item.retry = item.retry + 1
if not result then