util_notify_channel.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. local lib_smtp = require "lib_smtp"
  2. return {
  3. -- 发送到 custom_post
  4. ["custom_post"] = function(msg)
  5. if config.CUSTOM_POST_URL == nil or config.CUSTOM_POST_URL == "" then
  6. log.error("util_notify", "未配置 `config.CUSTOM_POST_URL`")
  7. return
  8. end
  9. if type(config.CUSTOM_POST_BODY_TABLE) ~= "table" then
  10. log.error("util_notify", "未配置 `config.CUSTOM_POST_BODY_TABLE`")
  11. return
  12. end
  13. local header = { ["content-type"] = config.CUSTOM_POST_CONTENT_TYPE }
  14. local body = json.decode(json.encode(config.CUSTOM_POST_BODY_TABLE))
  15. -- 遍历并替换其中的变量
  16. local function traverse_and_replace(t)
  17. for k, v in pairs(t) do
  18. if type(v) == "table" then
  19. traverse_and_replace(v)
  20. elseif type(v) == "string" then
  21. t[k] = string.gsub(v, "{msg}", msg)
  22. end
  23. end
  24. end
  25. traverse_and_replace(body)
  26. -- 根据 content-type 进行编码, 默认为 application/x-www-form-urlencoded
  27. if string.find(config.CUSTOM_POST_CONTENT_TYPE, "json") then
  28. body = json.encode(body)
  29. else
  30. body = urlencodeTab(body)
  31. end
  32. log.info("util_notify", "POST", config.CUSTOM_POST_URL, config.CUSTOM_POST_CONTENT_TYPE, body)
  33. return util_http.fetch(nil, "POST", config.CUSTOM_POST_URL, header, body)
  34. end,
  35. -- 发送到 telegram
  36. ["telegram"] = function(msg)
  37. if config.TELEGRAM_API == nil or config.TELEGRAM_API == "" then
  38. log.error("util_notify", "未配置 `config.TELEGRAM_API`")
  39. return
  40. end
  41. if config.TELEGRAM_CHAT_ID == nil or config.TELEGRAM_CHAT_ID == "" then
  42. log.error("util_notify", "未配置 `config.TELEGRAM_CHAT_ID`")
  43. return
  44. end
  45. local header = { ["content-type"] = "application/json" }
  46. local body = { ["chat_id"] = config.TELEGRAM_CHAT_ID, ["disable_web_page_preview"] = true, ["text"] = msg }
  47. log.info("util_notify", "POST", config.TELEGRAM_API)
  48. return util_http.fetch(nil, "POST", config.TELEGRAM_API, header, json.encode(body))
  49. end,
  50. -- 发送到 gotify
  51. ["gotify"] = function(msg)
  52. if config.GOTIFY_API == nil or config.GOTIFY_API == "" then
  53. log.error("util_notify", "未配置 `config.GOTIFY_API`")
  54. return
  55. end
  56. if config.GOTIFY_TOKEN == nil or config.GOTIFY_TOKEN == "" then
  57. log.error("util_notify", "未配置 `config.GOTIFY_TOKEN`")
  58. return
  59. end
  60. local url = config.GOTIFY_API .. "/message?token=" .. config.GOTIFY_TOKEN
  61. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  62. local body = { title = config.GOTIFY_TITLE, message = msg, priority = config.GOTIFY_PRIORITY }
  63. log.info("util_notify", "POST", config.GOTIFY_API)
  64. return util_http.fetch(nil, "POST", url, header, json.encode(body))
  65. end,
  66. -- 发送到 pushdeer
  67. ["pushdeer"] = function(msg)
  68. if config.PUSHDEER_API == nil or config.PUSHDEER_API == "" then
  69. log.error("util_notify", "未配置 `config.PUSHDEER_API`")
  70. return
  71. end
  72. if config.PUSHDEER_KEY == nil or config.PUSHDEER_KEY == "" then
  73. log.error("util_notify", "未配置 `config.PUSHDEER_KEY`")
  74. return
  75. end
  76. local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
  77. local body = { pushkey = config.PUSHDEER_KEY or "", type = "text", text = msg }
  78. log.info("util_notify", "POST", config.PUSHDEER_API)
  79. return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body))
  80. end,
  81. -- 发送到 bark
  82. ["bark"] = function(msg)
  83. if config.BARK_API == nil or config.BARK_API == "" then
  84. log.error("util_notify", "未配置 `config.BARK_API`")
  85. return
  86. end
  87. if config.BARK_KEY == nil or config.BARK_KEY == "" then
  88. log.error("util_notify", "未配置 `config.BARK_KEY`")
  89. return
  90. end
  91. local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
  92. local body = { body = msg }
  93. local url = config.BARK_API .. "/" .. config.BARK_KEY
  94. log.info("util_notify", "POST", url)
  95. return util_http.fetch(nil, "POST", url, header, urlencodeTab(body))
  96. end,
  97. -- 发送到 dingtalk
  98. ["dingtalk"] = function(msg)
  99. if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then
  100. log.error("util_notify", "未配置 `config.DINGTALK_WEBHOOK`")
  101. return
  102. end
  103. local url = config.DINGTALK_WEBHOOK
  104. -- 如果配置了 config.DINGTALK_SECRET 则需要签名(加签), 没配置则为自定义关键词
  105. if (config.DINGTALK_SECRET and config.DINGTALK_SECRET ~= "") then
  106. -- 时间异常则等待同步
  107. if os.time() < 1714500000 then
  108. socket.sntp()
  109. sys.waitUntil("NTP_UPDATE", 1000 * 10)
  110. end
  111. local timestamp = tostring(os.time()) .. "000"
  112. local sign = crypto.hmac_sha256(timestamp .. "\n" .. config.DINGTALK_SECRET, config.DINGTALK_SECRET):fromHex():toBase64():urlEncode()
  113. url = url .. "&timestamp=" .. timestamp .. "&sign=" .. sign
  114. end
  115. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  116. local body = { msgtype = "text", text = { content = msg } }
  117. body = json.encode(body)
  118. log.info("util_notify", "POST", url)
  119. local res_code, res_headers, res_body = util_http.fetch(nil, "POST", url, header, body)
  120. -- 处理响应
  121. -- https://open.dingtalk.com/document/orgapp/custom-robots-send-group-messages
  122. if res_code == 200 and res_body and res_body ~= "" then
  123. local res_data = json.decode(res_body)
  124. local res_errcode = res_data.errcode or 0
  125. local res_errmsg = res_data.errmsg or ""
  126. -- 系统繁忙 / 发送速度太快而限流
  127. if res_errcode == -1 or res_errcode == 410100 then
  128. return 500, res_headers, res_body
  129. end
  130. -- timestamp 无效
  131. if res_errcode == 310000 and (string.find(res_errmsg, "timestamp") or string.find(res_errmsg, "过期")) then
  132. socket.sntp()
  133. return 500, res_headers, res_body
  134. end
  135. end
  136. return res_code, res_headers, res_body
  137. end,
  138. -- 发送到 feishu
  139. ["feishu"] = function(msg)
  140. if config.FEISHU_WEBHOOK == nil or config.FEISHU_WEBHOOK == "" then
  141. log.error("util_notify", "未配置 `config.FEISHU_WEBHOOK`")
  142. return
  143. end
  144. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  145. local body = { msg_type = "text", content = { text = msg } }
  146. log.info("util_notify", "POST", config.FEISHU_WEBHOOK)
  147. return util_http.fetch(nil, "POST", config.FEISHU_WEBHOOK, header, json.encode(body))
  148. end,
  149. -- 发送到 wecom
  150. ["wecom"] = function(msg)
  151. if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then
  152. log.error("util_notify", "未配置 `config.WECOM_WEBHOOK`")
  153. return
  154. end
  155. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  156. local body = { msgtype = "text", text = { content = msg } }
  157. log.info("util_notify", "POST", config.WECOM_WEBHOOK)
  158. local res_code, res_headers, res_body = util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json.encode(body))
  159. -- 处理响应
  160. -- https://developer.work.weixin.qq.com/document/path/90313
  161. if res_code == 200 and res_body and res_body ~= "" then
  162. local res_data = json.decode(res_body)
  163. local res_errcode = res_data.errcode or 0
  164. -- 系统繁忙 / 接口调用超过限制
  165. if res_errcode == -1 or res_errcode == 45009 then
  166. return 500, res_headers, res_body
  167. end
  168. end
  169. return res_code, res_headers, res_body
  170. end,
  171. -- 发送到 pushover
  172. ["pushover"] = function(msg)
  173. if config.PUSHOVER_API_TOKEN == nil or config.PUSHOVER_API_TOKEN == "" then
  174. log.error("util_notify", "未配置 `config.PUSHOVER_API_TOKEN`")
  175. return
  176. end
  177. if config.PUSHOVER_USER_KEY == nil or config.PUSHOVER_USER_KEY == "" then
  178. log.error("util_notify", "未配置 `config.PUSHOVER_USER_KEY`")
  179. return
  180. end
  181. local header = { ["Content-Type"] = "application/json; charset=utf-8" }
  182. local body = { token = config.PUSHOVER_API_TOKEN, user = config.PUSHOVER_USER_KEY, message = msg }
  183. local url = "https://api.pushover.net/1/messages.json"
  184. log.info("util_notify", "POST", url)
  185. return util_http.fetch(nil, "POST", url, header, json.encode(body))
  186. end,
  187. -- 发送到 inotify
  188. ["inotify"] = function(msg)
  189. if config.INOTIFY_API == nil or config.INOTIFY_API == "" then
  190. log.error("util_notify", "未配置 `config.INOTIFY_API`")
  191. return
  192. end
  193. if not config.INOTIFY_API:endsWith(".send") then
  194. log.error("util_notify", "`config.INOTIFY_API` 必须以 `.send` 结尾")
  195. return
  196. end
  197. local url = config.INOTIFY_API .. "/" .. string.urlEncode(msg)
  198. log.info("util_notify", "GET", url)
  199. return util_http.fetch(nil, "GET", url)
  200. end,
  201. -- 发送到 next-smtp-proxy
  202. ["next-smtp-proxy"] = function(msg)
  203. if config.NEXT_SMTP_PROXY_API == nil or config.NEXT_SMTP_PROXY_API == "" then
  204. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_API`")
  205. return
  206. end
  207. if config.NEXT_SMTP_PROXY_USER == nil or config.NEXT_SMTP_PROXY_USER == "" then
  208. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_USER`")
  209. return
  210. end
  211. if config.NEXT_SMTP_PROXY_PASSWORD == nil or config.NEXT_SMTP_PROXY_PASSWORD == "" then
  212. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_PASSWORD`")
  213. return
  214. end
  215. if config.NEXT_SMTP_PROXY_HOST == nil or config.NEXT_SMTP_PROXY_HOST == "" then
  216. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_HOST`")
  217. return
  218. end
  219. if config.NEXT_SMTP_PROXY_PORT == nil or config.NEXT_SMTP_PROXY_PORT == "" then
  220. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_PORT`")
  221. return
  222. end
  223. if config.NEXT_SMTP_PROXY_TO_EMAIL == nil or config.NEXT_SMTP_PROXY_TO_EMAIL == "" then
  224. log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_TO_EMAIL`")
  225. return
  226. end
  227. local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
  228. local body = {
  229. user = config.NEXT_SMTP_PROXY_USER,
  230. password = config.NEXT_SMTP_PROXY_PASSWORD,
  231. host = config.NEXT_SMTP_PROXY_HOST,
  232. port = config.NEXT_SMTP_PROXY_PORT,
  233. form_name = config.NEXT_SMTP_PROXY_FORM_NAME,
  234. to_email = config.NEXT_SMTP_PROXY_TO_EMAIL,
  235. subject = config.NEXT_SMTP_PROXY_SUBJECT,
  236. text = msg,
  237. }
  238. log.info("util_notify", "POST", config.NEXT_SMTP_PROXY_API)
  239. return util_http.fetch(nil, "POST", config.NEXT_SMTP_PROXY_API, header, urlencodeTab(body))
  240. end,
  241. ["smtp"] = function(msg)
  242. local smtp_config = {
  243. host = config.SMTP_HOST,
  244. port = config.SMTP_PORT,
  245. username = config.SMTP_USERNAME,
  246. password = config.SMTP_PASSWORD,
  247. mail_from = config.SMTP_MAIL_FROM,
  248. mail_to = config.SMTP_MAIL_TO,
  249. tls_enable = config.SMTP_TLS_ENABLE,
  250. }
  251. local result = lib_smtp.send(msg, config.SMTP_MAIL_SUBJECT, smtp_config)
  252. log.info("util_notify", "SMTP", result.success, result.message, result.is_retry)
  253. if result.success then
  254. return 200, nil, result.message
  255. end
  256. if result.is_retry then
  257. return 500, nil, result.message
  258. end
  259. return 400, nil, result.message
  260. end,
  261. -- 发送到 serial
  262. ["serial"] = function(msg)
  263. uart.write(1, msg)
  264. log.info("util_notify", "serial", "消息已转发到串口")
  265. sys.wait(1000)
  266. return 200
  267. end,
  268. }