private async Task AfterLessonFinished(IDialogContext context, IAwaitable <string> result) { // TODO: inject dependency var badgeRepository = new Gamification.BadgeRepository(); var updatedProfile = GetUserGamerProfile(context); var user = context.GetUserData(); updatedProfile.Points += _pointsPerLesson; var newBadges = badgeRepository.GetEligibleBadges(updatedProfile, _userPointsBeforeLessonPlan); updatedProfile.Badges.AddRange(newBadges); if (newBadges != null && newBadges.Any()) { var tasks = newBadges.Select(async badge => { AdaptiveCard adaptiveCard = new AdaptiveCard() { Body = new List <CardElement>() { new Image() { Size = ImageSize.Medium, Url = "https://www.kastatic.org/images/badges/meteorite/thumbs-up-512x512.png" }, new TextBlock() { Text = await MessageTranslator.TranslateTextAsync($"You unlocked {badge}", user?.NativeLanguageIsoCode), Size = TextSize.Large, Wrap = true } } }; Attachment attachment = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = adaptiveCard }; var reply = context.MakeMessage(); reply.Attachments.Add(attachment); await context.PostAsync(reply, CancellationToken.None); }); await Task.WhenAll(tasks); } // The current lesson finished. Plug in Analytics. var finalMessage = await result; await context.PostAsync(finalMessage); // Refactor var updatedUserData = context.GetUserData(); updatedUserData.GamerProfile = updatedProfile; context.UpdateUserData(updatedUserData); context.Done(string.Empty); }
public async Task StartAsync(IDialogContext context) { var _oldGamerProfile = GetUserGamerProfile(context); _userPointsBeforeLessonPlan = _oldGamerProfile.Points; var user = context.GetUserData(); string friendlyUserName = user?.UserName; ICollection <string> lessonTitle = new List <string>(); // get up to 5 lessons int i = 0; foreach (Lesson lesson in LessonPlanModule.LessonPlan.Lessons) { if (i >= 7) { break; } //var lessonInNativaLanguage = await MessageTranslator.TranslateTextAsync(lesson.LessonTitle, user?.NativeLanguageIsoCode); lessonTitle.Add(lesson.LessonTitle); i++; } var questionInNativeLanguage = await MessageTranslator.TranslateTextAsync($"Which lesson would you like to start?", user?.NativeLanguageIsoCode); PromptDialog.Choice( context, this.AfterLessonSelected, lessonTitle, $"{questionInNativeLanguage}", Shared.DoNotUnderstand, attempts: Shared.MaxPromptAttempts); }
private async Task AfterChoosingLanguageSwitch(IDialogContext context, IAwaitable <object> result) { var response = await result as string; if (response == null) { await this.StartAsync(context); return; } try { var detectedLanguage = context.UserData.GetValue <string>(Constants.Shared.UserLanguageCodeKey); UserData userData = context.GetUserData(); var translatedResponse = await MessageTranslator.TranslateTextAsync(response); if (translatedResponse.Equals(Shared.Yes, StringComparison.InvariantCultureIgnoreCase)) { userData.NativeLanguageIsoCode = detectedLanguage; var englishName = CultureInfo.GetCultureInfo(userData.NativeLanguageIsoCode).EnglishName; string translatedSelfIntroduction = await MessageTranslator.TranslateTextAsync(string.Format(BotPersonality.BotLanguageIntroduction, englishName), userData.NativeLanguageIsoCode); var introductionResponseTask = context.PostTranslatedAsync($"{translatedSelfIntroduction}"); var translatedUserNameQuestionTask = MessageTranslator.TranslateTextAsync(BotPersonality.UserNameQuestion, userData.NativeLanguageIsoCode); await Task.WhenAll(introductionResponseTask, translatedUserNameQuestionTask); await context.PostTranslatedAsync(translatedUserNameQuestionTask.Result); context.UpdateUserData(userData); await EngageWithUser(context); } } catch (TooManyAttemptsException) { UserData userData = context.GetUserData(); string translatedTooManyAttemptMessage = await MessageTranslator.TranslateTextAsync(Shared.TooManyAttemptMessage, userData.NativeLanguageIsoCode); await Task.WhenAll(context.PostTranslatedAsync($"{translatedTooManyAttemptMessage}"), this.StartAsync(context)); } }
private async Task CheckAnswerOptionsAsync(IDialogContext context, IAwaitable <IMessageActivity> result) { var studentAnswer = string.Empty; try { studentAnswer = (await result).Text; } catch (Exception) { // Swallow exception for the demo purpose // TODO log the exception } var question = quiz.Questions.ElementAtOrDefault(quiz.currentQuestion); if (question == null) { return; } // if the answer given is correct if (studentAnswer != null && question.CorrectAnswers.Any(answers => studentAnswer.Equals(answers, StringComparison.InvariantCultureIgnoreCase))) { await context.PostAsync(question.CorrectAnswerBotResponse); // check if run out of questions if (quiz.currentQuestion > quiz.Questions.Count - 1) { context.Done("Congrats! You passed the quiz."); var user = context.GetUserData(); var nm = new NotificationManager(); nm.RecordEvent(EventType.GameCompleted.ToString(), quiz.Questions.Count.ToString(), "Quizz", user.UserName); } else { quiz.currentQuestion++; await this.StartAsync(context); } } else { await context.PostTranslatedAsync(question.WrongAnswerBotResponse); await this.StartAsync(context); } }
public async Task HandleUnrecognizedIntent(IDialogContext context, LuisResult result) { var userData = context.GetUserData(); var detectedLanguage = await MessageTranslator.IdentifyLangAsync(result.Query); if (!detectedLanguage.Equals(userData.NativeLanguageIsoCode) && !detectedLanguage.Equals(MessageTranslator.DEFAULT_LANGUAGE)) { context.UserData.SetValue(Constants.Shared.UserLanguageCodeKey, detectedLanguage); await SwitchLanguage(context, userData, detectedLanguage); } else { await context.PostTranslatedAsync(BotPersonality.BotResponseUnrecognizedIntent); await EngageWithUser(context); } }
private Gamification.GamerProfile GetUserGamerProfile(IDialogContext context) { var userData = context.GetUserData(); return(userData.GamerProfile); }