예제 #1
0
        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);
        }
    }
예제 #2
0
        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));
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
     public override async Task HandleAsync(CallbackQueryUpdateMessage message)
     {
         message = message with {
             MenuMessageId = MenuMessageId
         };
         await _botClient.EditTextButtonsMenuAsync(message, ButtonStructure.MetricMenuStructure);
     }
 }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
        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);
            }
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
예제 #12
0
        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);
        }
예제 #13
0
        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);
        }
예제 #14
0
        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);
        }
    }
예제 #15
0
 public abstract Task HandleAsync(CallbackQueryUpdateMessage message);
예제 #16
0
 public override async Task HandleAsync(CallbackQueryUpdateMessage message)
 {
     await _botClient.SendTextMessageAsync(message, MessageCode.UploadJson);
 }
예제 #17
0
 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);
 }
예제 #18
0
 public Task EditTextButtonsMenuAsync(CallbackQueryUpdateMessage message, ButtonStructure structure)
 {
     return(SendTextButtonsMenuAsync(message, structure));
 }