using KLHZ.Trader.Core.Common; using KLHZ.Trader.Core.Contracts.Messaging.Dtos; using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Enums; using KLHZ.Trader.Core.Contracts.Messaging.Interfaces; using KLHZ.Trader.Core.Exchange.Services; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System.Collections.Immutable; using Telegram.Bot; using Telegram.Bot.Polling; using Telegram.Bot.Types; using Telegram.Bot.Types.ReplyMarkups; namespace KLHZ.Trader.Core.TG.Services { public class BotMessagesHandler : IUpdateHandler { private readonly ImmutableArray _admins = []; private readonly IDataBus _eventBus; private readonly ILogger _logger; private readonly TraderDataProvider _traderDataProvider; public BotMessagesHandler(IDataBus eventBus, IOptions options, ILogger logger, TraderDataProvider traderDataProvider) { _traderDataProvider = traderDataProvider; _logger = logger; _eventBus = eventBus; _admins = ImmutableArray.CreateRange(options.Value.Admins); } public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken) { return Task.CompletedTask; } public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { try { if (update.Message != null && update.Message?.From != null && _admins.Contains(update.Message.From.Id)) { switch (update.Message.Text) { case "/start": { var replyKeyboardMarkup = new ReplyKeyboardMarkup(new[] { new KeyboardButton[] { Constants.BotCommandsButtons.EnableSelling, Constants.BotCommandsButtons.DisableSelling}, new KeyboardButton[] { Constants.BotCommandsButtons.EnablePurchases, Constants.BotCommandsButtons.DisablePurchases}}); await botClient.SendMessage(update.Message.Chat, "Принято!", replyMarkup: replyKeyboardMarkup); break; } case Constants.BotCommandsButtons.EnableSelling: { BotModeSwitcher.StartSelling(); await botClient.SendMessage(update.Message.Chat, "Продажи начаты!"); break; } case Constants.BotCommandsButtons.DisableSelling: { BotModeSwitcher.StopSelling(); await botClient.SendMessage(update.Message.Chat, "Продажи остановлены!"); break; } case Constants.BotCommandsButtons.EnablePurchases: { BotModeSwitcher.StartPurchase(); await botClient.SendMessage(update.Message.Chat, "Покупки начаты!"); break; } case Constants.BotCommandsButtons.DisablePurchases: { BotModeSwitcher.StopPurchase(); await botClient.SendMessage(update.Message.Chat, "Покупки остановлены!"); break; } case "скинуть IMOEXF": { var assets = await _traderDataProvider.GetAssetsByFigi("FUTIMOEXF000"); foreach (var asset in assets) { if (asset.Count > 0) { var command = new TradeCommand() { AccountId = asset.AccountId, CommandType = TradeCommandType.MarketSell, RecomendPrice = null, Figi = asset.Figi, Count = (long)asset.Count, EnableMargin = false, }; await _eventBus.Broadcast(command); } } break; } case "продать IMOEXF": { var command = new TradeCommand() { AccountId = "2274189208", CommandType = TradeCommandType.MarketSell, RecomendPrice = null, Figi = "FUTIMOEXF000", Count = 1 }; await _eventBus.Broadcast(command); break; } case "купить IMOEXF": { var command = new TradeCommand() { AccountId = "2274189208", CommandType = TradeCommandType.MarketBuy, RecomendPrice = null, Figi = "FUTIMOEXF000", Count = 1 }; await _eventBus.Broadcast(command); break; } } await botClient.SendMessage(update.Message.Chat, "Принято!"); } } catch (Exception ex) { _logger.LogError(ex, "Ошибка при обработке сообщения из телеграма."); } } } }