public ExamService(ExamsAndMetricService examsAndMetricService, DictionaryService dictionaryService, UsersWordsService usersWordsService) { _examsAndMetricService = examsAndMetricService; _dictionaryService = dictionaryService; _usersWordsService = usersWordsService; }
public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { var variants = examList.Randomize().SelectMany(e => e.GetTranslations()).ToArray(); var msg = $"=====> {word.Word} <=====\r\n" + $"Choose the translation"; await chatIo.SendMessageAsync(msg, InlineButtons.CreateVariants(variants)); var choice = await chatIo.TryWaitInlineIntKeyboardInput(); if (choice == null) { return(ExamResult.Retry); } if (word.GetTranslations().Contains(variants[choice.Value])) { await service.RegisterSuccess(word); return(ExamResult.Passed); } await service.RegisterFailure(word); return(ExamResult.Failed); }
public AddWordService(UsersWordsService usersWordsService, YandexDictionaryApiClient yapiDicClient, YandexTranslateApiClient yapiTransClient, DictionaryService dictionaryService) { _usersWordsService = usersWordsService; _yapiDicClient = yapiDicClient; _yapiTransClient = yapiTransClient; _dictionaryService = dictionaryService; }
public SelectLearningSet(LearningSetService learningSetService, LocalDictionaryService localDictionaryService, UserService userService, UsersWordsService usersWordsService, AddWordService addWordService) { _learningSetService = learningSetService; _localDictionaryService = localDictionaryService; _userService = userService; _usersWordsService = usersWordsService; _addWordService = addWordService; }
public LearnBotCommandHandler( UserService userService, UsersWordsService usersWordsService, ExamSettings examSettings) { _userService = userService; _usersWordsService = usersWordsService; _examSettings = examSettings; }
public ExamFlow( ChatIO chatIo, UsersWordsService usersWordsService, ExamSettings examSettings) { _chatIo = chatIo; _usersWordsService = usersWordsService; _examSettings = examSettings; }
public ShowWellKnownWordsFlow( ChatRoom chat, UsersWordsService usersWordsService, LeafWellKnownWordsUpdateHook wellKnownWordsUpdateHook) { Chat = chat; _usersWordsService = usersWordsService; _wellKnownWordsUpdateHook = wellKnownWordsUpdateHook; }
private static void Main() { TaskScheduler.UnobservedTaskException += (sender, args) => Console.WriteLine($"Unobserved ex {args.Exception}"); _settings = ReadConfiguration(); var yandexDictionaryClient = new YandexDictionaryApiClient(_settings.YadicapiKey, _settings.YadicapiTimeout); var yandexTranslateApiClient = new YandexTranslateApiClient(_settings.YatransapiKey, _settings.YatransapiTimeout); var client = new MongoClient(_settings.MongoConnectionString); var db = client.GetDatabase("SayWhatDb"); var userWordRepo = new UserWordsRepo(db); var dictionaryRepo = new DictionaryRepo(db); var userRepo = new UsersRepo(db); var examplesRepo = new ExamplesRepo(db); userWordRepo.UpdateDb(); dictionaryRepo.UpdateDb(); userRepo.UpdateDb(); examplesRepo.UpdateDb(); _userWordService = new UsersWordsService(userWordRepo, examplesRepo); _dictionaryService = new DictionaryService(dictionaryRepo, examplesRepo); _userService = new UserService(userRepo); _addWordService = new AddWordService( _userWordService, yandexDictionaryClient, yandexTranslateApiClient, _dictionaryService, _userService); QuestionSelector.Singletone = new QuestionSelector(_dictionaryService); Console.WriteLine("Dic started"); _botClient = new TelegramBotClient(_settings.TelegramToken); var me = _botClient.GetMeAsync().Result; Console.WriteLine( $"Hello, World! I am user {me.Id} and my name is {me.FirstName}." ); _botClient.OnUpdate += BotClientOnOnUpdate; _botClient.OnMessage += Bot_OnMessage; _botClient.StartReceiving(); // workaround for infinity awaiting new TaskCompletionSource <bool>().Task.Wait(); // it will never happens _botClient.StopReceiving(); }
public ExamFlow( ChatRoom chat, UserService userService, UsersWordsService usersWordsService, ExamSettings examSettings) { Chat = chat; _userService = userService; _usersWordsService = usersWordsService; _examSettings = examSettings; }
public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { var words = word.Word.Split(',').Select(s => s.Trim()).ToArray(); var minCount = words.Min(t => t.Count(c => c == ' ')); if (minCount > 0 && word.AbsoluteScore < minCount * 4) { return(ExamResult.Impossible); } await chatIo.SendMessageAsync($"=====> {word.TranslationAsList} <=====\r\n" + $"Write the translation... "); var userEntry = await chatIo.WaitUserTextInputAsync(); if (string.IsNullOrEmpty(userEntry)) { return(ExamResult.Retry); } if (words.Any(t => string.Compare(userEntry.Trim(), t, StringComparison.OrdinalIgnoreCase) == 0)) { await service.RegisterSuccess(word); return(ExamResult.Passed); } //search for other translation var translationCandidate = await _dictionaryService.GetAllTranslationWords(userEntry.ToLower()); if (translationCandidate.Any(t1 => word.GetTranslations().Any(t2 => string.CompareOrdinal(t1.Trim(), t2.Trim()) == 0))) { //translation is correct, but for other word await chatIo.SendMessageAsync( $"the translation was correct, but the question was about the word '{word.Word} - {word.TranslationAsList}'\r\nlet's try again"); return(ExamResult.Retry); } var translates = string.Join(",", translationCandidate); if (!string.IsNullOrWhiteSpace(translates)) { await chatIo.SendMessageAsync($"'{userEntry}' translates as {translates}"); } await chatIo.SendMessageAsync("The right translation was: " + word.Word); await service.RegisterFailure(word); return(ExamResult.Failed); }
public async Task <ExamResult> Pass( ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { if (!word.HasAnyPhrases) { return(ExamResult.Impossible); } var targetPhrase = word.GetRandomExample(); var otherExamples = examList .SelectMany(e => e.Phrases) .Where(p => p != targetPhrase) .Take(8).ToArray(); if (!otherExamples.Any()) { return(ExamResult.Impossible); } var variants = otherExamples .Append(targetPhrase) .Randomize() .Select(e => e.TranslatedPhrase) .ToArray(); var msg = $"=====> {targetPhrase.OriginPhrase} <=====\r\n" + $"Choose the translation"; await chatIo.SendMessageAsync(msg, InlineButtons.CreateVariants(variants)); var choice = await chatIo.TryWaitInlineIntKeyboardInput(); if (choice == null) { return(ExamResult.Retry); } if (variants[choice.Value] == targetPhrase.TranslatedPhrase) { await service.RegisterSuccess(word); return(ExamResult.Passed); } await service.RegisterFailure(word); return(ExamResult.Failed); }
public ChatRoomFlow(ChatIO chatIo, TelegramUserInfo userInfo, BotSettings settings, AddWordService addWordsService, UsersWordsService usersWordsService, UserService userService ) { ChatIo = chatIo; _userInfo = userInfo; _settings = settings; _addWordsService = addWordsService; _usersWordsService = usersWordsService; _userService = userService; }
public AddFromLearningSetFlow( ChatRoom chat, LocalDictionaryService localDictionaryService, LearningSet set, UserService userService, UsersWordsService usersWordsService, AddWordService addWordService) { Chat = chat; _localDictionaryService = localDictionaryService; _set = set; _userService = userService; _usersWordsService = usersWordsService; _addWordService = addWordService; }
public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { if (!word.Phrases.Any()) { return(ExamResult.Impossible); } var phrase = word.GetRandomExample(); var replaced = phrase.TranslatedPhrase.Replace(phrase.TranslatedWord, "..."); if (replaced == phrase.TranslatedPhrase) { return(ExamResult.Impossible); } var sb = new StringBuilder(); sb.AppendLine($"\"{phrase.OriginPhrase}\""); sb.AppendLine($" translated as "); sb.AppendLine($"\"{replaced}\""); sb.AppendLine(); sb.AppendLine($"Enter missing word: "); await chatIo.SendMessageAsync(sb.ToString()); while (true) { var enter = await chatIo.WaitUserTextInputAsync(); if (string.IsNullOrWhiteSpace(enter)) { continue; } if (string.Compare(phrase.OriginWord, enter.Trim(), StringComparison.OrdinalIgnoreCase) == 0) { await service.RegisterSuccess(word); return(ExamResult.Passed); } await chatIo.SendMessageAsync($"Origin phrase was \"{phrase.TranslatedPhrase}\""); await service.RegisterFailure(word); return(ExamResult.Failed); } }
public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { if (!word.Phrases.Any()) { return(ExamResult.Impossible); } var phrase = word.GetRandomExample(); var replaced = phrase.OriginPhrase.Replace(phrase.OriginWord, "..."); if (replaced == phrase.OriginPhrase) { return(ExamResult.Impossible); } var sb = new StringBuilder(); sb.AppendLine($"\"{phrase.TranslatedPhrase}\""); sb.AppendLine(); sb.AppendLine($" translated as "); sb.AppendLine(); sb.AppendLine($"\"{replaced}\""); sb.AppendLine($"Choose missing word..."); var variants = examList.Randomize().Select(e => e.Word).ToArray(); var _ = chatIo.SendMessageAsync(sb.ToString(), InlineButtons.CreateVariants(variants)); var choice = await chatIo.TryWaitInlineIntKeyboardInput(); if (choice == null) { return(ExamResult.Retry); } if (variants[choice.Value] == word.Word) { await service.RegisterSuccess(word); return(ExamResult.Passed); } await chatIo.SendMessageAsync($"Origin was: \"{phrase.OriginPhrase}\""); await service.RegisterFailure(word); return(ExamResult.Failed); }
public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { var translations = word.GetTranslations().ToArray(); var minCount = translations.Min(t => t.Count(c => c == ' ')); if (minCount > 0 && word.AbsoluteScore < minCount * 4) { return(ExamResult.Impossible); } await chatIo.SendMessageAsync($"=====> {word.Word} <=====\r\n" + $"Write the translation... "); var translation = await chatIo.WaitUserTextInputAsync(); if (string.IsNullOrEmpty(translation)) { return(ExamResult.Retry); } if (translations.Any(t => string.Compare(translation.Trim(), t, StringComparison.OrdinalIgnoreCase) == 0)) { await service.RegisterSuccess(word); return(ExamResult.Passed); } var allMeaningsOfWord = await _dictionaryService.GetAllTranslationWords(word.Word); if (allMeaningsOfWord .Any(t => string.Compare(translation, t, StringComparison.OrdinalIgnoreCase) == 0)) { await chatIo.SendMessageAsync( $"Chosen translation is out of scope (but it is correct). Expected translations are: " + word.TranslationAsList); return(ExamResult.Impossible); } await chatIo.SendMessageAsync("The translation was: " + word.TranslationAsList); await service.RegisterFailure(word); return(ExamResult.Failed); }
public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { if (!word.HasAnyPhrases) { return(ExamResult.Impossible); } var targetPhrase = word.GetRandomExample(); string shuffled; while (true) { var wordsInExample = targetPhrase.OriginWords; if (wordsInExample.Length < 2) { return(ExamResult.Impossible); } shuffled = string.Join(" ", wordsInExample.Randomize()); if (shuffled != targetPhrase.OriginPhrase) { break; } } await chatIo.SendMessageAsync("Words in phrase are shuffled. Write them in correct order:\r\n'" + shuffled + "'"); var entry = await chatIo.WaitUserTextInputAsync(); entry = entry.Trim(); if (string.Compare(targetPhrase.OriginPhrase, entry.Trim(), StringComparison.OrdinalIgnoreCase) == 0) { await service.RegisterSuccess(word); return(ExamResult.Passed); } await chatIo.SendMessageAsync($"Original phrase was: '{targetPhrase.OriginPhrase}'"); await service.RegisterFailure(word); return(ExamResult.Failed); }
public async Task <ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { var msg = $"=====> {word.Word} <=====\r\n" + $"Do you know the translation?"; var _ = chatIo.SendMessageAsync(msg, new InlineKeyboardButton() { CallbackData = "1", Text = "See the translation" }); await chatIo.WaitInlineIntKeyboardInput(); _ = chatIo.SendMessageAsync($"Translation is \r\n" + $"{word.TranslationAsList}\r\n" + $" Did you guess?", new InlineKeyboardButton { CallbackData = "1", Text = "Yes" }, new InlineKeyboardButton { CallbackData = "0", Text = "No" }); var choice = await chatIo.WaitInlineIntKeyboardInput(); if (choice == 1) { await service.RegisterSuccess(word); return(ExamResult.Passed); } else { await service.RegisterFailure(word); return(ExamResult.Failed); } }
public MainFlow( ChatIO chatIo, TelegramUserInfo userInfo, BotSettings settings, AddWordService addWordsService, UsersWordsService usersWordsService, UserService userService, LocalDictionaryService localDictionaryService, LearningSetService learningSetService) { ChatIo = chatIo; _userInfo = userInfo; _settings = settings; _addWordsService = addWordsService; _usersWordsService = usersWordsService; _userService = userService; _localDictionaryService = localDictionaryService; _learningSetService = learningSetService; }
public async Task<ExamResult> Pass(ChatIO chatIo, UsersWordsService service, UserWordModel word, UserWordModel[] examList) { if (!word.Phrases.Any()) return ExamResult.Impossible; var targetPhrase = word.GetRandomExample(); var other = examList.SelectMany(e => e.Phrases) .Where(p => !string.IsNullOrWhiteSpace(p?.OriginPhrase) && p!= targetPhrase) .Take(8).ToArray(); if(!other.Any()) return ExamResult.Impossible; var variants = other .Append(targetPhrase) .Randomize() .Select(e => e.OriginPhrase) .ToArray(); var msg = $"=====> {targetPhrase.TranslatedPhrase} <=====\r\n" + $"Choose the translation"; await chatIo.SendMessageAsync(msg, variants.Select((v, i) => new InlineKeyboardButton { CallbackData = i.ToString(), Text = v }).ToArray()); var choice = await chatIo.TryWaitInlineIntKeyboardInput(); if (choice == null) return ExamResult.Retry; if (variants[choice.Value] == targetPhrase.OriginPhrase) { await service.RegisterSuccess(word); return ExamResult.Passed; } await service.RegisterFailure(word); return ExamResult.Failed; }
private static void Main() { TaskScheduler.UnobservedTaskException += (sender, args) => Console.WriteLine($"Unobserved ex {args.Exception}"); _settings = ReadConfiguration(substitudeDebugConfig: false); var yandexDictionaryClient = new YandexDictionaryApiClient(_settings.YadicapiKey, _settings.YadicapiTimeout); var client = new MongoClient(_settings.MongoConnectionString); var db = client.GetDatabase(_settings.MongoDbName); //StatsMigrator.Do(db).Wait(); var userWordRepo = new UserWordsRepo(db); var dictionaryRepo = new LocalDictionaryRepo(db); var userRepo = new UsersRepo(db); var examplesRepo = new ExamplesRepo(db); var questionMetricsRepo = new QuestionMetricRepo(db); var learningSetsRepo = new LearningSetsRepo(db); userWordRepo.UpdateDb(); dictionaryRepo.UpdateDb(); userRepo.UpdateDb(); examplesRepo.UpdateDb(); questionMetricsRepo.UpdateDb(); learningSetsRepo.UpdateDb(); Botlog.QuestionMetricRepo = questionMetricsRepo; _userWordService = new UsersWordsService(userWordRepo, examplesRepo); _localDictionaryService = new LocalDictionaryService(dictionaryRepo, examplesRepo); _userService = new UserService(userRepo); _learningSetService = new LearningSetService(learningSetsRepo); _addWordService = new AddWordService( _userWordService, yandexDictionaryClient, _localDictionaryService, _userService); QuestionSelector.Singletone = new QuestionSelector(_localDictionaryService); _botClient = new TelegramBotClient(_settings.TelegramToken); Botlog.CreateTelegramLogger(_settings.BotHelperToken, _settings.ControlPanelChatId); var me = _botClient.GetMeAsync().Result; Botlog.WriteInfo($"Waking up. I am {me.Id}:{me.Username} ", true); _botClient.SetMyCommandsAsync(new[] { new BotCommand { Command = BotCommands.Help, Description = "Help and instructions (Помощь и инстркции)" }, new BotCommand { Command = BotCommands.Start, Description = "Main menu (Главное меню)" }, new BotCommand { Command = BotCommands.Translate, Description = "Translator (Переводчик)" }, new BotCommand { Command = BotCommands.Learn, Description = "Learning translated words (Учить слова)" }, new BotCommand { Command = BotCommands.New, Description = "Show learning sets (Показать наборы для изучения)" }, new BotCommand { Command = BotCommands.Stats, Description = "Your stats (Твоя статистика)" }, new BotCommand { Command = BotCommands.Words, Description = "Your learned words (Твои выученные слова)" }, new BotCommand { Command = BotCommands.Chlang, Description = "Change interface language (Сменить язык интерфейса)" }, }).Wait(); var allUpdates = _botClient.GetUpdatesAsync().Result; Botlog.WriteInfo($"{allUpdates.Length} messages were missed"); foreach (var update in allUpdates) { OnBotWokeUp(update); } if (allUpdates.Any()) { _botClient.MessageOffset = allUpdates.Last().Id + 1; Botlog.WriteInfo($"{Chats.Count} users were waiting for us"); } _botClient.OnUpdate += BotClientOnOnUpdate; _botClient.OnMessage += Bot_OnMessage; _botClient.StartReceiving(); Botlog.WriteInfo($"... and here i go!", false); // workaround for infinity awaiting new TaskCompletionSource <bool>().Task.Wait(); // it will never happens _botClient.StopReceiving(); }
public ShowWellLearnedWordsCommandHandler(UsersWordsService userWordsService, LeafWellKnownWordsUpdateHook wellKnownWordsUpdateHook) { _userWordsService = userWordsService; _wellKnownWordsUpdateHook = wellKnownWordsUpdateHook; }