util_notify.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. -- 发送到 serial
  304. ["serial"] = function(msg)
  305. uart.write(1, msg)
  306. log.info("util_notify", msg)
  307. log.info("util_notify", "消息已转发到串口")
  308. sys.wait(1000)
  309. return 200
  310. end,
  311. }
  312. local function append()
  313. local msg = "\n"
  314. -- 本机号码
  315. local number = mobile.number(mobile.simid())
  316. if number then
  317. msg = msg .. "\n本机号码: " .. number
  318. end
  319. -- 开机时长
  320. local ms = mcu.ticks()
  321. local seconds = math.floor(ms / 1000)
  322. local minutes = math.floor(seconds / 60)
  323. local hours = math.floor(minutes / 60)
  324. seconds = seconds % 60
  325. minutes = minutes % 60
  326. local boot_time = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  327. if ms >= 0 then
  328. msg = msg .. "\n开机时长: " .. boot_time
  329. end
  330. -- 运营商
  331. local oper = util_mobile.getOper(true)
  332. if oper ~= "" then
  333. msg = msg .. "\n运营商: " .. oper
  334. end
  335. -- 信号
  336. local rsrp = mobile.rsrp()
  337. if rsrp ~= 0 then
  338. msg = msg .. "\n信号: " .. rsrp .. "dBm"
  339. end
  340. -- 频段
  341. local band = util_mobile.getBand()
  342. if band >= 0 then
  343. msg = msg .. "\n频段: B" .. band
  344. end
  345. -- 流量统计
  346. local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic()
  347. uplinkB = uplinkGB * 1024 * 1024 * 1024 + uplinkB
  348. downlinkB = downlinkGB * 1024 * 1024 * 1024 + downlinkB
  349. local function formatBytes(bytes)
  350. if bytes < 1024 then
  351. return bytes .. "B"
  352. elseif bytes < 1024 * 1024 then
  353. return string.format("%.2fKB", bytes / 1024)
  354. elseif bytes < 1024 * 1024 * 1024 then
  355. return string.format("%.2fMB", bytes / 1024 / 1024)
  356. else
  357. return string.format("%.2fGB", bytes / 1024 / 1024 / 1024)
  358. end
  359. end
  360. -- msg = msg .. "\n流量: ↑" .. formatBytes(uplinkB) .. " ↓" .. formatBytes(downlinkB)
  361. -- 位置
  362. local _, _, map_link = util_location.get()
  363. if map_link ~= "" then
  364. msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行
  365. end
  366. return msg
  367. end
  368. --- 发送通知
  369. -- @param msg 消息内容
  370. -- @param channel 通知渠道
  371. -- @return true: 无需重发, false: 需要重发
  372. function util_notify.send(msg, channel)
  373. log.info("util_notify.send", "发送通知", channel)
  374. -- 判断消息内容 msg
  375. if type(msg) ~= "string" then
  376. log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
  377. return true
  378. end
  379. if msg == "" then
  380. log.error("util_notify.send", "发送通知失败", "消息为空")
  381. return true
  382. end
  383. -- 判断通知渠道 channel
  384. if channel and notify[channel] == nil then
  385. log.error("util_notify.send", "发送通知失败", "未知通知渠道", channel)
  386. return true
  387. end
  388. -- 通知内容追加更多信息
  389. -- 若已经包含则不再追加
  390. local isappend = true
  391. if string.find(msg,"本机号码:") and string.find(msg,"开机时长:") then
  392. log.info("util_notify.send", "不追加更多信息")
  393. isappend = false
  394. end
  395. if config.NOTIFY_APPEND_MORE_INFO and isappend then
  396. msg = msg .. append()
  397. end
  398. -- 发送通知
  399. local code, headers, body = notify[channel](msg)
  400. if code == nil then
  401. log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
  402. return true
  403. end
  404. if code == -6 then
  405. -- 发生在 url 过长时, 重发也不会成功
  406. log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
  407. return true
  408. end
  409. if code >= 200 and code < 500 then
  410. -- http 2xx 成功
  411. -- http 3xx 重定向, 重发也不会成功
  412. -- http 4xx 客户端错误, 重发也不会成功
  413. log.info("util_notify.send", "发送通知成功", "code:", code, "body:", body)
  414. return true
  415. end
  416. log.error("util_notify.send", "发送通知失败, 等待重发", "code:", code, "body:", body)
  417. return false
  418. end
  419. --- 添加到消息队列
  420. -- @param msg 消息内容
  421. -- @param channels 通知渠道
  422. function util_notify.add(msg, channels)
  423. if type(msg) == "table" then
  424. msg = table.concat(msg, "\n")
  425. end
  426. channels = channels or config.NOTIFY_TYPE
  427. if type(channels) ~= "table" then
  428. channels = {channels}
  429. end
  430. for _, channel in ipairs(channels) do
  431. table.insert(msg_queue, {channel = channel, msg = msg, retry = 0})
  432. end
  433. sys.publish("NEW_MSG")
  434. log.debug("util_notify.add", "添加到消息队列, 当前队列长度:", #msg_queue)
  435. log.debug("util_notify.add", "添加到消息队列的内容:", msg)
  436. end
  437. -- 轮询消息队列
  438. -- 发送成功则从消息队列中删除
  439. -- 发送失败则等待下次轮询
  440. local function poll()
  441. local item, result, roaming
  442. while true do
  443. -- 消息队列非空, 且网络已注册
  444. if next(msg_queue) ~= nil and (mobile.status() == 1 or mobile.status() == 5) then
  445. -- 判断是否漫游
  446. if mobile.status() == 5 then
  447. roaming = true
  448. else
  449. roaming = false
  450. end
  451. log.debug("util_notify.poll", "轮询消息队列中, 当前队列长度:", #msg_queue)
  452. item = msg_queue[1]
  453. table.remove(msg_queue, 1)
  454. if item.retry > (config.NOTIFY_RETRY_MAX or 100) then
  455. log.error("util_notify.poll", "超过最大重发次数", "msg:", item.msg)
  456. else
  457. if roaming then
  458. log.debug("util_notify.poll", "当前设备处于漫游状态只使用 Serial 通信方式")
  459. result = util_notify.send(item.msg, 'serial')
  460. else
  461. result = util_notify.send(item.msg, item.channel)
  462. end
  463. item.retry = item.retry + 1
  464. if not result then
  465. -- 发送失败, 移到队尾
  466. table.insert(msg_queue, item)
  467. sys.wait(5000)
  468. end
  469. end
  470. sys.wait(50)
  471. else
  472. sys.waitUntil("NEW_MSG", 1000 * 10)
  473. end
  474. end
  475. end
  476. sys.taskInit(poll)
  477. return util_notify