public void Enter(int userId)
        {
            // check Ya status
            var yaStatus = _addWordService.PingYandex();

            if (yaStatus.isYaDicOnline)
            {
                Console.WriteLine("Yandex dic is online");
            }
            else
            {
                Console.WriteLine("Yandex dic is offline");
            }

            if (yaStatus.isYaTransOnline)
            {
                Console.WriteLine("Yandex trans is online");
            }
            else
            {
                Console.WriteLine("Yandex trans is offline");
            }

            // ask word for translation
            while (true)
            {
                Console.Write("Enter [e] for exit or ");
                Console.Write("Enter english word: ");
                string word = Console.ReadLine();
                if (word == "e")
                {
                    break;
                }

                //find word in local dictionary(if not, find it in Ya dictionary)
                var translations = _addWordService.FindInDictionaryWithPhrases(word);
                if (!translations.Any() && yaStatus.isYaDicOnline)
                {
                    translations = _addWordService.TranslateAndAddToDictionary(word);
                }
                if (!translations.Any())
                {
                    Console.WriteLine("No translations found. Check the word and try again");
                }

                // choose thr translation
                else
                {
                    Console.WriteLine("e: [back to main menu]");
                    Console.WriteLine("c: [CANCEL THE ENTRY]");
                    int i = 1;
                    foreach (var translation in translations)
                    {
                        if (translation.Phrases.Any())
                        {
                            Console.WriteLine($"{i}: {translation.GetTranslations().FirstOrDefault()}\t (+{translation.Phrases.Count})");
                        }
                        else
                        {
                            Console.WriteLine($"{i}: {translation.GetTranslations().FirstOrDefault()}");
                        }
                        i++;
                    }
                    try
                    {
                        var results = ChooseTranslation(translations.ToArray());
                        if (results?.Any() == true)
                        {
                            var count = _addWordService.AddResultToUserCollection(userId, results);
                            Console.WriteLine($"Saved. Translations: {count}");
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        return;
                    }
                }
            }
        }
        private async Task <string?> EnterSingleWordAsync(string?word = null)
        {
            if (string.IsNullOrWhiteSpace(word))
            {
                await Chat.SendMessageAsync($"{Emojis.Translate} {Chat.Texts.EnterWordOrStart}");

                word = await Chat.WaitUserTextInputAsync();
            }

            Chat.User.OnAnyActivity();

            // Search translations in local dictionary

            UserWordModel alreadyExistUserWord = null;

            if (!word.IsRussian())
            {
                alreadyExistUserWord = await _addWordService.GetWordNullByEngWord(Chat.User, word);
            }

            var translations = await _addWordService.FindInLocalDictionaryWithExamples(word);

            if (!translations.Any()) // if not, search it in Ya dictionary
            {
                translations = await _addWordService.TranslateAndAddToDictionary(word);
            }

            if (translations?.Any() != true)
            {
                await Chat.SendMessageAsync(Chat.Texts.NoTranslationsFound);

                return(null);
            }

            // get button selection marks. It works only for english words!
            bool[] selectionMarks = GetSelectionMarks(translations, alreadyExistUserWord);

            if (!selectionMarks[0])
            {
                // Automaticly select first translation if it was not selected before
                await _addWordService.AddTranslationToUser(Chat.User, translations[0].GetEnRu());

                selectionMarks[0] = true;
            }

            // getting first transcription
            var transcription = translations.FirstOrDefault(t => !string.IsNullOrWhiteSpace(t.EnTranscription))
                                ?.EnTranscription;

            var handler = new LastTranslationHandler(
                translations: translations,
                chat: Chat,
                addWordService: _addWordService);

            _translationSelectedUpdateHook.SetLastTranslationHandler(handler);

            await handler.SendTranslationMessage(word, transcription, selectionMarks);

            if (word.IsRussian())
            {
                Chat.User.OnRussianWordTranslationRequest();
            }
            else
            {
                Chat.User.OnEnglishWordTranslationRequest();
            }

            try
            {
                // user request next word
                var res = await Chat.WaitUserTextInputAsync();

                // notify handler, that we leave the flow
                return(res);
            }
            finally
            {
                handler.OnNextTranslationRequest();
            }
        }
Beispiel #3
0
        private async Task <bool> EnterSingleWordAsync(User user, string?word = null)
        {
            if (word == null)
            {
                await _chatIo.SendMessageAsync("Enter english word", new InlineKeyboardButton
                {
                    CallbackData = "/start",
                    Text         = "Cancel"
                });

                while (true)
                {
                    var input = await _chatIo.WaitUserInputAsync();

                    if (input.CallbackQuery != null && input.CallbackQuery.Data == "/start")
                    {
                        throw new ProcessInterruptedWithMenuCommand("/start");
                    }

                    if (!string.IsNullOrEmpty(input.Message?.Text))
                    {
                        word = input.Message.Text;
                        break;
                    }
                }
            }

            //find word in local dictionary(if not, find it in Ya dictionary)
            var translations = await _addWordService.FindInDictionaryWithoutExamples(word);

            if (!translations.Any())
            {
                translations = await _addWordService.TranslateAndAddToDictionary(word);
            }
            if (translations?.Any() != true)
            {
                await _chatIo.SendMessageAsync("No translations found. Check the word and try again");

                return(true);
            }

            await _chatIo.SendMessageAsync($"Choose translation for '{word}'",
                                           InlineButtons.CreateVariantsWithCancel(translations.Select(t => t.RuWord)));

            await _addWordService.RegistrateTranslationRequest(user);

            while (true)
            {
                var input = await _chatIo.TryWaitInlineIntKeyboardInput();

                if (!input.HasValue)
                {
                    return(false);
                }
                if (input !.Value < 0 || input.Value >= translations.Count)
                {
                    continue;
                }

                var selected = translations[input.Value];
                await _addWordService.AddWordsToUser(user, new[] { selected });

                if (selected.Examples.Count > 0)
                {
                    await _chatIo.SendMessageAsync($"Saved. Examples: {selected.Examples.Count}");
                }
                else
                {
                    await _chatIo.SendMessageAsync($"Saved.");
                }
                return(true);
            }
        }