diff --git a/README.md b/README.md index 3780d1e..57e9bb5 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ - [x] [telegram](https://github.com/0wQ/telegram-notify) - [x] [pushdeer](https://www.pushdeer.com/) - [x] [bark](https://github.com/Finb/Bark) + - [x] [dingtalk](https://open.dingtalk.com/document/robots/custom-robot-access) - [x] 基站定位 - [x] 定时查询流量 - [x] 开机通知 diff --git a/script/config.lua b/script/config.lua index 5d68b41..4b99697 100644 --- a/script/config.lua +++ b/script/config.lua @@ -1,5 +1,5 @@ return { - -- 通知类型 telegram, pushdeer, bark + -- 通知类型 telegram, pushdeer, bark, dingtalk NOTIFY_TYPE = "pushdeer", -- -- telegram 通知配置, https://github.com/0wQ/telegram-notify @@ -15,6 +15,9 @@ return { BARK_API = "https://api.day.app", BARK_KEY = "", -- + -- dingtalk 通知配置, https://open.dingtalk.com/document/robots/custom-robot-access + DINGTALK_WEBHOOK = "", + -- -- 定时查询流量间隔, 单位毫秒, 设置为 0 关闭 QUERY_TRAFFIC_INTERVAL = 1000 * 60 * 60 * 6, -- diff --git a/script/util_notify.lua b/script/util_notify.lua index 6cf87d0..c6ed9b0 100644 --- a/script/util_notify.lua +++ b/script/util_notify.lua @@ -29,7 +29,7 @@ local function notifyToTelegram(msg) end -- 发送到 pushdeer -local function notifyToPushDeer(msg, httpCallback) +local function notifyToPushDeer(msg) if config.PUSHDEER_API == "" then log.error("util_notify.notifyToPushDeer", "未配置 `config.PUSHDEER_API`") return @@ -53,7 +53,7 @@ local function notifyToPushDeer(msg, httpCallback) end -- 发送到 bark -local function notifyToBark(msg, httpCallback) +local function notifyToBark(msg) if config.BARK_API == "" then log.error("util_notify.notifyToBark", "未配置 `config.BARK_API`") return @@ -75,6 +75,30 @@ local function notifyToBark(msg, httpCallback) return http.request("POST", url, header, urlencodeTab(body)).wait() 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, json_data) + return http.request("POST", config.DINGTALK_WEBHOOK, header, json_data).wait() +end + function util_notify.send(msg) log.info("util_notify.send", "发送通知", config.NOTIFY_TYPE) @@ -121,6 +145,8 @@ function util_notify.send(msg) notify = notifyToPushDeer elseif config.NOTIFY_TYPE == "bark" then notify = notifyToBark + elseif config.NOTIFY_TYPE == "dingtalk" then + notify = notifyToDingTalk else log.error("util_notify.send", "发送通知失败", "未配置 `config.NOTIFY_TYPE`") return @@ -135,7 +161,7 @@ function util_notify.send(msg) while retry_count < max_retry do local code, headers, body = notify(msg) if code == 200 then - log.info("util_notify.send", "发送通知成功", "retry_count:", retry_count) + log.info("util_notify.send", "发送通知成功", "retry_count:", retry_count, "code:", code, "body:", body) break else retry_count = retry_count + 1