Przeglądaj źródła

自定义电报地址

luolongfei 3 lat temu
rodzic
commit
195caac9b3
4 zmienionych plików z 37 dodań i 21 usunięć
  1. 14 14
      .env.example
  2. 2 2
      config.php
  3. 1 1
      libs/Lang.php
  4. 20 4
      libs/MessageServices/TelegramBot.php

+ 14 - 14
.env.example

@@ -12,7 +12,7 @@
 #####################################################################
 
 # .env 文件版本
-ENV_FILE_VERSION='v2.2'
+ENV_FILE_VERSION='v2.3'
 
 ######################  账户配置 Account config  #########################
 # Freenom 账户 Freenom Account
@@ -24,7 +24,7 @@ FREENOM_PASSWORD=''
 # 多账户支持 Support for multiple accounts
 MULTIPLE_ACCOUNTS=''
 
-# Freenom 代理 e.g. http://127.0.0.1:1081 or socks5://127.0.0.1:1080
+# 【可选】 Freenom 代理 e.g. http://127.0.0.1:1081 or socks5://127.0.0.1:1080
 FREENOM_PROXY=''
 ######################  end 账户配置  #########################
 
@@ -41,18 +41,18 @@ TO=''
 # 是否启用邮件推送功能 1:启用 0:不启用 Whether to enable email push features 1: enabled 0: not enabled
 MAIL_ENABLE=0
 
-# 自定义邮箱配置 Custom email config
+# 【可选】 自定义邮箱配置 Custom email config
 # 如果你想使用除 “QQ邮箱、163邮箱、Gmail、Outlook邮箱” 外的第三方邮箱或者自建邮箱服务作为机器人邮箱,可以自定义邮箱配置,否则请不要配置这些项
 # If you want to use third-party mailboxes or self-built mailbox services other than "QQ mailbox, 163 mailbox, Gmail,
 # Outlook mailbox" as robot mailbox, you can customize mailbox configuration, otherwise, please don't configure these items.
 
-# SMTP 服务器 SMTP server e.g. smtp.gmail.com
+# 【可选】 SMTP 服务器 SMTP server e.g. smtp.gmail.com
 MAIL_HOST=''
 
-# SMTP 端口号,一般是 587 或者 465 SMTP port
+# 【可选】 SMTP 端口号,一般是 587 或者 465 SMTP port
 MAIL_PORT=''
 
-# 邮件加密方式,一般是 tls 或者 ssl 或者 starttls Email encryption method
+# 【可选】 邮件加密方式,一般是 tls 或者 ssl 或者 starttls Email encryption method
 MAIL_ENCRYPTION=''
 
 # end 自定义邮箱配置 end of custom email config
@@ -60,21 +60,21 @@ MAIL_ENCRYPTION=''
 ######################  end 通知邮件配置  #########################
 
 ######################  Telegram bot  #########################
-# 可选配置,通过 Telegram bot 发送通知消息 This is an optional configuration to send notification messages via Telegram bot
+# 通过 Telegram bot 发送通知消息 Send messages via Telegram bot
 
-# 你的chat_id,通过发送“/start”给@userinfobot可以获取自己的id Your chat_id, you can get your own id by sending "/start" to @userinfobot
+# 你的 chat_id,通过发送“/start”给 @userinfobot 可以获取自己的 id Your chat_id, you can get your own id by sending "/start" to @userinfobot
 TELEGRAM_CHAT_ID=''
 
-# 你的Telegram bot的token Token for your Telegram bot
+# 你的 Telegram bot  token Token for your Telegram bot
 TELEGRAM_BOT_TOKEN=''
 
-# Telegram 代理 e.g. http://127.0.0.1:1081 or socks5://127.0.0.1:1080
+# 【可选】 Telegram 代理 e.g. http://127.0.0.1:1081 or socks5://127.0.0.1:1080
 TELEGRAM_PROXY=''
 
