util_notify.lua 16 KB

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