util_notify.lua 14 KB

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