Browse Source

:sparkles: 支持 dingtalk 加签方式调用

Mizore 2 years ago
parent
commit
cb7d2f182a
2 changed files with 15 additions and 7 deletions
  1. 4 2
      script/config.lua
  2. 11 5
      script/util_notify.lua

+ 4 - 2
script/config.lua

@@ -5,7 +5,7 @@ return {
     --
     -- 通知类型, 支持配置多个
     -- NOTIFY_TYPE = {"custom_post", "telegram", "pushdeer", "bark", "dingtalk", "feishu", "wecom", "pushover", "inotify", "next-smtp-proxy", "gotify", "serial"},
-    NOTIFY_TYPE = "serial",
+    NOTIFY_TYPE = "custom_post",
     --
     -- custom_post 通知配置, 自定义 POST 请求, CUSTOM_POST_BODY_TABLE 中的 {msg} 会被替换为通知内容
     CUSTOM_POST_URL = "https://sctapi.ftqq.com/<SENDKEY>.send",
@@ -25,7 +25,9 @@ return {
     BARK_KEY = "",
     --
     -- dingtalk 通知配置, https://open.dingtalk.com/document/robots/custom-robot-access
+    -- 如果是加签方式, 请填写 DINGTALK_SECRET, 否则留空为自定义关键词方式, https://open.dingtalk.com/document/robots/customize-robot-security-settings
     DINGTALK_WEBHOOK = "",
+    DINGTALK_SECRET = "",
     --
     -- feishu 通知配置, https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN
     FEISHU_WEBHOOK = "",
@@ -70,7 +72,7 @@ return {
     QUERY_TRAFFIC_INTERVAL = 0,
     --
     -- 定时基站定位间隔, 单位毫秒, 设置为 0 关闭 (定位成功后会追加到通知内容后面, 基站定位本身会消耗流量, 通知内容增加也会导致流量消耗增加)
-    LOCATION_INTERVAL = 0,
+    LOCATION_INTERVAL = 30 * 1000,
     --
     -- 开机通知 (会消耗流量)
     BOOT_NOTIFY = true,

+ 11 - 5
script/util_notify.lua

@@ -135,14 +135,20 @@ local notify = {
             return
         end
 
+        local url = config.DINGTALK_WEBHOOK
+        -- 如果配置了 config.DINGTALK_SECRET 则需要签名(加签), 没配置则为自定义关键词
+        if (config.DINGTALK_SECRET ~= nil and config.DINGTALK_SECRET ~= "") then
+            local timestamp = tostring(os.time()) .. "000"
+            local sign = crypto.hmac_sha256(timestamp .. "\n" .. config.DINGTALK_SECRET, config.DINGTALK_SECRET):fromHex():toBase64():urlEncode()
+            url = url .. "&timestamp=" .. timestamp .. "&sign=" .. sign
+        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")
+        body = json.encode(body)
 
-        log.info("util_notify", "POST", config.DINGTALK_WEBHOOK)
-        return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data)
+        log.info("util_notify", "POST", url)
+        return util_http.fetch(nil, "POST", url, header, body)
     end,
     -- 发送到 feishu
     ["feishu"] = function(msg)