Browse Source

:sparkles: 更新 Telegram 通知 请求参数以及配置方法, 支持自行反代

Mizore 2 years ago
parent
commit
510cb45a6c
2 changed files with 18 additions and 11 deletions
  1. 2 3
      script/config.lua
  2. 16 8
      script/util_notify.lua

+ 2 - 3
script/config.lua

@@ -3,9 +3,8 @@ return {
     -- NOTIFY_TYPE = {"telegram", "pushdeer", "bark", "dingtalk", "feishu", "wecom", "pushover", "inotify", "next-smtp-proxy", "gotify"},
     NOTIFY_TYPE = "pushdeer",
     --
-    -- telegram 通知配置, https://github.com/0wQ/telegram-notify
-    TELEGRAM_PROXY_API = "",
-    TELEGRAM_TOKEN = "",
+    -- telegram 通知配置, https://github.com/0wQ/telegram-notify 或者自行反代
+    TELEGRAM_API = "https://api.telegram.org/bot{token}/sendMessage",
     TELEGRAM_CHAT_ID = "",
     --
     -- pushdeer 通知配置, https://www.pushdeer.com/

+ 16 - 8
script/util_notify.lua

@@ -16,20 +16,28 @@ end
 local notify = {
     -- 发送到 telegram
     ["telegram"] = function(msg)
-        if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then
-            log.error("util_notify", "未配置 `config.TELEGRAM_PROXY_API`")
+        if config.TELEGRAM_API == nil or config.TELEGRAM_API == "" then
+            log.error("util_notify", "未配置 `config.TELEGRAM_API`")
+            return
+        end
+        if config.TELEGRAM_CHAT_ID == nil or config.TELEGRAM_CHAT_ID == "" then
+            log.error("util_notify", "未配置 `config.TELEGRAM_CHAT_ID`")
             return
         end
 
         local header = {
-            ["content-type"] = "text/plain",
-            ["x-disable-web-page-preview"] = "1",
-            ["x-chat-id"] = config.TELEGRAM_CHAT_ID or "",
-            ["x-token"] = config.TELEGRAM_TOKEN or ""
+            ["content-type"] = "application/json"
+        }
+        local body = {
+            ["chat_id"] = config.TELEGRAM_CHAT_ID,
+            ["disable_web_page_preview"] = true,
+            ["text"] = msg
         }
+        local json_data = json.encode(body)
+        json_data = string.gsub(json_data, "\\b", "\\n")
 
-        log.info("util_notify", "POST", config.TELEGRAM_PROXY_API)
-        return util_http.fetch(nil, "POST", config.TELEGRAM_PROXY_API, header, msg)
+        log.info("util_notify", "POST", config.TELEGRAM_API)
+        return util_http.fetch(nil, "POST", config.TELEGRAM_API, header, json_data)
     end,
     -- 发送到 gotify
     ["gotify"] = function(msg)