From b71384511e5fafb46d97dcc2a3fabadc841a098d Mon Sep 17 00:00:00 2001
From: JustTestCode <65500757+JustTestCode@users.noreply.github.com>
Date: Fri, 3 Mar 2023 22:56:57 +0800
Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=BB=BAtg=20Ap?=
=?UTF-8?q?i=E9=85=8D=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加自建tg Api配置
---
.../TelegramApiClient.cs | 12 ++++++++----
.../TelegramBatchedSink.cs | 5 ++++-
.../TelegramLoggerConfigurationExtensions.cs | 3 ++-
3 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs
index 3004c40ef..cb42e20e4 100644
--- a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs
+++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs
@@ -32,7 +32,7 @@ public class TelegramApiClient : PushService
/// The Telegram bot token.
/// The timeout seconds.
/// Thrown if the bot token is null or empty.
- public TelegramApiClient(string botToken, string chatId, string proxy="", int timeoutSeconds = 10)
+ public TelegramApiClient(string botToken, string chatId, string proxy = "", string apiHost = "", int timeoutSeconds = 10)
{
if (string.IsNullOrWhiteSpace(botToken))
{
@@ -42,8 +42,12 @@ public TelegramApiClient(string botToken, string chatId, string proxy="", int ti
_chatId = chatId;
_proxy = proxy;
-
- this._apiUrl = new Uri($"{TelegramBotApiUrl}{botToken}/sendMessage");
+ var botApiUrl = TelegramBotApiUrl;
+ if (!string.IsNullOrWhiteSpace(apiHost))
+ {
+ botApiUrl = apiHost
+ }
+ this._apiUrl = new Uri($"{botApiUrl}{botToken}/sendMessage");
if (proxy.IsNotNullOrEmpty())
{
@@ -101,7 +105,7 @@ private WebProxy GetWebProxy(string proxyAddress)
var credentials = new NetworkCredential(proxyUser, proxyPass);
- webProxy = new WebProxy(address, true,null, credentials);
+ webProxy = new WebProxy(address, true, null, credentials);
}
else
{
diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramBatchedSink.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramBatchedSink.cs
index 5ac1455aa..4fa7d2b2e 100644
--- a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramBatchedSink.cs
+++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramBatchedSink.cs
@@ -15,11 +15,13 @@ public class TelegramBatchedSink : BatchedSink
private readonly string _botToken;
private readonly string _chatId;
private readonly string _proxy;
+ private readonly string _apiHost;
public TelegramBatchedSink(
string botToken,
string chatId,
string proxy,
+ string apiHost,
Predicate predicate,
bool sendBatchesAsOneMessages,
IFormatProvider formatProvider,
@@ -29,6 +31,7 @@ LogEventLevel minimumLogEventLevel
_botToken = botToken;
_chatId = chatId;
_proxy = proxy;
+ _apiHost = apiHost;
}
public override void Emit(LogEvent logEvent)
@@ -37,7 +40,7 @@ public override void Emit(LogEvent logEvent)
base.Emit(logEvent);
}
- protected override PushService PushService => new TelegramApiClient(_botToken, _chatId, _proxy, 5);
+ protected override PushService PushService => new TelegramApiClient(_botToken, _chatId, _proxy, _apiHost, 5);
public override void Dispose()
{
diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramLoggerConfigurationExtensions.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramLoggerConfigurationExtensions.cs
index ab9ebb466..9b4ca8d51 100644
--- a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramLoggerConfigurationExtensions.cs
+++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramLoggerConfigurationExtensions.cs
@@ -13,6 +13,7 @@ public static LoggerConfiguration TelegramBatched(
string botToken,
string chatId,
string proxy,
+ string apiHost,
string containsTrigger = Constants.DefaultContainsTrigger,
bool sendBatchesAsOneMessages = true,
IFormatProvider formatProvider = null,
@@ -22,7 +23,7 @@ public static LoggerConfiguration TelegramBatched(
if (containsTrigger.IsNullOrEmpty()) containsTrigger = Constants.DefaultContainsTrigger;
Predicate predicate = x => x.MessageTemplate.Text.Contains(containsTrigger);
- return loggerSinkConfiguration.Sink(new TelegramBatchedSink(botToken, chatId, proxy, predicate, sendBatchesAsOneMessages, formatProvider, restrictedToMinimumLevel), restrictedToMinimumLevel);
+ return loggerSinkConfiguration.Sink(new TelegramBatchedSink(botToken, chatId, proxy, apiHost, predicate, sendBatchesAsOneMessages, formatProvider, restrictedToMinimumLevel), restrictedToMinimumLevel);
}
}
}
From ef6c5b87b619d38909ee3cbd1290736e149aa6be Mon Sep 17 00:00:00 2001
From: JustTestCode <65500757+JustTestCode@users.noreply.github.com>
Date: Sun, 5 Mar 2023 23:23:02 +0800
Subject: [PATCH 2/2] fix
fix
---
src/Ray.BiliBiliTool.Console/appsettings.json | 3 ++-
.../Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/Ray.BiliBiliTool.Console/appsettings.json b/src/Ray.BiliBiliTool.Console/appsettings.json
index 48061223e..90adac573 100644
--- a/src/Ray.BiliBiliTool.Console/appsettings.json
+++ b/src/Ray.BiliBiliTool.Console/appsettings.json
@@ -113,7 +113,8 @@
"botToken": "",
"chatId": "",
"restrictedToMinimumLevel": "Information",
- "proxy": "" //代理,user:password@host:port
+ "proxy": "", //代理,user:password@host:port
+ "apiHost": ""
}
},
//4.企业微信机器人(https://work.weixin.qq.com/api/doc/90000/90136/91770)
diff --git a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs
index cb42e20e4..735475dc6 100644
--- a/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs
+++ b/src/Ray.Serilog.Sinks/Ray.Serilog.Sinks.TelegramBatched/TelegramApiClient.cs
@@ -45,7 +45,7 @@ public TelegramApiClient(string botToken, string chatId, string proxy = "", stri
var botApiUrl = TelegramBotApiUrl;
if (!string.IsNullOrWhiteSpace(apiHost))
{
- botApiUrl = apiHost
+ botApiUrl = apiHost;
}
this._apiUrl = new Uri($"{botApiUrl}{botToken}/sendMessage");