util_notify.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. function util_notify.send(msg)
  110. log.info("util_notify.send", "发送通知", config.NOTIFY_TYPE)
  111. if type(msg) == "table" then
  112. msg = table.concat(msg, "\n")
  113. end
  114. if type(msg) ~= "string" then
  115. log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
  116. return
  117. end
  118. local model = hmeta.model() or ""
  119. local simid = mobile.simid()
  120. local iccid = mobile.iccid(simid) or ""
  121. local rsrp = mobile.rsrp()
  122. local mcc, mnc, band = util_mobile.mcc, util_mobile.mnc, util_mobile.band
  123. local oper = util_mobile.getOper(true)
  124. local lat, lng = util_location.getCoord()
  125. local map_url = "https://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng
  126. msg = msg .. "\n"
  127. if model then
  128. msg = msg .. "\nMODEL: " .. model
  129. end
  130. if iccid then
  131. msg = msg .. "\nICCID: " .. iccid
  132. end
  133. if oper then
  134. msg = msg .. "\n运营商: " .. oper
  135. end
  136. msg = msg .. "\n信号: " .. rsrp .. "dBm"
  137. if band ~= "" then
  138. msg = msg .. "\n频段: B" .. band
  139. end
  140. if lat ~= 0 and lng ~= 0 then
  141. msg = msg .. "\n位置: " .. map_url
  142. end
  143. -- 判断通知类型
  144. local notify
  145. if config.NOTIFY_TYPE == "telegram" then
  146. notify = notifyToTelegram
  147. elseif config.NOTIFY_TYPE == "pushdeer" then
  148. notify = notifyToPushDeer
  149. elseif config.NOTIFY_TYPE == "bark" then
  150. notify = notifyToBark
  151. elseif config.NOTIFY_TYPE == "dingtalk" then
  152. notify = notifyToDingTalk
  153. elseif config.NOTIFY_TYPE == "feishu" then
  154. notify = notifyToFeishu
  155. else
  156. log.error("util_notify.send", "发送通知失败", "未配置 `config.NOTIFY_TYPE`")
  157. return
  158. end
  159. sys.taskInit(
  160. function()
  161. sys.wait(100)
  162. local max_retry = 10
  163. local retry_count = 0
  164. while retry_count < max_retry do
  165. local code, headers, body = notify(msg)
  166. if code == 200 then
  167. log.info("util_notify.send", "发送通知成功", "retry_count:", retry_count, "code:", code, "body:", body)
  168. break
  169. else
  170. retry_count = retry_count + 1
  171. log.error("util_notify.send", "发送通知失败", "retry_count:", retry_count, "code:", code, "body:", body)
  172. util_netled.blink(500, 200, 3000)
  173. sys.wait(10000)
  174. end
  175. end
  176. end
  177. )
  178. end
  179. return util_notify