util_notify.lua 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. local util_notify = {}
  2. local function urlencodeTab(params)
  3. local msg = {}
  4. for k, v in pairs(params) do
  5. table.insert(msg, string.urlEncode(k) .. "=" .. string.urlEncode(v))
  6. table.insert(msg, "&")
  7. end
  8. table.remove(msg)
  9. return table.concat(msg)
  10. end
  11. -- 发送到 telegram
  12. local function notifyToTelegram(msg)
  13. if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then
  14. log.error("util_notify.notifyToTelegram", "未配置 `config.TELEGRAM_PROXY_API`")
  15. return
  16. end
  17. local header = {
  18. ["content-type"] = "text/plain",
  19. ["x-disable-web-page-preview"] = "1",
  20. ["x-chat-id"] = config.TELEGRAM_CHAT_ID or "",
  21. ["x-token"] = config.TELEGRAM_TOKEN or ""
  22. }
  23. log.info("util_notify.notifyToTelegram", "POST", config.TELEGRAM_PROXY_API)
  24. return http.request("POST", config.TELEGRAM_PROXY_API, header, msg).wait()
  25. end
  26. -- 发送到 pushdeer
  27. local function notifyToPushDeer(msg)
  28. if config.PUSHDEER_API == nil or config.PUSHDEER_API == "" then
  29. log.error("util_notify.notifyToPushDeer", "未配置 `config.PUSHDEER_API`")
  30. return
  31. end
  32. if config.PUSHDEER_KEY == nil or config.PUSHDEER_KEY == "" then
  33. log.error("util_notify.notifyToPushDeer", "未配置 `config.PUSHDEER_KEY`")
  34. return
  35. end
  36. local header = {
  37. ["Content-Type"] = "application/x-www-form-urlencoded"
  38. }
  39. local body = {
  40. pushkey = config.PUSHDEER_KEY or "",
  41. type = "text",
  42. text = msg
  43. }
  44. log.info("util_notify.notifyToPushDeer", "POST", config.PUSHDEER_API)
  45. return http.request("POST", config.PUSHDEER_API, header, urlencodeTab(body)).wait()
  46. end
  47. -- 发送到 bark
  48. local function notifyToBark(msg)
  49. if config.BARK_API == nil or config.BARK_API == "" then
  50. log.error("util_notify.notifyToBark", "未配置 `config.BARK_API`")
  51. return
  52. end
  53. if config.BARK_KEY == nil or config.BARK_KEY == "" then
  54. log.error("util_notify.notifyToBark", "未配置 `config.BARK_KEY`")
  55. return
  56. end
  57. local header = {
  58. ["Content-Type"] = "application/x-www-form-urlencoded"
  59. }
  60. local body = {
  61. body = msg
  62. }
  63. local url = config.BARK_API .. "/" .. config.BARK_KEY
  64. log.info("util_notify.notifyToBark", "POST", url)
  65. return http.request("POST", url, header, urlencodeTab(body)).wait()
  66. end
  67. -- 发送到 dingtalk
  68. local function notifyToDingTalk(msg)
  69. if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then
  70. log.error("util_notify.notifyToDingTalk", "未配置 `config.DINGTALK_WEBHOOK`")
  71. return
  72. end
  73. local header = {
  74. ["Content-Type"] = "application/json; charset=utf-8"
  75. }
  76. local body = {
  77. msgtype = "text",
  78. text = {
  79. content = msg
  80. }
  81. }
  82. local json_data = json.encode(body)
  83. -- LuatOS Bug, json.encode 会将 \n 转换为 \b
  84. json_data = string.gsub(json_data, "\\b", "\\n")
  85. log.info("util_notify.notifyToDingTalk", "POST", config.DINGTALK_WEBHOOK, json_data)
  86. return http.request("POST", config.DINGTALK_WEBHOOK, header, json_data).wait()
  87. end
  88. -- 发送到 feishu
  89. local function notifyToFeishu(msg)
  90. if config.FEISHU_WEBHOOK == nil or config.FEISHU_WEBHOOK == "" then
  91. log.error("util_notify.notifyToFeishu", "未配置 `config.FEISHU_WEBHOOK`")
  92. return
  93. end
  94. local header = {
  95. ["Content-Type"] = "application/json; charset=utf-8"
  96. }
  97. local body = {
  98. msg_type = "text",
  99. content = {
  100. text = msg
  101. }
  102. }
  103. local json_data = json.encode(body)
  104. -- LuatOS Bug, json.encode 会将 \n 转换为 \b
  105. json_data = string.gsub(json_data, "\\b", "\\n")
  106. log.info("util_notify.notifyToFeishu", "POST", config.FEISHU_WEBHOOK, json_data)
  107. return http.request("POST", config.FEISHU_WEBHOOK, header, json_data).wait()
  108. end
  109. -- 发送到 wecom
  110. local function notifyToWeCom(msg)
  111. if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then
  112. log.error("util_notify.notifyToWeCom", "未配置 `config.WECOM_WEBHOOK`")
  113. return
  114. end
  115. local header = {
  116. ["Content-Type"] = "application/json; charset=utf-8"
  117. }
  118. local body = {
  119. msgtype = "text",
  120. text = {
  121. content = msg
  122. }
  123. }
  124. local json_data = json.encode(body)
  125. -- LuatOS Bug, json.encode 会将 \n 转换为 \b
  126. json_data = string.gsub(json_data, "\\b", "\\n")
  127. log.info("util_notify.notifyToWeCom", "POST", config.WECOM_WEBHOOK, json_data)
  128. return http.request("POST", config.WECOM_WEBHOOK, header, json_data).wait()
  129. end
  130. -- 发送到 next-smtp-proxy
  131. local function notifyToNextSmtpProxy(msg)
  132. if config.NEXT_SMTP_PROXY_API == nil or config.NEXT_SMTP_PROXY_API == "" then
  133. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_API`")
  134. return
  135. end
  136. if config.NEXT_SMTP_PROXY_USER == nil or config.NEXT_SMTP_PROXY_USER == "" then
  137. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_USER`")
  138. return
  139. end
  140. if config.NEXT_SMTP_PROXY_PASSWORD == nil or config.NEXT_SMTP_PROXY_PASSWORD == "" then
  141. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_PASSWORD`")
  142. return
  143. end
  144. if config.NEXT_SMTP_PROXY_HOST == nil or config.NEXT_SMTP_PROXY_HOST == "" then
  145. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_HOST`")
  146. return
  147. end
  148. if config.NEXT_SMTP_PROXY_PORT == nil or config.NEXT_SMTP_PROXY_PORT == "" then
  149. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_PORT`")
  150. return
  151. end
  152. if config.NEXT_SMTP_PROXY_TO_EMAIL == nil or config.NEXT_SMTP_PROXY_TO_EMAIL == "" then
  153. log.error("util_notify.notifyToNextSmtpProxy", "未配置 `config.NEXT_SMTP_PROXY_TO_EMAIL`")
  154. return
  155. end
  156. local header = {
  157. ["Content-Type"] = "application/x-www-form-urlencoded"
  158. }
  159. local body = {
  160. user = config.NEXT_SMTP_PROXY_USER,
  161. password = config.NEXT_SMTP_PROXY_PASSWORD,
  162. host = config.NEXT_SMTP_PROXY_HOST,
  163. port = config.NEXT_SMTP_PROXY_PORT,
  164. form_name = config.NEXT_SMTP_PROXY_FORM_NAME,
  165. to_email = config.NEXT_SMTP_PROXY_TO_EMAIL,
  166. subject = config.NEXT_SMTP_PROXY_SUBJECT,
  167. text = msg
  168. }
  169. log.info("util_notify.notifyToNextSmtpProxy", "POST", config.NEXT_SMTP_PROXY_API, urlencodeTab(body))
  170. return http.request("POST", config.NEXT_SMTP_PROXY_API, header, urlencodeTab(body)).wait()
  171. end
  172. function util_notify.send(msg)
  173. log.info("util_notify.send", "发送通知", config.NOTIFY_TYPE)
  174. if type(msg) == "table" then
  175. msg = table.concat(msg, "\n")
  176. end
  177. if type(msg) ~= "string" then
  178. log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
  179. return
  180. end
  181. local model = hmeta.model() or ""
  182. local simid = mobile.simid()
  183. local iccid = mobile.iccid(simid) or ""
  184. local rsrp = mobile.rsrp()
  185. local mcc, mnc, band = util_mobile.mcc, util_mobile.mnc, util_mobile.band
  186. local oper = util_mobile.getOper(true)
  187. local lat, lng = util_location.getCoord()
  188. local map_url = "https://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng
  189. msg = msg .. "\n"
  190. if model then
  191. msg = msg .. "\nMODEL: " .. model
  192. end
  193. if iccid then
  194. msg = msg .. "\nICCID: " .. iccid
  195. end
  196. if oper then
  197. msg = msg .. "\n运营商: " .. oper
  198. end
  199. msg = msg .. "\n信号: " .. rsrp .. "dBm"
  200. if band ~= "" then
  201. msg = msg .. "\n频段: B" .. band
  202. end
  203. if lat ~= 0 and lng ~= 0 then
  204. msg = msg .. "\n位置: " .. map_url
  205. end
  206. -- 判断通知类型
  207. local notify
  208. if config.NOTIFY_TYPE == "telegram" then
  209. notify = notifyToTelegram
  210. elseif config.NOTIFY_TYPE == "pushdeer" then
  211. notify = notifyToPushDeer
  212. elseif config.NOTIFY_TYPE == "bark" then
  213. notify = notifyToBark
  214. elseif config.NOTIFY_TYPE == "dingtalk" then
  215. notify = notifyToDingTalk
  216. elseif config.NOTIFY_TYPE == "feishu" then
  217. notify = notifyToFeishu
  218. elseif config.NOTIFY_TYPE == "wecom" then
  219. notify = notifyToWeCom
  220. elseif config.NOTIFY_TYPE == "next-smtp-proxy" then
  221. notify = notifyToNextSmtpProxy
  222. else
  223. log.error("util_notify.send", "发送通知失败", "未配置 `config.NOTIFY_TYPE`")
  224. return
  225. end
  226. sys.taskInit(
  227. function()
  228. sys.wait(100)
  229. local max_retry = 10
  230. local retry_count = 0
  231. while retry_count < max_retry do
  232. local code, headers, body = notify(msg)
  233. if code == 200 then
  234. log.info("util_notify.send", "发送通知成功", "retry_count:", retry_count, "code:", code, "body:", body)
  235. break
  236. else
  237. retry_count = retry_count + 1
  238. log.error("util_notify.send", "发送通知失败", "retry_count:", retry_count, "code:", code, "body:", body)
  239. util_netled.blink(500, 200, 3000)
  240. sys.wait(10000)
  241. end
  242. end
  243. end
  244. )
  245. end
  246. return util_notify