util_notify.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. -- 发送到 pushover
  133. local function notifyToPushover(msg)
  134. if config.PUSHOVER_API_TOKEN == nil or config.PUSHOVER_API_TOKEN == "" then
  135. log.error("util_notify.notifyToPushover", "未配置 `config.PUSHOVER_API_TOKEN`")
  136. return
  137. end
  138. if config.PUSHOVER_USER_KEY == nil or config.PUSHOVER_USER_KEY== "" then
  139. log.error("util_notify.notifyToPushover", "未配置 `config.PUSHOVER_USER_KEY`")
  140. return
  141. end
  142. local header = {
  143. ["Content-Type"] = "application/json; charset=utf-8"
  144. }
  145. local body = {
  146. token = config.PUSHOVER_API_TOKEN,
  147. user = config.PUSHOVER_USER_KEY,
  148. message = msg
  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. local url = "https://api.pushover.net/1/messages.json"
  154. log.info("util_notify.notifyToPushover", "POST", config.PUSHOVER_API_TOKEN)
  155. return util_http.fetch(nil, "POST", url, header, json_data)
  156. end
  157. -- 发送到 next-smtp-proxy
  158. local function notifyToNextSmtpProxy(msg)
  159. if config.NEXT_SMTP_PROXY_API == nil or config.NEXT_SMTP_PROXY_API == "" then
  160. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_API`")
  161. return
  162. end
  163. if config.NEXT_SMTP_PROXY_USER == nil or config.NEXT_SMTP_PROXY_USER == "" then
  164. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_USER`")
  165. return
  166. end
  167. if config.NEXT_SMTP_PROXY_PASSWORD == nil or config.NEXT_SMTP_PROXY_PASSWORD == "" then
  168. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_PASSWORD`")
  169. return
  170. end
  171. if config.NEXT_SMTP_PROXY_HOST == nil or config.NEXT_SMTP_PROXY_HOST == "" then
  172. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_HOST`")
  173. return
  174. end
  175. if config.NEXT_SMTP_PROXY_PORT == nil or config.NEXT_SMTP_PROXY_PORT == "" then
  176. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_PORT`")
  177. return
  178. end
  179. if config.NEXT_SMTP_PROXY_TO_EMAIL == nil or config.NEXT_SMTP_PROXY_TO_EMAIL == "" then
  180. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_TO_EMAIL`")
  181. return
  182. end
  183. local header = {
  184. ["Content-Type"] = "application/x-www-form-urlencoded"
  185. }
  186. local body = {
  187. user = config.NEXT_SMTP_PROXY_USER,
  188. password = config.NEXT_SMTP_PROXY_PASSWORD,
  189. host = config.NEXT_SMTP_PROXY_HOST,
  190. port = config.NEXT_SMTP_PROXY_PORT,
  191. form_name = config.NEXT_SMTP_PROXY_FORM_NAME,
  192. to_email = config.NEXT_SMTP_PROXY_TO_EMAIL,
  193. subject = config.NEXT_SMTP_PROXY_SUBJECT,
  194. text = msg
  195. }
  196. log.info("util_notify.notifyToNextSmtpProxy", "POST", config.NEXT_SMTP_PROXY_API)
  197. return util_http.fetch(nil, "POST", config.NEXT_SMTP_PROXY_API, header, urlencodeTab(body))
  198. end
  199. local function append()
  200. local msg = "\n"
  201. -- 本机号码
  202. local number = mobile.number(mobile.simid())
  203. if number then
  204. msg = msg .. "\n本机号码: " .. number
  205. end
  206. -- 开机时长
  207. local ms = mcu.ticks()
  208. local seconds = math.floor(ms / 1000)
  209. local minutes = math.floor(seconds / 60)
  210. local hours = math.floor(minutes / 60)
  211. seconds = seconds % 60
  212. minutes = minutes % 60
  213. local boot_time = string.format("%02d:%02d:%02d", hours, minutes, seconds)
  214. if ms >= 0 then
  215. msg = msg .. "\n开机时长: " .. boot_time
  216. end
  217. -- 运营商
  218. local oper = util_mobile.getOper(true)
  219. if oper ~= "" then
  220. msg = msg .. "\n运营商: " .. oper
  221. end
  222. -- 信号
  223. local rsrp = mobile.rsrp()
  224. if rsrp ~= 0 then
  225. msg = msg .. "\n信号: " .. rsrp .. "dBm"
  226. end
  227. -- 频段
  228. local band = util_mobile.getBand()
  229. if band >= 0 then
  230. msg = msg .. "\n频段: B" .. band
  231. end
  232. -- 位置
  233. local _, _, map_link = util_location.get()
  234. if map_link ~= "" then
  235. msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行
  236. end
  237. return msg
  238. end
  239. --- 发送通知
  240. -- @param msg 消息内容
  241. -- @return true: 无需重发, false: 需要重发
  242. function util_notify.send(msg)
  243. log.info("util_notify.send", "发送通知", config.NOTIFY_TYPE)
  244. if type(msg) == "table" then
  245. msg = table.concat(msg, "\n")
  246. end
  247. if type(msg) ~= "string" then
  248. log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
  249. return true
  250. end
  251. if msg == "" then
  252. log.error("util_notify.send", "发送通知失败", "消息为空")
  253. return true
  254. end
  255. if config.NOTIFY_APPEND_MORE_INFO then
  256. msg = msg .. append()
  257. end
  258. -- 判断通知类型
  259. local notify
  260. if config.NOTIFY_TYPE == "telegram" then
  261. notify = notifyToTelegram
  262. elseif config.NOTIFY_TYPE == "pushdeer" then
  263. notify = notifyToPushDeer
  264. elseif config.NOTIFY_TYPE == "bark" then
  265. notify = notifyToBark
  266. elseif config.NOTIFY_TYPE == "dingtalk" then
  267. notify = notifyToDingTalk
  268. elseif config.NOTIFY_TYPE == "feishu" then
  269. notify = notifyToFeishu
  270. elseif config.NOTIFY_TYPE == "wecom" then
  271. notify = notifyToWeCom
  272. elseif config.NOTIFY_TYPE == "pushover" then
  273. notify = notifyToPushover
  274. elseif config.NOTIFY_TYPE == "next-smtp-proxy" then
  275. notify = notifyToNextSmtpProxy
  276. else
  277. log.error("util_notify.send", "发送通知失败", "未配置 `config.NOTIFY_TYPE`")
  278. return true
  279. end
  280. local code, headers, body = notify(msg)
  281. if code == nil then
  282. log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
  283. return true
  284. end
  285. if code == -6 then
  286. -- 发生在 url 过长时, 重发也不会成功
  287. log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
  288. return true
  289. end
  290. if code >= 200 and code < 300 then
  291. -- http 2xx 成功
  292. log.info("util_notify.send", "发送通知成功", "code:", code, "body:", body)
  293. return true
  294. end
  295. if code >= 300 and code < 400 then
  296. -- http 3xx 重定向, 重发也不会成功
  297. log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
  298. return true
  299. end
  300. if code >= 400 and code < 500 then
  301. -- http 4xx 客户端错误, 重发也不会成功
  302. log.info("util_notify.send", "发送通知失败, 无需重发", "code:", code, "body:", body)
  303. return true
  304. end
  305. log.error("util_notify.send", "发送通知失败, 等待重发", "code:", code, "body:", body)
  306. return false
  307. end
  308. --- 添加到消息队列
  309. -- @param msg 消息内容
  310. function util_notify.add(msg)
  311. table.insert(msg_queue, {msg = msg, retry = 0})
  312. sys.publish("NEW_MSG")
  313. log.debug("util_notify.add", "添加到消息队列, 当前队列长度:", #msg_queue)
  314. end
  315. -- 轮询消息队列
  316. -- 发送成功则从消息队列中删除
  317. -- 发送失败则等待下次轮询
  318. local function poll()
  319. local item, result
  320. while true do
  321. -- 消息队列非空, 且网络已注册
  322. if next(msg_queue) ~= nil and mobile.status() == 1 then
  323. log.debug("util_notify.poll", "轮询消息队列中, 当前队列长度:", #msg_queue)
  324. item = msg_queue[1]
  325. table.remove(msg_queue, 1)
  326. if item.retry > (config.NOTIFY_RETRY_MAX or 100) then
  327. log.error("util_notify.poll", "超过最大重发次数", "msg:", item.msg)
  328. else
  329. result = util_notify.send(item.msg)
  330. item.retry = item.retry + 1
  331. if not result then
  332. -- 发送失败, 移到队尾
  333. table.insert(msg_queue, item)
  334. sys.wait(5000)
  335. end
  336. end
  337. sys.wait(50)
  338. else
  339. sys.waitUntil("NEW_MSG", 1000 * 10)
  340. end
  341. end
  342. end
  343. sys.taskInit(poll)
  344. return util_notify