private async Task AddWordToUser(WordInLearningSet word) { var tranlsations = await _addWordService.GetOrDownloadTranslation(word.Word); var allowedTranlsations = tranlsations.Where( translation => word.AllowedTranslations.Any( allowed => allowed.Equals(translation.TranslatedText, StringComparison.InvariantCultureIgnoreCase))); foreach (var tranlsation in allowedTranlsations) { await _addWordService.AddTranslationToUser(Chat.User, tranlsation); } }
private async Task SelectIthTranslation(int messageId, int index) { _areSelected[index] = !_areSelected[index]; if (_areSelected[index]) //if become selected { await _addWordService.AddTranslationToUser(Chat.User, _translations[index].GetEnRu()); } else { await _addWordService.RemoveTranslationFromUser(Chat.User, _translations[index].GetEnRu()); } var buttons = CreateButtons(); await Chat.EditMessageButtons(messageId, buttons.ToArray()); }
public async Task Handle(Update update) { var buttonData = AddWordHelper.ParseQueryDataOrNull(update.CallbackQuery.Data); if (buttonData == null) { await Chat.ConfirmCallback(update.CallbackQuery.Id); return; } // last translation is cached. var cached = _cachedHandlerTranslationOrNull; if (cached != null && cached.OriginWordText.Equals(buttonData.Origin)) { // if translation is cached - fall into handler for fast handling await cached.Handle(buttonData.Translation, update); return; } // word is not cached // so we need to find already translated items var allTranslations = await _addWordService.FindInLocalDictionaryWithExamples(buttonData.Origin); var originMessageButtons = update.CallbackQuery .Message ?.ReplyMarkup ?.InlineKeyboard ?.SelectMany(i => i) .ToArray(); if (originMessageButtons == null) { await Chat.ConfirmCallback(update.CallbackQuery.Id); return; } if (originMessageButtons.Length < allTranslations.Count) { await Chat.ConfirmCallback(update.CallbackQuery.Id); return; } var selectionMarks = GetSelectionMarks(allTranslations, originMessageButtons); var index = AddWordHelper.FindIndexOf(allTranslations, buttonData.Translation); if (index == -1) { await Chat.ConfirmCallback(update.CallbackQuery.Id); return; } var selectedBefore = selectionMarks[index]; if (!selectedBefore) { selectionMarks[index] = true; await _addWordService.AddTranslationToUser(Chat.User, allTranslations[index].GetEnRu()); await Chat.AnswerCallbackQueryWithTooltip(update.CallbackQuery.Id, Chat.Texts.MessageAfterTranslationIsSelected(allTranslations[index])); } else { selectionMarks[index] = false; await _addWordService.RemoveTranslationFromUser(Chat.User, allTranslations[index].GetEnRu()); await Chat.AnswerCallbackQueryWithTooltip(update.CallbackQuery.Id, Chat.Texts.MessageAfterTranslationIsDeselected(allTranslations[index])); } await Chat.EditMessageButtons( update.CallbackQuery.Message.MessageId, allTranslations .Select((t, i) => AddWordHelper.CreateButtonFor(t, selectionMarks[i])) .ToArray()); }
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(); } }