Exemplo n.º 1
0
        public void TestParse(string text)
        {
            var(trigger, success, message) = NotificationTriggers.BuildFromText(text);
            Console.WriteLine($"Success: {success}, message: {message}");

            var actual = trigger.Serialize();

            Console.WriteLine(actual);
            Assert.AreEqual(text.WithoutWhitespaces(), actual);
        }
Exemplo n.º 2
0
        private static Subscriber Convert(SubscriberDbo dbo)
        {
            var trigger = NotificationTriggers.BuildFromText(dbo.SearchSettings);

            if (!trigger.success)
            {
                throw new InvalidOperationException($"Can't parse setting for subscriber {JsonConvert.SerializeObject(dbo)}");
            }
            return(new Subscriber(dbo.Id, dbo.TelegramUsername, dbo.TelegramChatId, dbo.TelegramUserId, dbo.SendTelegramMessages, trigger.result));
        }
        public ParseResult Parse(string telegramMessage)
        {
            var message = telegramMessage?.ToLower() ?? string.Empty;

            if (message.StartsWith(configuration.TelegramSearchSettingWords))
            {
                log.LogInformation("Seen search setting words, lets set up search");

                var setting = message.Remove(0, configuration.TelegramSearchSettingWords.Length);
                var trigger = NotificationTriggers.BuildFromText(setting);

                if (!trigger.success)
                {
                    log.LogError($"Can't parse trigger expression. Error: {trigger.message}");
                    return(ParseResult.Fail(trigger.message));
                }

                return(ParseResult.Success(commandFactory.CreateSetUpSearch(trigger.result)));
            }

            if (message == configuration.TelegramSearchSettingRequestWords)
            {
                return(ParseResult.Success(commandFactory.CreateSetUpSearchMenu()));
            }

            if (message == configuration.TelegramLastNewsRequestFormat)
            {
                return(ParseResult.Success(commandFactory.CreateLastNewsMenu()));
            }

            var lastNewsParseResult = PatternParser.ParseExpressionWithInt(configuration.TelegramLastNewsFormat, message);

            if (lastNewsParseResult.success)
            {
                return(ParseResult.Success(commandFactory.CreateLastNews(lastNewsParseResult.result.Value)));
            }

            return(ParseResult.Success(commandFactory.CreateMenu()));
        }