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

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 { 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", NOTIFY_TYPE = "pushdeer",
-- --
-- telegram 通知配置, https://github.com/0wQ/telegram-notify -- telegram 通知配置, https://github.com/0wQ/telegram-notify

View File

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