Browse Source

:sparkles: 支持自定义 POST 请求, resolve #8

Mizore 2 years ago
parent
commit
452f6d9692
2 changed files with 47 additions and 2 deletions
  1. 7 2
      script/config.lua
  2. 40 0
      script/util_notify.lua

+ 7 - 2
script/config.lua

@@ -1,7 +1,12 @@
 return {
     -- 通知类型, 支持配置多个
-    -- NOTIFY_TYPE = {"telegram", "pushdeer", "bark", "dingtalk", "feishu", "wecom", "pushover", "inotify", "next-smtp-proxy", "gotify"},
-    NOTIFY_TYPE = "pushdeer",
+    -- NOTIFY_TYPE = {"custom_post", "telegram", "pushdeer", "bark", "dingtalk", "feishu", "wecom", "pushover", "inotify", "next-smtp-proxy", "gotify"},
+    NOTIFY_TYPE = "custom_post",
+    --
+    -- custom_post 通知配置, 自定义 POST 请求, CUSTOM_POST_BODY_TABLE 中的 {msg} 会被替换为通知内容
+    CUSTOM_POST_URL = "https://sctapi.ftqq.com/<SENDKEY>.send",
+    CUSTOM_POST_CONTENT_TYPE = "application/json",
+    CUSTOM_POST_BODY_TABLE = {["title"] = "这里是标题", ["desp"] = "这里是内容, 会被替换掉:\n{msg}\n{msg}"},
     --
     -- telegram 通知配置, https://github.com/0wQ/telegram-notify 或者自行反代
     TELEGRAM_API = "https://api.telegram.org/bot{token}/sendMessage",

+ 40 - 0
script/util_notify.lua

@@ -14,6 +14,46 @@ local function urlencodeTab(params)
 end
 
 local notify = {
+    -- 发送到 custom_post
+    ["custom_post"] = function(msg)
+        if config.CUSTOM_POST_URL == nil or config.CUSTOM_POST_URL == "" then
+            log.error("util_notify", "未配置 `config.CUSTOM_POST_URL`")
+            return
+        end
+        if type(config.CUSTOM_POST_BODY_TABLE) ~= "table" then
+            log.error("util_notify", "未配置 `config.CUSTOM_POST_BODY_TABLE`")
+            return
+        end
+
+        local header = {
+            ["content-type"] = config.CUSTOM_POST_CONTENT_TYPE
+        }
+
+        local body = config.CUSTOM_POST_BODY_TABLE
+        -- 遍历并替换其中的变量
+        local function traverse_and_replace(t)
+            for k, v in pairs(t) do
+                if type(v) == "table" then
+                    traverse_and_replace(v)
+                elseif type(v) == "string" then
+                    t[k] = string.gsub(v, "{msg}", msg)
+                end
+            end
+        end
+        traverse_and_replace(body)
+
+        -- 根据 content-type 进行编码, 默认为 application/x-www-form-urlencoded
+        if string.find(config.CUSTOM_POST_CONTENT_TYPE, "json") then
+            body = json.encode(body)
+            -- LuatOS Bug, json.encode 会将 \n 转换为 \b
+            body = string.gsub(body, "\\b", "\\n")
+        else
+            body = urlencodeTab(body)
+        end
+
+        log.info("util_notify", "POST", config.CUSTOM_POST_URL, config.CUSTOM_POST_CONTENT_TYPE, body)
+        return util_http.fetch(nil, "POST", config.CUSTOM_POST_URL, header, body)
+    end,
     -- 发送到 telegram
     ["telegram"] = function(msg)
         if config.TELEGRAM_API == nil or config.TELEGRAM_API == "" then