Refactor Telegram API and enhance TelegramPanel form handling
Updated the TelegramApi class to improve URL construction for API calls by introducing a new MethodUrl method, ensuring proper handling of bot tokens. Refactored the TelegramPanel component to use a form for input fields, allowing for better user experience and submission handling. Adjusted password input fields to use 'new-password' for improved security. These changes aim to enhance the functionality and usability of the Telegram integration.
This commit is contained in:
@@ -185,11 +185,10 @@ public sealed class TelegramApi(ILogger<TelegramApi> logger) : ITelegramApi
|
||||
using var handler = CreateHandler(settings);
|
||||
using var client = new HttpClient(handler)
|
||||
{
|
||||
BaseAddress = new Uri("https://api.telegram.org/"),
|
||||
Timeout = timeout ?? TimeSpan.FromSeconds(30),
|
||||
};
|
||||
|
||||
var url = $"bot{settings.BotToken}/{method}";
|
||||
var url = MethodUrl(settings.BotToken, method);
|
||||
using var response = payload is null
|
||||
? await client.PostAsync(url, null, cancellationToken)
|
||||
: await client.PostAsJsonAsync(url, payload, Json, cancellationToken);
|
||||
@@ -205,6 +204,14 @@ public sealed class TelegramApi(ILogger<TelegramApi> logger) : ITelegramApi
|
||||
return await response.Content.ReadFromJsonAsync<T>(Json, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Адрес метода — абсолютным, а не относительным к BaseAddress. Токен содержит двоеточие
|
||||
/// («8613942817:AAH…»), и как относительный путь «bot8613942817:AAH…/getUpdates» разбирается
|
||||
/// в схему «bot8613942817»: запрос падает с «scheme is not supported», не дойдя до сети.
|
||||
/// </summary>
|
||||
public static Uri MethodUrl(string token, string method) =>
|
||||
new($"https://api.telegram.org/bot{token}/{method}");
|
||||
|
||||
/// <summary>
|
||||
/// Транспорт с прокси, если он задан. SOCKS5 поддерживает сам <see cref="WebProxy"/> начиная
|
||||
/// с .NET 6 — достаточно схемы в адресе, отдельной библиотеки не нужно.
|
||||
|
||||
Reference in New Issue
Block a user