public override async Task HandleAsync(CallbackQueryUpdateMessage message) { if (!int.TryParse(Parameter, out int currentPageNumber)) { currentPageNumber = 1; } var source = _sourceRepository.GetByExternalId(message.ChatId) ?? throw new ArgumentException($"Can't perfom {Button} command, there is no source with id {message.ChatId}"); var metricNames = _metricRepository.GetBySourceId(source.Id, currentPageNumber, PagingMenuData.DefaultPageSize) .ToDictionary(m => FormatKey(m.Id), m => $"{m}"); var count = _metricRepository.CountBySourceId(source.Id); var pageCount = (count / PagingMenuData.DefaultPageSize) + 1; var menuData = new PagingMenuData { ButtonTexts = metricNames, CurrentPage = currentPageNumber, PageCount = pageCount, Previous = ButtonCode.MetricMenu, }; message = message with { MenuMessageId = MenuMessageId }; await _botClient.EditTextButtonsMenuWithPagingAsync(message, Button, menuData); } }
public async Task ToJsonButtonCommand_HandleAsync_Success(int updateId) { var message = new CallbackQueryUpdateMessage { ChatId = 1, UpdateId = updateId }; var botResponseMock = new Mock <FakeBotResponse>(); botResponseMock.Setup(x => x.Response(It.IsAny <string>())); IBotClient botClient = new FakeBotClient(botResponseMock.Object); var metricRepoMock = new Mock <IMetricRepository>(); var testData = new TestData(); var metrics = testData.GetMetrics(updateId); metricRepoMock.Setup(x => x.GetBySourceIdWithValues(It.IsAny <long>())).Returns(metrics); var sourceRepoMock = new Mock <ISourceRepository>(); sourceRepoMock.Setup(x => x.GetByExternalId(It.IsAny <long>())).Returns(testData.Source); var command = new ToJsonButtonCommand(botClient, metricRepoMock.Object, sourceRepoMock.Object); await command.HandleAsync(message); var filename = FileName.GetJsonFilename(updateId.ToString()); botResponseMock.Verify(x => x.Response(filename)); }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { if (!long.TryParse(Parameter, out long metricId)) { throw new ArgumentException($"Can't perfom {Button} command, id parameter is not a number - {Parameter}"); } var source = _sourceRepository.GetByExternalId(message.ChatId) ?? throw new ArgumentException($"Can't perfom {Button} command, there is no source with id {message.ChatId}"); var metric = _metricRepository.GetById(metricId) ?? throw new ArgumentException($"Can't perfom {Button} command, there is no metric with id {metricId}"); metric.SetDeleted(); _metricRepository.Update(metric); if (metric.IsMain) { // after removed the main metric, set any other main var hasMainMain = _metricRepository.SetMainMetricIfHasNo(source.Id); source.UpdateState(hasMainMain ? StateType.HasMetric : StateType.NeedAddMetric); _sourceRepository.Update(source); } await _botClient.SendTextMessageAsync(message, MessageCode.Done); }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { var mainMetric = _metricRepository.GetMainByExternalId(message.ChatId); if (mainMetric == null) { await _botClient.SendTextMessageAsync(message, MessageCode.NoMainMetric); return; } var values = _valueRepository.GetByMetricId(mainMetric.Id, 1, int.MaxValue); if (values.Count() < 2) { await _botClient.SendTextMessageAsync(message, MessageCode.ToFewValues); return; } var plotData = new PlotDataFactory().ValuesTo(values, mainMetric.ToString()); var filename = FileName.GetPlotFilename(message.UpdateId.ToString()); IFileService fileService = new ScottPlotService(new PngFileName(filename), plotData); using (var plotService = new FileReaderService(fileService)) { var stream = plotService.GetFileStream(); await _botClient.SendFileAsync(message.ChatId, stream, filename); } }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { message = message with { MenuMessageId = MenuMessageId }; await _botClient.EditTextButtonsMenuAsync(message, ButtonStructure.MetricMenuStructure); } }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { Source source = _sourceRepository.GetByExternalId(message.ChatId) ?? throw new ArgumentException($"Can't perfom {Button} command, there is no source with id {message.ChatId}"); source.UpdateState(StateType.NeedAddMetric); _sourceRepository.Update(source); await _botClient.SendTextMessageAsync(message, MessageCode.EnterMainMetric); }
public Task EditTextButtonsMenuWithPagingAsync(CallbackQueryUpdateMessage message, ButtonCode topButtonCode, PagingMenuData menuData) { var menuBuilder = new InlineKeyboardButtonMenuBuilder(_localizer); var replyMarkup = menuBuilder.AddTextPerLine(menuData.ButtonTexts) .AddPagingLine(menuData.CurrentPage, menuData.PageCount, topButtonCode, message.MenuMessageId) .Build(); PrintOutReplyMarkup(message.LanguageCode, topButtonCode, replyMarkup); return(Task.CompletedTask); }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { var mainMetric = _metricRepository.GetMainByExternalId(message.ChatId); if (mainMetric != null) { var metricText = mainMetric.ToString(); await _botClient.SendTextMessageAsync(message, metricText); } else { await _botClient.SendTextMessageAsync(message, MessageCode.NoMainMetric); } }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { if (!long.TryParse(Parameter, out long valueId)) { throw new ArgumentException($"Can't perfom DelValue command, id parameter is not a number - {Parameter}"); } var value = _valueRepository.GetById(valueId) ?? throw new ArgumentException($"Can't perfom DelValue command, there is no value with id {valueId}"); value.SetDeleted(); _valueRepository.Update(value); await _botClient.SendTextMessageAsync(message, MessageCode.Done); }
public async Task EditTextButtonsMenuWithPagingAsync(CallbackQueryUpdateMessage message, ButtonCode topButtonCode, PagingMenuData menuData) { var menuBuilder = new InlineKeyboardButtonMenuBuilder(_localizer); var replyMarkup = menuBuilder .AddTextPerLine(menuData.ButtonTexts) .AddPagingLine(menuData.CurrentPage, menuData.PageCount, topButtonCode, message.MenuMessageId) .AddBackButton(menuData.Previous, message.MenuMessageId) .Build(); await TelegramBot.EditMessageTextAsync( message.ChatId, message.MenuMessageId, _localizer.GetButtonText(message.LanguageCode, topButtonCode), replyMarkup : replyMarkup); }
public async Task EditTextButtonsMenuAsync(CallbackQueryUpdateMessage message, ButtonStructure structure) { var langCode = message.LanguageCode; var menuBuilder = new InlineKeyboardButtonMenuBuilder(_localizer); var replyMarkup = menuBuilder .AddButtonCodePerLine(structure.MenuButtons, langCode, message.MenuMessageId) .AddBackButton(structure.PreviousButtonCode, message.MenuMessageId) .Build(); await TelegramBot.EditMessageTextAsync( message.ChatId, message.MenuMessageId, _localizer.GetButtonText(langCode, structure.TopButtonCode), replyMarkup : replyMarkup); }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { if (!long.TryParse(Parameter, out long id)) { throw new ArgumentException($"Can't perfom {Button} command, id parameter is not a number - {Parameter}"); } var newMainMetric = _metricRepository.GetById(id) ?? throw new ArgumentException($"Can't perfom {Button} command, there is no metric with id {id}"); var oldMainMetric = _metricRepository.GetMainByExternalId(message.ChatId); if (oldMainMetric != null && oldMainMetric.Id != newMainMetric.Id) { oldMainMetric.UpdateMain(false); _metricRepository.Update(oldMainMetric); newMainMetric.UpdateMain(true); _metricRepository.Update(newMainMetric); } await _botClient.SendTextMessageAsync(message, MessageCode.Done); }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { var source = _sourceRepository.GetByExternalId(message.ChatId) ?? throw new ArgumentException($"Can't perfom {Button} command, there is no source with id {message.ChatId}"); var metrics = _metricRepository.GetBySourceIdWithValues(source.Id); if (metrics == null || !metrics.Any()) { await _botClient.SendTextMessageAsync(message, MessageCode.NoMetrics); return; } var options = new JsonSerializerOptions() { WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), }; var bytes = JsonSerializer.SerializeToUtf8Bytes(metrics, options); var filename = FileName.GetJsonFilename(message.UpdateId.ToString()); await _botClient.SendFileAsync(message.ChatId, bytes, filename); }
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { if (!int.TryParse(Parameter, out int currentPageNumber)) { currentPageNumber = 1; } var mainMetric = _metricRepository.GetMainByExternalId(message.ChatId); if (mainMetric == null) { await _botClient.SendTextMessageAsync(message, MessageCode.NoMainMetric); return; } var valueNames = _valueRepository.GetByMetricId(mainMetric.Id, currentPageNumber, PagingMenuData.DefaultPageSize) .ToDictionary(m => FormatKey(m.Id), m => $"{m}"); var count = _valueRepository.CountByMetric(mainMetric.Id); var pageCount = (count / PagingMenuData.DefaultPageSize) + 1; var menuData = new PagingMenuData { ButtonTexts = valueNames, CurrentPage = currentPageNumber, PageCount = pageCount, Previous = ButtonCode.Menu, }; message = message with { MenuMessageId = MenuMessageId }; await _botClient.EditTextButtonsMenuWithPagingAsync(message, Button, menuData); } }
public abstract Task HandleAsync(CallbackQueryUpdateMessage message);
public override async Task HandleAsync(CallbackQueryUpdateMessage message) { await _botClient.SendTextMessageAsync(message, MessageCode.UploadJson); }
public async Task Visit(CallbackQueryUpdateMessage message) { // This is the result of selecting a menu button var buttonCommand = _buttonCommands.GetCommand(message.Content); await buttonCommand.HandleAsync(message); }
public Task EditTextButtonsMenuAsync(CallbackQueryUpdateMessage message, ButtonStructure structure) { return(SendTextButtonsMenuAsync(message, structure)); }