Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加自建tg Api配置 #472

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Ray.BiliBiliTool.Console/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TelegramApiClient : PushService
/// <param name="botToken">The Telegram bot token.</param>
/// <param name="timeoutSeconds">The timeout seconds.</param>
/// <exception cref="ArgumentException">Thrown if the bot token is null or empty.</exception>
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))
{
Expand All @@ -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())
{
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogEvent> predicate,
bool sendBatchesAsOneMessages,
IFormatProvider formatProvider,
Expand All @@ -29,6 +31,7 @@ LogEventLevel minimumLogEventLevel
_botToken = botToken;
_chatId = chatId;
_proxy = proxy;
_apiHost = apiHost;
}

public override void Emit(LogEvent logEvent)
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,7 +23,7 @@ public static LoggerConfiguration TelegramBatched(
if (containsTrigger.IsNullOrEmpty()) containsTrigger = Constants.DefaultContainsTrigger;
Predicate<LogEvent> 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);
}
}
}