util_notify.lua 12 KB

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