114 lines
5.4 KiB
C#
114 lines
5.4 KiB
C#
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 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<long> _admins = [];
|
|
private readonly BotModeSwitcher _botModeSwitcher;
|
|
private readonly IDataBus _eventBus;
|
|
private readonly ILogger<BotMessagesHandler> _logger;
|
|
public BotMessagesHandler(BotModeSwitcher botModeSwitcher, IDataBus eventBus, IOptions<TgBotConfig> options, ILogger<BotMessagesHandler> logger)
|
|
{
|
|
_logger = logger;
|
|
_botModeSwitcher = botModeSwitcher;
|
|
_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 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, "Ошибка при обработке сообщения из телеграма.");
|
|
}
|
|
}
|
|
}
|
|
}
|