util_notify.lua 15 KB

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