util_notify.lua 13 KB

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