util_notify.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. function util_notify.send(msg)
  131. log.info("util_notify.send", "发送通知", config.NOTIFY_TYPE)
  132. if type(msg) == "table" then
  133. msg = table.concat(msg, "\n")
  134. end
  135. if type(msg) ~= "string" then
  136. log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
  137. return
  138. end
  139. local model = hmeta.model() or ""
  140. local simid = mobile.simid()
  141. local iccid = mobile.iccid(simid) or ""
  142. local rsrp = mobile.rsrp()
  143. local mcc, mnc, band = util_mobile.mcc, util_mobile.mnc, util_mobile.band
  144. local oper = util_mobile.getOper(true)
  145. local lat, lng = util_location.getCoord()
  146. local map_url = "https://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng
  147. msg = msg .. "\n"
  148. if model then
  149. msg = msg .. "\nMODEL: " .. model
  150. end
  151. if iccid then
  152. msg = msg .. "\nICCID: " .. iccid
  153. end
  154. if oper then
  155. msg = msg .. "\n运营商: " .. oper
  156. end
  157. msg = msg .. "\n信号: " .. rsrp .. "dBm"
  158. if band ~= "" then
  159. msg = msg .. "\n频段: B" .. band
  160. end
  161. if lat ~= 0 and lng ~= 0 then
  162. msg = msg .. "\n位置: " .. map_url
  163. end
  164. -- 判断通知类型
  165. local notify
  166. if config.NOTIFY_TYPE == "telegram" then
  167. notify = notifyToTelegram
  168. elseif config.NOTIFY_TYPE == "pushdeer" then
  169. notify = notifyToPushDeer
  170. elseif config.NOTIFY_TYPE == "bark" then
  171. notify = notifyToBark
  172. elseif config.NOTIFY_TYPE == "dingtalk" then
  173. notify = notifyToDingTalk
  174. elseif config.NOTIFY_TYPE == "feishu" then
  175. notify = notifyToFeishu
  176. elseif config.NOTIFY_TYPE == "wecom" then
  177. notify = notifyToWeCom
  178. else
  179. log.error("util_notify.send", "发送通知失败", "未配置 `config.NOTIFY_TYPE`")
  180. return
  181. end
  182. sys.taskInit(
  183. function()
  184. sys.wait(100)
  185. local max_retry = 10
  186. local retry_count = 0
  187. while retry_count < max_retry do
  188. local code, headers, body = notify(msg)
  189. if code == 200 then
  190. log.info("util_notify.send", "发送通知成功", "retry_count:", retry_count, "code:", code, "body:", body)
  191. break
  192. else
  193. retry_count = retry_count + 1
  194. log.error("util_notify.send", "发送通知失败", "retry_count:", retry_count, "code:", code, "body:", body)
  195. util_netled.blink(500, 200, 3000)
  196. sys.wait(10000)
  197. end
  198. end
  199. end
  200. )
  201. end
  202. return util_notify