util_notify.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, httpCallback)
  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, httpCallback)
  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. function util_notify.send(msg)
  68. log.info("util_notify.send", "发送通知", config.NOTIFY_TYPE)
  69. if type(msg) == "table" then
  70. msg = table.concat(msg, "\n")
  71. end
  72. if type(msg) ~= "string" then
  73. log.error("util_notify.send", "发送通知失败", "参数类型错误", type(msg))
  74. return
  75. end
  76. local model = hmeta.model() or ""
  77. local simid = mobile.simid()
  78. local iccid = mobile.iccid(simid) or ""
  79. local rsrp = mobile.rsrp()
  80. local mcc, mnc, band = util_mobile.mcc, util_mobile.mnc, util_mobile.band
  81. local oper = util_mobile.getOper(true)
  82. local lat, lng = util_location.getCoord()
  83. local map_url = "https://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng
  84. msg = msg .. "\n"
  85. if model then
  86. msg = msg .. "\nMODEL: " .. model
  87. end
  88. if iccid then
  89. msg = msg .. "\nICCID: " .. iccid
  90. end
  91. if oper then
  92. msg = msg .. "\n运营商: " .. oper
  93. end
  94. msg = msg .. "\n信号: " .. rsrp .. "dBm"
  95. if band ~= "" then
  96. msg = msg .. "\n频段: B" .. band
  97. end
  98. if lat ~= 0 and lng ~= 0 then
  99. msg = msg .. "\n位置: " .. map_url
  100. end
  101. -- 判断通知类型
  102. local notify
  103. if config.NOTIFY_TYPE == "telegram" then
  104. notify = notifyToTelegram
  105. elseif config.NOTIFY_TYPE == "pushdeer" then
  106. notify = notifyToPushDeer
  107. elseif config.NOTIFY_TYPE == "bark" then
  108. notify = notifyToBark
  109. else
  110. log.error("util_notify.send", "发送通知失败", "未配置 `config.NOTIFY_TYPE`")
  111. return
  112. end
  113. sys.taskInit(
  114. function()
  115. sys.wait(100)
  116. local max_retry = 10
  117. local retry_count = 0
  118. while retry_count < max_retry do
  119. local code, headers, body = notify(msg)
  120. if code == 200 then
  121. log.info("util_notify.send", "发送通知成功", "retry_count:", retry_count)
  122. break
  123. else
  124. retry_count = retry_count + 1
  125. log.error("util_notify.send", "发送通知失败", "retry_count:", retry_count, "code:", code, "body:", body)
  126. util_netled.blink(500, 200, 3000)
  127. sys.wait(10000)
  128. end
  129. end
  130. end
  131. )
  132. end
  133. return util_notify