util_notify_channel.lua 13 KB

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