/* * Please change OnTurnAsync logic to better suit your dialog needs. */ public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken)) { if (turnContext == null) { throw new ArgumentNullException(nameof(turnContext)); } if (turnContext.Activity.Type == ActivityTypes.Message) { var dialogContext = await _dialogs.CreateContextAsync(turnContext, cancellationToken); if (dialogContext.ActiveDialog == null) { var model = await _translator.TranslateAsync(turnContext.Activity.Text, Constants.DefaultLanguage, cancellationToken); var botState = await _accessor.FetchStateAsync(turnContext); botState.SpokenLanguage = model.DetectedLanguage; await _accessor.SaveStateAsync(turnContext, botState); turnContext.Activity.Text = model.Text; turnContext.OnSendActivities(HandleBotResponses); turnContext.OnUpdateActivity(HandleBotResponse); } else { dialogContext.Context.OnSendActivities(HandleBotResponses); dialogContext.Context.OnUpdateActivity(HandleBotResponse); } } await next(cancellationToken).ConfigureAwait(false); }
protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken) { if (IsLanguageChangeRequested(turnContext.Activity.Text)) { var currentLang = turnContext.Activity.Text.ToLower(); var lang = currentLang == EnglishEnglish || currentLang == SpanishEnglish ? EnglishEnglish : EnglishSpanish; // If the user requested a language change through the suggested actions with values "es" or "en", // simply change the user's language preference in the user state. // The translation middleware will catch this setting and translate both ways to the user's // selected language. // If Spanish was selected by the user, the reply below will actually be shown in spanish to the user. await _languagePreference.SetAsync(turnContext, lang, cancellationToken); var reply = MessageFactory.Text($"Your current language code is: {lang}"); await turnContext.SendActivityAsync(reply, cancellationToken); // Save the user profile updates into the user state. await _userState.SaveChangesAsync(turnContext, false, cancellationToken); } else { var currentLang = await _languagePreference.GetAsync(turnContext); if (currentLang == "es") { var translatedText = await _translator.TranslateAsync(turnContext.Activity.Text, "en", cancellationToken); turnContext.Activity.Text = translatedText; } // First, we use the dispatch model to determine which cognitive service (LUIS or QnA) to use. var recognizerResult = await _botServices.Dispatch.RecognizeAsync(turnContext, cancellationToken); // Top intent tell us which cognitive service to use. var topIntent = recognizerResult.GetTopScoringIntent(); // Next, we call the dispatcher with the top intent. await DispatchToTopIntentAsync(turnContext, topIntent.intent, recognizerResult, cancellationToken); } }
private async Task <Dictionary <string, string> > GetNewPhraseTranslationsAsync(List <string> phrases, CancellationToken cancellationToken) { _logger.LogTrace(nameof(GetNewPhraseTranslationsAsync)); IDictionary <string, string> phraseTranslations = await _translator .TranslateAsync(phrases, cancellationToken) .ConfigureAwait(false); Dictionary <string, string> newPhrases = new Dictionary <string, string>(); List <DAL.Models.Translation> translations = new List <DAL.Models.Translation>(phrases.Count); foreach (string phrase in phrases) { string newTranslation = phraseTranslations[phrase]; newPhrases.Add(phrase, newTranslation); translations.Add(new DAL.Models.Translation { RussianPhrase = phrase, EnglishPhrase = newTranslation }); } try { await _context.Translations .AddRangeAsync(translations, cancellationToken) .ConfigureAwait(false); await _context.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); } catch (Exception exception) { _logger.LogError(exception, "Не выполнена запись новых переводов в базу данных."); throw; } return(newPhrases); }
private async Task <string> GetTranslatedTextAsync(string title) { return(await _translator.TranslateAsync(title, _language)); }