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

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,219 +13,212 @@ local function urlencodeTab(params)
return table.concat(msg) return table.concat(msg)
end end
-- 发送到 telegram local notify = {
local function notifyToTelegram(msg) -- 发送到 telegram
if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then ["telegram"] = function(msg)
log.error("util_notify.notifyToTelegram", "未配置 `config.TELEGRAM_PROXY_API`") if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then
return log.error("util_notify", "未配置 `config.TELEGRAM_PROXY_API`")
end return
end
local header = { local header = {
["content-type"] = "text/plain", ["content-type"] = "text/plain",
["x-disable-web-page-preview"] = "1", ["x-disable-web-page-preview"] = "1",
["x-chat-id"] = config.TELEGRAM_CHAT_ID or "", ["x-chat-id"] = config.TELEGRAM_CHAT_ID or "",
["x-token"] = config.TELEGRAM_TOKEN or "" ["x-token"] = config.TELEGRAM_TOKEN or ""
}
log.info("util_notify.notifyToTelegram", "POST", config.TELEGRAM_PROXY_API)
return util_http.fetch(nil, "POST", config.TELEGRAM_PROXY_API, header, msg)
end
-- 发送到 pushdeer
local function notifyToPushDeer(msg)
if config.PUSHDEER_API == nil or config.PUSHDEER_API == "" then
log.error("util_notify.notifyToPushDeer", "未配置 `config.PUSHDEER_API`")
return
end
if config.PUSHDEER_KEY == nil or config.PUSHDEER_KEY == "" then
log.error("util_notify.notifyToPushDeer", "未配置 `config.PUSHDEER_KEY`")
return
end
local header = {
["Content-Type"] = "application/x-www-form-urlencoded"
}
local body = {
pushkey = config.PUSHDEER_KEY or "",
type = "text",
text = msg
}
log.info("util_notify.notifyToPushDeer", "POST", config.PUSHDEER_API)
return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body))
end
-- 发送到 bark
local function notifyToBark(msg)
if config.BARK_API == nil or config.BARK_API == "" then
log.error("util_notify.notifyToBark", "未配置 `config.BARK_API`")
return
end
if config.BARK_KEY == nil or config.BARK_KEY == "" then
log.error("util_notify.notifyToBark", "未配置 `config.BARK_KEY`")
return
end
local header = {
["Content-Type"] = "application/x-www-form-urlencoded"
}
local body = {
body = msg
}
local url = config.BARK_API .. "/" .. config.BARK_KEY
log.info("util_notify.notifyToBark", "POST", url)
return util_http.fetch(nil, "POST", url, header, urlencodeTab(body))
end
-- 发送到 dingtalk
local function notifyToDingTalk(msg)
if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then
log.error("util_notify.notifyToDingTalk", "未配置 `config.DINGTALK_WEBHOOK`")
return
end
local header = {
["Content-Type"] = "application/json; charset=utf-8"
}
local body = {
msgtype = "text",
text = {
content = msg
} }
}
local json_data = json.encode(body)
-- 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.TELEGRAM_PROXY_API)
return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data) return util_http.fetch(nil, "POST", config.TELEGRAM_PROXY_API, header, msg)
end end,
-- 发送到 pushdeer
["pushdeer"] = function(msg)
if config.PUSHDEER_API == nil or config.PUSHDEER_API == "" then
log.error("util_notify", "未配置 `config.PUSHDEER_API`")
return
end
if config.PUSHDEER_KEY == nil or config.PUSHDEER_KEY == "" then
log.error("util_notify", "未配置 `config.PUSHDEER_KEY`")
return
end
-- 发送到 feishu local header = {
local function notifyToFeishu(msg) ["Content-Type"] = "application/x-www-form-urlencoded"
if config.FEISHU_WEBHOOK == nil or config.FEISHU_WEBHOOK == "" then }
log.error("util_notify.notifyToFeishu", "未配置 `config.FEISHU_WEBHOOK`") local body = {
return pushkey = config.PUSHDEER_KEY or "",
end type = "text",
local header = {
["Content-Type"] = "application/json; charset=utf-8"
}
local body = {
msg_type = "text",
content = {
text = msg text = msg
} }
}
local json_data = json.encode(body)
-- 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.PUSHDEER_API)
return util_http.fetch(nil, "POST", config.FEISHU_WEBHOOK, header, json_data) return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body))
end end,
-- 发送到 bark
["bark"] = function(msg)
if config.BARK_API == nil or config.BARK_API == "" then
log.error("util_notify", "未配置 `config.BARK_API`")
return
end
if config.BARK_KEY == nil or config.BARK_KEY == "" then
log.error("util_notify", "未配置 `config.BARK_KEY`")
return
end
-- 发送到 wecom local header = {
local function notifyToWeCom(msg) ["Content-Type"] = "application/x-www-form-urlencoded"
if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then
log.error("util_notify.notifyToWeCom", "未配置 `config.WECOM_WEBHOOK`")
return
end
local header = {
["Content-Type"] = "application/json; charset=utf-8"
}
local body = {
msgtype = "text",
text = {
content = msg
} }
} local body = {
local json_data = json.encode(body) body = msg
-- LuatOS Bug, json.encode 会将 \n 转换为 \b }
json_data = string.gsub(json_data, "\\b", "\\n") local url = config.BARK_API .. "/" .. config.BARK_KEY
log.info("util_notify.notifyToWeCom", "POST", config.WECOM_WEBHOOK) log.info("util_notify", "POST", url)
return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data) return util_http.fetch(nil, "POST", url, header, urlencodeTab(body))
end end,
-- 发送到 dingtalk
["dingtalk"] = function(msg)
if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then
log.error("util_notify", "未配置 `config.DINGTALK_WEBHOOK`")
return
end
local header = {
["Content-Type"] = "application/json; charset=utf-8"
}
local body = {
msgtype = "text",
text = {
content = msg
}
}
local json_data = json.encode(body)
-- LuatOS Bug, json.encode 会将 \n 转换为 \b
json_data = string.gsub(json_data, "\\b", "\\n")
-- 发送到 pushover log.info("util_notify", "POST", config.DINGTALK_WEBHOOK)
local function notifyToPushover(msg) return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data)
if config.PUSHOVER_API_TOKEN == nil or config.PUSHOVER_API_TOKEN == "" then end,
log.error("util_notify.notifyToPushover", "未配置 `config.PUSHOVER_API_TOKEN`") -- 发送到 feishu
return ["feishu"] = function(msg)
if config.FEISHU_WEBHOOK == nil or config.FEISHU_WEBHOOK == "" then
log.error("util_notify", "未配置 `config.FEISHU_WEBHOOK`")
return
end
local header = {
["Content-Type"] = "application/json; charset=utf-8"
}
local body = {
msg_type = "text",
content = {
text = msg
}
}
local json_data = json.encode(body)
-- LuatOS Bug, json.encode 会将 \n 转换为 \b
json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify", "POST", config.FEISHU_WEBHOOK)
return util_http.fetch(nil, "POST", config.FEISHU_WEBHOOK, header, json_data)
end,
-- 发送到 wecom
["wecom"] = function(msg)
if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then
log.error("util_notify", "未配置 `config.WECOM_WEBHOOK`")
return
end
local header = {
["Content-Type"] = "application/json; charset=utf-8"
}
local body = {
msgtype = "text",
text = {
content = msg
}
}
local json_data = json.encode(body)
-- LuatOS Bug, json.encode 会将 \n 转换为 \b
json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify", "POST", config.WECOM_WEBHOOK)
return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data)
end,
-- 发送到 pushover
["pushover"] = function(msg)
if config.PUSHOVER_API_TOKEN == nil or config.PUSHOVER_API_TOKEN == "" then
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", "未配置 `config.PUSHOVER_USER_KEY`")
return
end
local header = {
["Content-Type"] = "application/json; charset=utf-8"
}
local body = {
token = config.PUSHOVER_API_TOKEN,
user = config.PUSHOVER_USER_KEY,
message = msg
}
local json_data = json.encode(body)
-- LuatOS Bug, json.encode 会将 \n 转换为 \b
json_data = string.gsub(json_data, "\\b", "\\n")
local url = "https://api.pushover.net/1/messages.json"
log.info("util_notify", "POST", url)
return util_http.fetch(nil, "POST", url, header, json_data)
end,
-- 发送到 next-smtp-proxy
["next-smtp-proxy"] = function(msg)
if config.NEXT_SMTP_PROXY_API == nil or config.NEXT_SMTP_PROXY_API == "" then
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", "未配置 `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", "未配置 `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", "未配置 `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", "未配置 `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", "未配置 `config.NEXT_SMTP_PROXY_TO_EMAIL`")
return
end
local header = {
["Content-Type"] = "application/x-www-form-urlencoded"
}
local body = {
user = config.NEXT_SMTP_PROXY_USER,
password = config.NEXT_SMTP_PROXY_PASSWORD,
host = config.NEXT_SMTP_PROXY_HOST,
port = config.NEXT_SMTP_PROXY_PORT,
form_name = config.NEXT_SMTP_PROXY_FORM_NAME,
to_email = config.NEXT_SMTP_PROXY_TO_EMAIL,
subject = config.NEXT_SMTP_PROXY_SUBJECT,
text = msg
}
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 end
if config.PUSHOVER_USER_KEY == nil or config.PUSHOVER_USER_KEY== "" then }
log.error("util_notify.notifyToPushover", "未配置 `config.PUSHOVER_USER_KEY`")
return
end
local header = {
["Content-Type"] = "application/json; charset=utf-8"
}
local body = {
token = config.PUSHOVER_API_TOKEN,
user = config.PUSHOVER_USER_KEY,
message = msg
}
local json_data = json.encode(body)
-- LuatOS Bug, json.encode 会将 \n 转换为 \b
json_data = string.gsub(json_data, "\\b", "\\n")
local url = "https://api.pushover.net/1/messages.json"
log.info("util_notify.notifyToPushover", "POST", config.PUSHOVER_API_TOKEN)
return util_http.fetch(nil, "POST", url, header, json_data)
end
-- 发送到 next-smtp-proxy
local function notifyToNextSmtpProxy(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`")
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`")
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`")
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`")
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`")
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`")
return
end
local header = {
["Content-Type"] = "application/x-www-form-urlencoded"
}
local body = {
user = config.NEXT_SMTP_PROXY_USER,
password = config.NEXT_SMTP_PROXY_PASSWORD,
host = config.NEXT_SMTP_PROXY_HOST,
port = config.NEXT_SMTP_PROXY_PORT,
form_name = config.NEXT_SMTP_PROXY_FORM_NAME,
to_email = config.NEXT_SMTP_PROXY_TO_EMAIL,
subject = config.NEXT_SMTP_PROXY_SUBJECT,
text = msg
}
log.info("util_notify.notifyToNextSmtpProxy", "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 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