Browse Source

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

Mizore 2 years ago
parent
commit
496fafaf02
2 changed files with 220 additions and 237 deletions
  1. 2 1
      script/config.lua
  2. 218 236
      script/util_notify.lua

+ 2 - 1
script/config.lua

@@ -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

+ 218 - 236
script/util_notify.lua

@@ -13,219 +13,212 @@ local function urlencodeTab(params)
     return table.concat(msg)
 end
 
--- 发送到 telegram
-local function notifyToTelegram(msg)
-    if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then
-        log.error("util_notify.notifyToTelegram", "未配置 `config.TELEGRAM_PROXY_API`")
-        return
-    end
-
-    local header = {
-        ["content-type"] = "text/plain",
-        ["x-disable-web-page-preview"] = "1",
-        ["x-chat-id"] = config.TELEGRAM_CHAT_ID 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 notify = {
+    -- 发送到 telegram
+    ["telegram"] = function(msg)
+        if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then
+            log.error("util_notify", "未配置 `config.TELEGRAM_PROXY_API`")
+            return
+        end
 
-    local header = {
-        ["Content-Type"] = "application/json; charset=utf-8"
-    }
-    local body = {
-        msgtype = "text",
-        text = {
-            content = msg
+        local header = {
+            ["content-type"] = "text/plain",
+            ["x-disable-web-page-preview"] = "1",
+            ["x-chat-id"] = config.TELEGRAM_CHAT_ID or "",
+            ["x-token"] = config.TELEGRAM_TOKEN or ""
         }
-    }
-    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)
-    return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data)
-end
 
--- 发送到 feishu
-local function notifyToFeishu(msg)
-    if config.FEISHU_WEBHOOK == nil or config.FEISHU_WEBHOOK == "" then
-        log.error("util_notify.notifyToFeishu", "未配置 `config.FEISHU_WEBHOOK`")
-        return
-    end
+        log.info("util_notify", "POST", config.TELEGRAM_PROXY_API)
+        return util_http.fetch(nil, "POST", config.TELEGRAM_PROXY_API, header, msg)
+    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
 
-    local header = {
-        ["Content-Type"] = "application/json; charset=utf-8"
-    }
-    local body = {
-        msg_type = "text",
-        content = {
+        local header = {
+            ["Content-Type"] = "application/x-www-form-urlencoded"
+        }
+        local body = {
+            pushkey = config.PUSHDEER_KEY or "",
+            type = "text",
             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)
-    return util_http.fetch(nil, "POST", config.FEISHU_WEBHOOK, header, json_data)
-end
 
--- 发送到 wecom
-local function notifyToWeCom(msg)
-    if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then
-        log.error("util_notify.notifyToWeCom", "未配置 `config.WECOM_WEBHOOK`")
-        return
-    end
+        log.info("util_notify", "POST", config.PUSHDEER_API)
+        return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body))
+    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
 
-    local header = {
-        ["Content-Type"] = "application/json; charset=utf-8"
-    }
-    local body = {
-        msgtype = "text",
-        text = {
-            content = msg
+        local header = {
+            ["Content-Type"] = "application/x-www-form-urlencoded"
+        }
+        local body = {
+            body = msg
         }
-    }
-    local json_data = json.encode(body)
-    -- 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", "POST", url)
+        return util_http.fetch(nil, "POST", url, header, urlencodeTab(body))
+    end,
+    -- 发送到 dingtalk
+    ["dingtalk"] = function(msg)
+        if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then
+            log.error("util_notify", "未配置 `config.DINGTALK_WEBHOOK`")
+            return
+        end
 
-    log.info("util_notify.notifyToWeCom", "POST", config.WECOM_WEBHOOK)
-    return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data)
-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.DINGTALK_WEBHOOK)
+        return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data)
+    end,
+    -- 发送到 feishu
+    ["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
 
--- 发送到 pushover
-local function notifyToPushover(msg)
-    if config.PUSHOVER_API_TOKEN == nil or config.PUSHOVER_API_TOKEN == "" then
-        log.error("util_notify.notifyToPushover", "未配置 `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`")
-        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 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 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"
+        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
+        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
+        }
 
--- 发送到 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
+        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 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 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
 
-    if config.NOTIFY_APPEND_MORE_INFO then
-        msg = msg .. append()
+    -- 判断通知渠道 channel
+    if channel and notify[channel] == nil then
+        log.error("util_notify.send", "发送通知失败", "未知通知渠道", channel)
+        return true
     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
+    -- 通知内容追加更多信息
+    if config.NOTIFY_APPEND_MORE_INFO then
+        msg = msg .. append()
     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