示例#1
0
        private IWcbTelegramBotAction handleCountMessage(Update update)
        {
            var text = update.Message.Text.Substring(("/count".Length)).Trim();

            text = Word.EscapeString(text);

            IEnumerable <Word>          words;
            IEnumerable <WordUsedTimes> usages;

            if (String.IsNullOrEmpty(text))
            {
                usages = _usagesRepo.GetByTelegramIdTopWords(update.Message.Chat.Id, 3);
                words  = _wordsRepo.GetByID(usages.Select(usage => usage.WordID));

                return(new WcbTelegramBotAction
                {
                    Type = WcbActionType.Text,
                    ChatID = update.Message.Chat.Id,
                    Text = String.Join("\n", words.Select(word => $"<b>{word.Text}</b>: {usages.Where(usage => usage.WordID == word.ID).First().UsedTimes}")),
                    Mode = ParseMode.Html
                });
            }
            else
            {
                IEnumerable <Word> sourceWords = Word.GetWordsFromText(text).Take(3);
                words = _wordsRepo.GetByText(sourceWords.Select(word => word.Text).ToList());
                //TODO: вот тут words пустые, хотя я мокаю этот метод на возврат list<words>
                usages = _usagesRepo.GetByTelegramIdAndWordsList(update.Message.Chat.Id, words.Select(word => word.Text));

                var responseText = new List <string>();
                foreach (var sourceWord in sourceWords)
                {
                    if (sourceWord.TooShort)
                    {
                        responseText.Add($"<b>{sourceWord.Text}</b>: too short");
                        continue;
                    }
                    var foundWord = words.Where(word => word.Text == sourceWord.Text).FirstOrDefault();
                    if (foundWord == null)
                    {
                        responseText.Add($"<b>{sourceWord.Text}</b>: not found");
                        continue;
                    }
                    var foundWordUsage = usages.Where(usage => usage.WordID == foundWord.ID).First();
                    responseText.Add($"<b>{sourceWord.Text}</b>: {foundWordUsage.UsedTimes}");
                }

                return(new WcbTelegramBotAction
                {
                    Type = WcbActionType.Text,
                    ChatID = update.Message.Chat.Id,
                    Text = String.Join("\n", responseText),
                    Mode = ParseMode.Html
                });
            }
        }