util_notify.lua 17 KB

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