-## Telegram api自建反向代理地址(选填)
-## 教程:https://www.hostloc.com/thread-805441-1-1.html
-## 如反向代理地址 http://aaa.bbb.ccc 则填写 aaa.bbb.ccc
-TELEGRAM_API_HOST=''
+# 【可选】 自建 Telegram 反向代理地址,不明白则请忽略此配置项,保持默认即可
+# 参考:https://web.archive.org/web/20220221035315/https://hostloc.com/thread-805441-1-1.html
+# 只要是一个合法的网址即可,程式会自动提取网址的主机部分。例如 https://xxx.xx.workers.dev/ or xxx.xx.xx
+CUSTOM_TELEGRAM_HOST='api.telegram.org'
 
 # 是否启用 Telegram Bot 功能 1:启用 0:不启用 Whether to enable Telegram Bot features 1: enabled 0: not enabled
 TELEGRAM_BOT_ENABLE=0

+ 2 - 2
config.php

@@ -42,8 +42,8 @@ return [
             'not_enabled_tips' => env('TELEGRAM_CHAT_ID') && env('TELEGRAM_BOT_TOKEN'), // 提醒未启用
             'class' => \Luolongfei\Libs\MessageServices\TelegramBot::class,
             'name' => lang('100065'),
-            'proxy' => env('TELEGRAM_PROXY'),
-            'host' => env('TELEGRAM_API_HOST'),
+            'proxy' => env('TELEGRAM_PROXY') ?: null,
+            'host' => env('CUSTOM_TELEGRAM_HOST') ?: 'api.telegram.org',
         ],
 
         /**

+ 1 - 1
libs/Lang.php

@@ -18,7 +18,7 @@ class Lang extends Base
 
     public function init()
     {
-        // 读取语言包,位于 resources/lang/ 目录下
+        // 读取语言包,语言包位于 resources/lang/ 目录下
         $this->lang = require sprintf('%s/lang/%s.php', RESOURCES_PATH, env('LANGUAGE', 'zh'));
     }
 

+ 20 - 4
libs/MessageServices/TelegramBot.php

@@ -28,7 +28,7 @@ class TelegramBot extends MessageGateway
     protected $token;
 
     /**
-     * @var host TELEGRAM_API_HOST
+     * @var string Telegram 主机地址
      */
     protected $host;
 
@@ -41,7 +41,7 @@ class TelegramBot extends MessageGateway
     {
         $this->chatID = config('message.telegram.chat_id');
         $this->token = config('message.telegram.token');
-        $this->host = config('message.telegram.host') ?: 'api.telegram.org';
+        $this->host = $this->getTelegramHost();
 
         $this->client = new Client([
             'headers' => [
@@ -51,10 +51,26 @@ class TelegramBot extends MessageGateway
             'timeout' => self::TIMEOUT,
             'verify' => config('verify_ssl'),
             'debug' => config('debug'),
-            'proxy' => config('message.telegram.proxy') ?: null,
+            'proxy' => config('message.telegram.proxy'),
         ]);
     }
 
+    /**
+     * 获取 Telegram 主机地址
+     *
+     * @return string
+     */
+    private function getTelegramHost()
+    {
+        $host = (string)config('message.telegram.host');
+
+        if (preg_match('/^(?:https?:\/\/)?(?P<host>[^\/?\n]+)/iu', $host, $m)) {
+            return $m['host'];
+        }
+
+        return 'api.telegram.org';
+    }
+
     /**
      * 生成域名状态 MarkDown 完整文本
      *
@@ -281,7 +297,7 @@ class TelegramBot extends MessageGateway
                 sprintf('https://%s/bot%s/sendMessage', $this->host, $this->token),
                 [
                     'form_params' => [
-                        'chat_id' => $recipient ? $recipient : $this->chatID,
+                        'chat_id' => $recipient ?: $this->chatID,
                         'text' => $content, // Text of the message to be sent, 1-4096 characters after entities parsing
                         'parse_mode' => $isMarkdown ? 'MarkdownV2' : 'HTML',
                         'disable_web_page_preview' => true,