支持 dingtalk 通知

This commit is contained in:
Mizore 2023-01-14 23:24:31 +08:00
parent 8978078ef3
commit 92578e5adb
3 changed files with 34 additions and 4 deletions

View File

@ -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] 开机通知

View File

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

View File

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