куфсешщт
This commit is contained in:
@@ -24,6 +24,7 @@ public sealed class ForwardWorker : BackgroundService
|
|||||||
private readonly ConcurrentDictionary<(long ChatId, string MediaGroupId), AlbumBuffer> _albumBuffers = new();
|
private readonly ConcurrentDictionary<(long ChatId, string MediaGroupId), AlbumBuffer> _albumBuffers = new();
|
||||||
|
|
||||||
private CancellationToken _appStopping;
|
private CancellationToken _appStopping;
|
||||||
|
private TelegramBotClient _bot;
|
||||||
|
|
||||||
public ForwardWorker(
|
public ForwardWorker(
|
||||||
IOptions<TelegramOptions> telegram,
|
IOptions<TelegramOptions> telegram,
|
||||||
@@ -57,7 +58,7 @@ public sealed class ForwardWorker : BackgroundService
|
|||||||
_logger.LogInformation("Init database");
|
_logger.LogInformation("Init database");
|
||||||
await _repo.InitializeAsync(stoppingToken);
|
await _repo.InitializeAsync(stoppingToken);
|
||||||
|
|
||||||
var bot = new TelegramBotClient(_telegram.BotToken);
|
_bot = new TelegramBotClient(_telegram.BotToken);
|
||||||
var receiverOptions = new ReceiverOptions
|
var receiverOptions = new ReceiverOptions
|
||||||
{
|
{
|
||||||
AllowedUpdates = new[]
|
AllowedUpdates = new[]
|
||||||
@@ -69,13 +70,13 @@ public sealed class ForwardWorker : BackgroundService
|
|||||||
|
|
||||||
_logger.LogInformation("Start Telegram polling for channel {ChannelId}", _telegram.ChannelId);
|
_logger.LogInformation("Start Telegram polling for channel {ChannelId}", _telegram.ChannelId);
|
||||||
|
|
||||||
bot.StartReceiving(
|
_bot.StartReceiving(
|
||||||
async (client, update, ct) =>
|
async (client, update, ct) =>
|
||||||
{
|
{
|
||||||
await _updateGate.WaitAsync(ct);
|
await _updateGate.WaitAsync(ct);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await HandleUpdateAsync(update, bot, ct);
|
await HandleUpdateAsync(update, _bot, ct);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -288,54 +289,20 @@ public sealed class ForwardWorker : BackgroundService
|
|||||||
|
|
||||||
private async Task TryAddTelegramReactionAfterRepostAsync(long chatId, int messageId, CancellationToken ct)
|
private async Task TryAddTelegramReactionAfterRepostAsync(long chatId, int messageId, CancellationToken ct)
|
||||||
{
|
{
|
||||||
if (!_telegram.AddReactionAfterRepost)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var emoji = _telegram.RepostReactionEmoji;
|
|
||||||
if (string.IsNullOrWhiteSpace(emoji))
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Делаем прямой вызов Bot API, чтобы не зависеть от наличия метода в конкретной версии Telegram.Bot.
|
|
||||||
// https://core.telegram.org/bots/api#setmessagereaction
|
|
||||||
var url = $"https://api.telegram.org/bot{_telegram.BotToken}/setMessageReaction";
|
|
||||||
|
|
||||||
var payload = new
|
|
||||||
{
|
|
||||||
chat_id = chatId,
|
|
||||||
message_id = messageId,
|
|
||||||
reaction = new object[]
|
|
||||||
{
|
|
||||||
new { type = "emoji", emoji }
|
|
||||||
},
|
|
||||||
is_big = _telegram.RepostReactionIsBig
|
|
||||||
};
|
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(payload);
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var http = _httpFactory.CreateClient(TelegramBotApiHttpClientName);
|
_logger.LogWarning($"Начинаю простановку реакции.");
|
||||||
using var resp = await http.PostAsync(url, content, ct).ConfigureAwait(false);
|
await _bot.SetMessageReaction(chatId, messageId, [new ReactionTypeEmoji() { Emoji = "🦄" }], cancellationToken: ct);
|
||||||
var body = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
_logger.LogWarning($"Реация успешно проставлена.");
|
||||||
|
|
||||||
if (!resp.IsSuccessStatusCode)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
catch (OperationCanceledException ex) when (ct.IsCancellationRequested)
|
||||||
using var doc = JsonDocument.Parse(body);
|
|
||||||
if (!doc.RootElement.TryGetProperty("ok", out var ok) || ok.ValueKind != JsonValueKind.True)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
|
||||||
{
|
{
|
||||||
|
_logger.LogWarning($"Ошибка проставноки реакции: {ex.Message}{ex.StackTrace}");
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
_logger.LogWarning($"Ошибка проставноки реакции: {ex.Message}{ex.StackTrace}");
|
||||||
// intentionally no logging (do not spam logs for tg calls)
|
// intentionally no logging (do not spam logs for tg calls)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user