util_notify.lua 11 KB

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