util_notify.lua 17 KB

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