klhztrader/KLHZ.Trader.Core/TG/Services/BotMessagesHandler.cs

124 lines
6.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using KLHZ.Trader.Core.Common;
using KLHZ.Trader.Core.Common.Messaging.Contracts;
using KLHZ.Trader.Core.Common.Messaging.Contracts.Messages;
using KLHZ.Trader.Core.Common.Messaging.Contracts.Messages.Enums;
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 "сбросить сбер":
{
var command = new TradeCommand()
{
CommandType = TradeCommandType.ForceClosePosition,
RecomendPrice = null,
Figi = "BBG004730N88",
};
await _eventBus.BroadcastCommand(command);
break;
}
case "продать сбер":
{
var command = new TradeCommand()
{
CommandType = TradeCommandType.MarketSell,
RecomendPrice = null,
Figi = "BBG004730N88",
Count = 1,
LotsCount = 1,
};
await _eventBus.BroadcastCommand(command);
break;
}
case "купить сбер":
{
var command = new TradeCommand()
{
CommandType = TradeCommandType.MarketBuy,
RecomendPrice = null,
Figi = "BBG004730N88",
Count = 1
};
await _eventBus.BroadcastCommand(command);
break;
}
}
await botClient.SendMessage(update.Message.Chat, "Принято!");
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при обработке сообщения из телеграма.");
}
}
}
}