From f66c9a2aab2e4ce9b46c100cdfd932aa48fb373b Mon Sep 17 00:00:00 2001
From: DR-lin-eng <52230594+DR-lin-eng@users.noreply.github.com>
Date: Sun, 16 Mar 2025 03:01:53 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=8F=98=E9=87=8F=E6=AD=A3?=
=?UTF-8?q?=E7=A1=AE=E6=80=A7=EF=BC=8C=E4=BF=AE=E6=AD=A3api=E7=AB=AF?=
=?UTF-8?q?=E7=82=B9=E5=8F=8D=E4=BB=A3=E4=BD=BF=E7=94=A8=20(#1314)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: DR-lin-eng <@DR-lin-eng>
Co-authored-by: DR-lin-eng <@DR-lin-eng>
---
.../Service/Notifier/TelegramNotifier.cs | 38 ++++++++++++++-----
1 file changed, 29 insertions(+), 9 deletions(-)
diff --git a/BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs b/BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs
index 21bc68cb..a014e420 100644
--- a/BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs
+++ b/BetterGenshinImpact/Service/Notifier/TelegramNotifier.cs
@@ -12,8 +12,8 @@ namespace BetterGenshinImpact.Service.Notifier;
public class TelegramNotifier : INotifier, IDisposable
{
- // 永远不更改此URL常量,这是Telegram API的标准URL前缀
- private const string TELEGRAM_API_URL = "https://api.telegram.org/bot";
+ // 默认Telegram API的标准URL前缀
+ private const string DEFAULT_TELEGRAM_API_URL = "https://api.telegram.org/bot";
private readonly bool _createdHttpClient;
private readonly HttpClient _httpClient;
@@ -28,7 +28,7 @@ public class TelegramNotifier : INotifier, IDisposable
/// 可选的HttpClient,如果不提供则创建新的
/// Telegram机器人Token
/// Telegram聊天ID
- /// 不再使用,保留参数仅为兼容性
+ /// 自定义Telegram API基础URL(可以只填写域名,如"xxx.xxx.xxx"),为空则使用默认URL
public TelegramNotifier(HttpClient httpClient = null, string telegramBotToken = "", string telegramChatId = "",
string telegramApiBaseUrl = "")
{
@@ -47,8 +47,28 @@ public class TelegramNotifier : INotifier, IDisposable
_httpClient.Timeout = TimeSpan.FromSeconds(30);
}
- // 忽略自定义API URL,始终使用标准Telegram API URL
- TelegramApiBaseUrl = TELEGRAM_API_URL;
+ // 使用自定义API URL,如果为空则使用默认Telegram API URL
+ if (string.IsNullOrEmpty(telegramApiBaseUrl))
+ {
+ TelegramApiBaseUrl = DEFAULT_TELEGRAM_API_URL;
+ }
+ else
+ {
+ // 格式化用户提供的API URL
+ var formattedUrl = telegramApiBaseUrl.Trim();
+
+ // 添加协议前缀(如果没有)
+ if (!formattedUrl.StartsWith("http://") && !formattedUrl.StartsWith("https://"))
+ formattedUrl = "https://" + formattedUrl;
+
+ // 确保URL以斜杠结尾
+ if (!formattedUrl.EndsWith("/")) formattedUrl += "/";
+
+ // 添加bot路径(如果需要)
+ if (!formattedUrl.EndsWith("/bot")) formattedUrl += "bot";
+
+ TelegramApiBaseUrl = formattedUrl;
+ }
}
///
@@ -62,9 +82,9 @@ public class TelegramNotifier : INotifier, IDisposable
public string TelegramChatId { get; set; }
///
- /// Telegram API基础URL - 内部使用
+ /// Telegram API基础URL
///
- private string TelegramApiBaseUrl { get; set; }
+ public string TelegramApiBaseUrl { get; set; }
public void Dispose()
{
@@ -125,8 +145,8 @@ public class TelegramNotifier : INotifier, IDisposable
private async Task SendTextMessageAsync(string message)
{
- // 构建Telegram API URL - 固定格式:https://api.telegram.org/bot{token}/sendMessage
- var endpoint = $"{TELEGRAM_API_URL}{TelegramBotToken}/sendMessage";
+ // 构建Telegram API URL - 使用自定义或默认API基础URL
+ var endpoint = $"{TelegramApiBaseUrl}{TelegramBotToken}/sendMessage";
try
{