util_notify.lua 14 KB

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