Логирование ошибок отправки оповещений в телеграм.
test / deploy_trader_prod (push) Successful in 3m0s Details

dev
vlad zverzhkhovskiy 2025-09-23 11:05:53 +03:00
parent 196e0246d7
commit dbacad168a
1 changed files with 13 additions and 2 deletions

View File

@ -1,6 +1,7 @@
using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Interfaces;
using KLHZ.Trader.Core.Contracts.Messaging.Interfaces;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Immutable;
using System.Threading.Channels;
@ -15,9 +16,11 @@ namespace KLHZ.Trader.Core.TG.Services
private readonly IUpdateHandler _updateHandler;
private readonly Channel<IMessage> _messages = Channel.CreateUnbounded<IMessage>();
private readonly ImmutableArray<long> _admins = [];
private readonly ILogger<BotStarter> _logger;
public BotStarter(IOptions<TgBotConfig> cfg, IUpdateHandler updateHandler, IDataBus dataBus, IOptions<TgBotConfig> options)
public BotStarter(IOptions<TgBotConfig> cfg, IUpdateHandler updateHandler, IDataBus dataBus, IOptions<TgBotConfig> options, ILogger<BotStarter> logger)
{
_logger = logger;
_botClient = new TelegramBotClient(cfg.Value.Token);
_updateHandler = updateHandler;
dataBus.AddChannel(string.Empty, _messages);
@ -32,7 +35,15 @@ namespace KLHZ.Trader.Core.TG.Services
var message = await _messages.Reader.ReadAsync();
foreach (var admin in _admins)
{
await _botClient.SendMessage(admin, message.Text);
try
{
await _botClient.SendMessage(admin, message.Text);
}
catch(Exception ex)
{
_logger.LogError(ex, "Ошибка при отправке сообщения в бота.");
}
}
}
}