util_notify.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. local lib_smtp = require "lib_smtp"
  2. local util_notify = {}
  3. -- 消息队列
  4. local msg_queue = {}
  5. local function urlencodeTab(params)
  6. local msg = {}
  7. for k, v in pairs(params) do
  8. table.insert(msg, string.urlEncode(k) .. "=" .. string.urlEncode(v))
  9. table.insert(msg, "&")
  10. end
  11. table.remove(msg)
  12. return table.concat(msg)
  13. end
  14. local notify = {
  15. -- 发送到 custom_post
  16. ["custom_post"] = function(msg)
  17. if config.CUSTOM_POST_URL == nil or config.CUSTOM_POST_URL == "" then
  18. log.error("util_notify", "未配置 `config.CUSTOM_POST_URL`")
  19. return
  20. end
  21. if type(config.CUSTOM_POST_BODY_TABLE) ~= "table" then
  22. log.error("util_notify", "未配置 `config.CUSTOM_POST_BODY_TABLE`")
  23. return
  24. end
  25. local header = { ["content-type"] = config.CUSTOM_POST_CONTENT_TYPE }
  26. local body = json.decode(json.encode(config.CUSTOM_POST_BODY_TABLE))
  27. -- 遍历并替换其中的变量
  28. local function traverse_and_replace(t)
  29. for k, v in pairs(t) do
  30. if type(v) == "table" then
  31. traverse_and_replace(v)
  32. elseif type(v) == "string" then
  33. t[k] = string.gsub(v, "{msg}", msg)
  34. end
  35. end
  36. end
  37. traverse_and_replace(body)
  38. -- 根据 content-type 进行编码, 默认为 application/x-www-form-urlencoded
  39. if string.find(config.CUSTOM_POST_CONTENT_TYPE, "json") then
  40. body = json.encode(body)
  41. -- LuatOS Bug, json.encode 会将 \n 转换为 \b
  42. body = string.gsub(body, "\\b", "\\n")
  43. else
  44. body = urlencodeTab(body)
  45. end
  46. log.info("util_notify", "POST", config.CUSTOM_POST_URL, config.CUSTOM_POST_CONTENT_TYPE, body)
  47. return util_http.fetch(nil, "POST", config.CUSTOM_POST_URL, header, body)
  48. end,
  49. -- 发送到 telegram
  50. ["telegram"] = function(msg)
  51. if config.TELEGRAM_API == nil or config.TELEGRAM_API == "" then
  52. log.error("util_notify", "未配置 `config.TELEGRAM_API`")
  53. return
  54. end
  55. if config.TELEGRAM_CHAT_ID == nil or config.TELEGRAM_CHAT_ID == "" then
  56. log.error("util_notify", "未配置 `config.TELEGRAM_CHAT_ID`")
  57. return
  58. end
  59. local header = { ["content-type"] = "application/json" }
  60. local body = { ["chat_id"] = config.TELEGRAM_CHAT_ID, ["disable_web_page_preview"] = true, ["text"] = msg }
  61. local json_data = json.encode(body)
  62. json_data = string.gsub(json_data, "\\b", "\\n")
  63. log.info("util_notify", "POST", config.TELEGRAM_API)
  64. return util_http.fetch(nil, "POST", config.TELEGRAM_API, header, json_data)
  65. end,
  66. -- 发送到 gotify
  67. ["gotify"] = function(msg)
  68. if config.GOTIFY_API == nil or config.GOTIFY_API == "" then
  69. log.error("util_notify", "未配置 `config.GOTIFY_API`")
  70. return
  71. end
  72. if config.GOTIFY_TOKEN == nil or config.GOTIFY_TOKEN == "" then
  73. log.error("util_notify", "未配置 `config.GOTIFY_TOKEN`")
  74. return
  75. end
  76. local url = config.GOTIFY_API .. "/message?token=" .. config.GOTIFY_TOKEN
  77. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  78. local body = { title = config.GOTIFY_TITLE, message = msg, priority = config.GOTIFY_PRIORITY }
  79. local json_data = json.encode(body)
  80. json_data = string.gsub(json_data, "\\b", "\\n")
  81. log.info("util_notify", "POST", config.GOTIFY_API)
  82. return util_http.fetch(nil, "POST", url, header, json_data)
  83. end,
  84. -- 发送到 pushdeer
  85. ["pushdeer"] = function(msg)
  86. if config.PUSHDEER_API == nil or config.PUSHDEER_API == "" then
  87. log.error("util_notify", "未配置 `config.PUSHDEER_API`")
  88. return
  89. end
  90. if config.PUSHDEER_KEY == nil or config.PUSHDEER_KEY == "" then
  91. log.error("util_notify", "未配置 `config.PUSHDEER_KEY`")
  92. return
  93. end
  94. local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
  95. local body = { pushkey = config.PUSHDEER_KEY or "", type = "text", text = msg }
  96. log.info("util_notify", "POST", config.PUSHDEER_API)
  97. return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body))
  98. end,
  99. -- 发送到 bark
  100. ["bark"] = function(msg)
  101. if config.BARK_API == nil or config.BARK_API == "" then
  102. log.error("util_notify", "未配置 `config.BARK_API`")
  103. return
  104. end
  105. if config.BARK_KEY == nil or config.BARK_KEY == "" then
  106. log.error("util_notify", "未配置 `config.BARK_KEY`")
  107. return
  108. end
  109. local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
  110. local body = { body = msg }
  111. local url = config.BARK_API .. "/" .. config.BARK_KEY
  112. log.info("util_notify", "POST", url)
  113. return util_http.fetch(nil, "POST", url, header, urlencodeTab(body))
  114. end,
  115. -- 发送到 dingtalk
  116. ["dingtalk"] = function(msg)
  117. if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then
  118. log.error("util_notify", "未配置 `config.DINGTALK_WEBHOOK`")
  119. return
  120. end
  121. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  122. local body = { msgtype = "text", text = { content = msg } }
  123. local json_data = json.encode(body)
  124. -- LuatOS Bug, json.encode 会将 \n 转换为 \b
  125. json_data = string.gsub(json_data, "\\b", "\\n")
  126. log.info("util_notify", "POST", config.DINGTALK_WEBHOOK)
  127. return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data)
  128. end,
  129. -- 发送到 feishu
  130. ["feishu"] = function(msg)
  131. if config.FEISHU_WEBHOOK == nil or config.FEISHU_WEBHOOK == "" then
  132. log.error("util_notify", "未配置 `config.FEISHU_WEBHOOK`")
  133. return
  134. end
  135. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  136. local body = { msg_type = "text", content = { text = msg } }
  137. local json_data = json.encode(body)
  138. -- LuatOS Bug, json.encode 会将 \n 转换为 \b
  139. json_data = string.gsub(json_data, "\\b", "\\n")
  140. log.info("util_notify", "POST", config.FEISHU_WEBHOOK)
  141. return util_http.fetch(nil, "POST", config.FEISHU_WEBHOOK, header, json_data)
  142. end,
  143. -- 发送到 wecom
  144. ["wecom"] = function(msg)
  145. if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then
  146. log.error("util_notify", "未配置 `config.WECOM_WEBHOOK`")
  147. return
  148. end
  149. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  150. local body = { msgtype = "text", text = { content = msg } }
  151. local json_data = json.encode(body)
  152. -- LuatOS Bug, json.encode 会将 \n 转换为 \b
  153. json_data = string.gsub(json_data, "\\b", "\\n")
  154. log.info("util_notify", "POST", config.WECOM_WEBHOOK)
  155. return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data)
  156. end,
  157. -- 发送到 pushover
  158. ["pushover"] = function(msg)
  159. if config.PUSHOVER_API_TOKEN == nil or config.PUSHOVER_API_TOKEN == "" then
  160. log.error("util_notify", "未配置 `config.PUSHOVER_API_TOKEN`")
  161. return
  162. end
  163. if config.PUSHOVER_USER_KEY == nil or config.PUSHOVER_USER_KEY == "" then
  164. log.error("util_notify", "未配置 `config.PUSHOVER_USER_KEY`")
  165. return
  166. end
  167. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  168. local body = { token = config.PUSHOVER_API_TOKEN, user = config.PUSHOVER_USER_KEY, message = msg }
  169. local json_data = json.encode(body)
  170. -- LuatOS Bug, json.encode 会将 \n 转换为 \b
  171. json_data = string.gsub(json_data, "\\b", "\\n")
  172. local url = "https://api.pushover.net/1/messages.json"
  173. log.info("util_notify", "POST", url)
  174. return util_http.fetch(nil, "POST", url, header, json_data)
  175. end,
  176. -- 发送到 inotify
  177. ["inotify"] = function(msg)
  178. if config.INOTIFY_API == nil or config.INOTIFY_API == "" then
  179. log.error("util_notify", "未配置 `config.INOTIFY_API`")
  180. return
  181. end
  182. if not config.INOTIFY_API:endsWith(".send") then
  183. log.error("util_notify", "`config.INOTIFY_API` 必须以 `.send` 结尾")
  184. return
  185. end
  186. local url = config.INOTIFY_API .. "/" .. string.urlEncode(msg)
  187. log.info("util_notify", "GET", url)
  188. return util_http.fetch(nil, "GET", url)
  189. end,
  190. -- 发送到 next-smtp-proxy
  191. ["next-smtp-proxy"] = function(msg)
  192. if config.NEXT_SMTP_PROXY_API == nil or config.NEXT_SMTP_PROXY_API == "" then
  193. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_API`")
  194. return
  195. end
  196. if config.NEXT_SMTP_PROXY_USER == nil or config.NEXT_SMTP_PROXY_USER == "" then
  197. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_USER`")
  198. return
  199. end
  200. if config.NEXT_SMTP_PROXY_PASSWORD == nil or config.NEXT_SMTP_PROXY_PASSWORD == "" then
  201. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_PASSWORD`")
  202. return
  203. end
  204. if config.NEXT_SMTP_PROXY_HOST == nil or config.NEXT_SMTP_PROXY_HOST == "" then
  205. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_HOST`")
  206. return
  207. end
  208. if config.NEXT_SMTP_PROXY_PORT == nil or config.NEXT_SMTP_PROXY_PORT == "" then
  209. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_PORT`")
  210. return
  211. end
  212. if config.NEXT_SMTP_PROXY_TO_EMAIL == nil or config.NEXT_SMTP_PROXY_TO_EMAIL == "" then
  213. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_TO_EMAIL`")
  214. return
  215. end
  216. local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
  217. local body = {
  218. user = config.NEXT_SMTP_PROXY_USER,
  219. password = config.NEXT_SMTP_PROXY_PASSWORD,
  220. host = config.NEXT_SMTP_PROXY_HOST,
  221. port = config.NEXT_SMTP_PROXY_PORT,
  222. form_name = config.NEXT_SMTP_PROXY_FORM_NAME,
  223. to_email = config.NEXT_SMTP_PROXY_TO_EMAIL,
  224. subject = config.NEXT_SMTP_PROXY_SUBJECT,
  225. text = msg,
  226. }
  227. log.info("util_notify", "POST", config.NEXT_SMTP_PROXY_API)
  228. return util_http.fetch(nil, "POST", config.NEXT_SMTP_PROXY_API, header, urlencodeTab(body))
  229. end,
  230. ["smtp"] = function(msg)
  231. local smtp_config = {
  232. host = config.SMTP_HOST,
  233. port = config.SMTP_PORT,
  234. username = config.SMTP_USERNAME,
  235. password = config.SMTP_PASSWORD,
  236. mail_from = config.SMTP_MAIL_FROM,
  237. mail_to = config.SMTP_MAIL_TO,
  238. tls_enable = config.SMTP_TLS_ENABLE,
  239. }
  240. local result = lib_smtp.send(msg, config.SMTP_MAIL_SUBJECT, smtp_config)
  241. log.info("util_notify", "SMTP", result.success, result.message, result.is_retry)
  242. if result.success then return 200, nil, result.message end
  243. if result.is_retry then return 500, nil, result.message end
  244. return 400, nil, result.message
  245. end,
  246. -- 发送到 serial
  247. ["serial"] = function(msg)
  248. uart.write(1, msg)
  249. log.info("util_notify", "serial", "消息已转发到串口")
  250. sys.wait(1000)
  251. return 200
  252. end,
  253. }
  254. local function append()
  255. local msg = "\n"
  256. -- 本机号码
  257. local number = mobile.number(mobile.simid())
  258. if number then msg = msg .. "\n本机号码: " .. number end
  259. -- 开机时长
  260. local ms = mcu.ticks()
  261. local seconds = math.floor(ms / 1000)
  262. local minutes = math.floor(seconds / 60)
  263. local hours = math.floor(minutes / 60)
  264. seconds = seconds % 60
  265. minutes = minutes % 60
  266. local boot_time = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  267. if ms >= 0 then msg = msg .. "\n开机时长: " .. boot_time end
  268. -- 运营商
  269. local oper = util_mobile.getOper(true)
  270. if oper ~= "" then msg = msg .. "\n运营商: " .. oper end
  271. -- 信号
  272. local rsrp = mobile.rsrp()
  273. if rsrp ~= 0 then msg = msg .. "\n信号: " .. rsrp .. "dBm" end
  274. -- 频段
  275. local band = util_mobile.getBand()
  276. if band >= 0 then msg = msg .. "\n频段: B" .. band end
  277. -- 流量统计
  278. local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic()
  279. uplinkB = uplinkGB * 1024 * 1024 * 1024 + uplinkB
  280. downlinkB = downlinkGB * 1024 * 1024 * 1024 + downlinkB
  281. local function formatBytes(bytes)
  282. if bytes < 1024 then
  283. return bytes .. "B"
  284. elseif bytes < 1024 * 1024 then
  285. return string.format("%.2fKB", bytes / 1024)
  286. elseif bytes < 1024 * 1024 * 1024 then
  287. return string.format("%.2fMB", bytes / 1024 / 1024)
  288. else
  289. return string.format("%.2fGB", bytes / 1024 / 1024 / 1024)
  290. end
  291. end
  292. -- msg = msg .. "\n流量: ↑" .. formatBytes(uplinkB) .. " ↓" .. formatBytes(downlinkB)
  293. -- 位置
  294. local _, _, map_link = util_location.get()
  295. if map_link ~= "" then
  296. msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行
  297. end
  298. return msg
  299. end
  300. --- 发送通知
  301. -- @param msg 消息内容
  302. -- @param channel 通知渠道
  303. -- @return true: 无需重发, false: 需要重发
  304. function util_notify.send(msg, channel)
  305. log.info("util_notify.send", "发送通知", channel)
  306. -- 判断消息内容 msg
  307. if type(msg) ~= "string" then
  308. log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
  309. return true
  310. end
  311. if msg == "" then
  312. log.error("util_notify.send", "发送通知失败", "消息为空")
  313. return true
  314. end
  315. -- 判断通知渠道 channel
  316. if channel and notify[channel] == nil then
  317. log.error("util_notify.send", "发送通知失败", "未知通知渠道", channel)
  318. return true
  319. end
  320. -- 发送通知
  321. local code, headers, body = notify[channel](msg)
  322. if code == nil then
  323. log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
  324. return true
  325. end
  326. if code == -6 then
  327. -- 发生在 url 过长时, 重发也不会成功
  328. log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
  329. return true
  330. end
  331. if code >= 200 and code < 500 then
  332. -- http 2xx 成功
  333. -- http 3xx 重定向, 重发也不会成功
  334. -- http 4xx 客户端错误, 重发也不会成功
  335. log.info("util_notify.send", "发送通知成功", "code:", code, "body:", body)
  336. return true
  337. end
  338. log.error("util_notify.send", "发送通知失败, 等待重发", "code:", code, "body:", body)
  339. return false
  340. end
  341. --- 添加到消息队列
  342. -- @param msg 消息内容
  343. -- @param channels 通知渠道
  344. function util_notify.add(msg, channels)
  345. if type(msg) == "table" then msg = table.concat(msg, "\n") end
  346. -- 通知内容追加更多信息, 若已经包含则不再追加
  347. local is_append = true
  348. if string.find(msg, "本机号码:") and string.find(msg, "开机时长:") then
  349. log.info("util_notify.send", "不追加更多信息")
  350. is_append = false
  351. end
  352. if config.NOTIFY_APPEND_MORE_INFO and is_append then msg = msg .. append() end
  353. channels = channels or config.NOTIFY_TYPE
  354. if type(channels) ~= "table" then channels = { channels } end
  355. for _, channel in ipairs(channels) do table.insert(msg_queue, { channel = channel, msg = msg, retry = 0 }) end
  356. sys.publish("NEW_MSG")
  357. log.debug("util_notify.add", "添加到消息队列, 当前队列长度:", #msg_queue)
  358. log.debug("util_notify.add", "添加到消息队列的内容:", msg:gsub("\r", "\\r"):gsub("\n", "\\n"))
  359. end
  360. -- 轮询消息队列
  361. -- 发送成功则从消息队列中删除
  362. -- 发送失败则等待下次轮询
  363. local function poll()
  364. local item, result, roaming
  365. while true do
  366. -- 消息队列非空, 且网络已注册
  367. if next(msg_queue) ~= nil and (mobile.status() == 1 or mobile.status() == 5) then
  368. -- 判断是否漫游
  369. if mobile.status() == 5 then
  370. roaming = true
  371. else
  372. roaming = false
  373. end
  374. log.debug("util_notify.poll", "轮询消息队列中, 当前队列长度:", #msg_queue)
  375. item = msg_queue[1]
  376. table.remove(msg_queue, 1)
  377. if item.retry > (config.NOTIFY_RETRY_MAX or 100) then
  378. log.error("util_notify.poll", "超过最大重发次数", "msg:", item.msg)
  379. else
  380. if roaming then
  381. log.debug("util_notify.poll", "当前设备处于漫游状态只使用 Serial 通信方式")
  382. result = util_notify.send(item.msg, 'serial')
  383. else
  384. result = util_notify.send(item.msg, item.channel)
  385. end
  386. item.retry = item.retry + 1
  387. if not result then
  388. -- 发送失败, 移到队尾
  389. table.insert(msg_queue, item)
  390. sys.wait(5000)
  391. end
  392. end
  393. sys.wait(50)
  394. else
  395. sys.waitUntil("NEW_MSG", 1000 * 10)
  396. end
  397. end
  398. end
  399. sys.taskInit(poll)
  400. return util_notify