util_notify.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 == "" then
  29. log.error("util_notify.notifyToPushDeer", "未配置 `config.PUSHDEER_API`")
  30. return
  31. end
  32. if 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 == "" then
  50. log.error("util_notify.notifyToBark", "未配置 `config.BARK_API`")
  51. return
  52. end
  53. if 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. function util_notify.send(msg)
  89. log.info("util_notify.send", "发送通知", config.NOTIFY_TYPE)
  90. if type(msg) == "table" then
  91. msg = table.concat(msg, "\n")
  92. end
  93. if type(msg) ~= "string" then
  94. log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
  95. return
  96. end
  97. local model = hmeta.model() or ""
  98. local simid = mobile.simid()
  99. local iccid = mobile.iccid(simid) or ""
  100. local rsrp = mobile.rsrp()
  101. local mcc, mnc, band = util_mobile.mcc, util_mobile.mnc, util_mobile.band
  102. local oper = util_mobile.getOper(true)
  103. local lat, lng = util_location.getCoord()
  104. local map_url = "https://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng
  105. msg = msg .. "\n"
  106. if model then
  107. msg = msg .. "\nMODEL: " .. model
  108. end
  109. if iccid then
  110. msg = msg .. "\nICCID: " .. iccid
  111. end
  112. if oper then
  113. msg = msg .. "\n运营商: " .. oper
  114. end
  115. msg = msg .. "\n信号: " .. rsrp .. "dBm"
  116. if band ~= "" then
  117. msg = msg .. "\n频段: B" .. band
  118. end
  119. if lat ~= 0 and lng ~= 0 then
  120. msg = msg .. "\n位置: " .. map_url
  121. end
  122. -- 判断通知类型
  123. local notify
  124. if config.NOTIFY_TYPE == "telegram" then
  125. notify = notifyToTelegram
  126. elseif config.NOTIFY_TYPE == "pushdeer" then
  127. notify = notifyToPushDeer
  128. elseif config.NOTIFY_TYPE == "bark" then
  129. notify = notifyToBark
  130. elseif config.NOTIFY_TYPE == "dingtalk" then
  131. notify = notifyToDingTalk
  132. else
  133. log.error("util_notify.send", "发送通知失败", "未配置 `config.NOTIFY_TYPE`")
  134. return
  135. end
  136. sys.taskInit(
  137. function()
  138. sys.wait(100)
  139. local max_retry = 10
  140. local retry_count = 0
  141. while retry_count < max_retry do
  142. local code, headers, body = notify(msg)
  143. if code == 200 then
  144. log.info("util_notify.send", "发送通知成功", "retry_count:", retry_count, "code:", code, "body:", body)
  145. break
  146. else
  147. retry_count = retry_count + 1
  148. log.error("util_notify.send", "发送通知失败", "retry_count:", retry_count, "code:", code, "body:", body)
  149. util_netled.blink(500, 200, 3000)
  150. sys.wait(10000)
  151. end
  152. end
  153. end
  154. )
  155. end
  156. return util_notify