private async Task SetUpTurnStateAsync(ITurnContext turnContext, BotFrameworkClient botFrameworkClient)
        {
            turnContext.TurnState.Add(botFrameworkClient);
            turnContext.TurnState.Add(_skillConversationIdFactoryBase);
            turnContext.TurnState.Add(_conversationState);
            turnContext.TurnState.Add(_userState);
            turnContext.TurnState.Add(_resourceExplorer);
            turnContext.TurnState.Add(_memoryScopes);
            turnContext.TurnState.Add(_pathResolvers);
            turnContext.TurnState.Add(_resourceExplorer.TryGetResource(_languageGeneratorId, out var resource) ? (LanguageGenerator) new ResourceMultiLanguageGenerator(_languageGeneratorId) : new TemplateEngineLanguageGenerator());
            turnContext.TurnState.Add(_languageGeneratorManagers.GetOrAdd(_resourceExplorer, _ => new LanguageGeneratorManager(_resourceExplorer)));
            turnContext.TurnState.Add(_languagePolicy);
            turnContext.TurnState.Add(_telemetryClient);

            // Catch "SetTestOptions" event and save into "Conversation.TestOptions".
            // Note: This is consumed by AdaptiveExpressions Extensions.RandomNext
            if (turnContext.Activity.Type == ActivityTypes.Event)
            {
                var eventActivity = turnContext.Activity.AsEventActivity();
                if (eventActivity.Name == "SetTestOptions")
                {
                    _logger.LogInformation("SetTestOptions received.  This could change the behavior of AdaptiveExpression RandomNext.");

                    var conversationState = turnContext.TurnState.Get <ConversationState>();
                    var property          = conversationState.CreateProperty <object>("TestOptions");
                    await property.SetAsync(turnContext, eventActivity.Value).ConfigureAwait(false);
                }
            }

            // put this on the TurnState using Set because some adapters (like BotFrameworkAdapter and CloudAdapter) will have already added it
            turnContext.TurnState.Set <BotCallbackHandler>(OnTurnAsync);
        }
Exemplo n.º 2
0
        private void SetUpTurnState(ITurnContext turnContext, BotFrameworkClient botFrameworkClient)
        {
            turnContext.TurnState.Add(botFrameworkClient);
            turnContext.TurnState.Add(_skillConversationIdFactoryBase);
            turnContext.TurnState.Add(_conversationState);
            turnContext.TurnState.Add(_userState);
            turnContext.TurnState.Add(_resourceExplorer);
            turnContext.TurnState.Add(_memoryScopes);
            turnContext.TurnState.Add(_pathResolvers);
            turnContext.TurnState.Add(_resourceExplorer.TryGetResource(_languageGeneratorId, out var resource) ? (LanguageGenerator) new ResourceMultiLanguageGenerator(_languageGeneratorId) : new TemplateEngineLanguageGenerator());
            turnContext.TurnState.Add(_languageGeneratorManagers.GetOrAdd(_resourceExplorer, _ => new LanguageGeneratorManager(_resourceExplorer)));
            turnContext.TurnState.Add(new LanguagePolicy(_defaultLocale));

            // put this on the TurnState using Set because some adapters (like BotFrameworkAdapter and CloudAdapter) will have already added it
            turnContext.TurnState.Set <BotCallbackHandler>(OnTurnAsync);
        }
Exemplo n.º 3
0
        public static Func <string, ImportResolverDelegate> MultiLanguageResolverDelegate(ResourceExplorer resourceExplorer) =>
        (string targetLocale) =>
        (string source, string id) =>
        {
            var languagePolicy = new LanguagePolicy();

            var locales = new string[] { string.Empty };
            if (!languagePolicy.TryGetValue(targetLocale, out locales))
            {
                if (!languagePolicy.TryGetValue(string.Empty, out locales))
                {
                    throw new Exception($"No supported language found for {targetLocale}");
                }
            }

            var resourceName = Path.GetFileName(PathUtils.NormalizePath(id));

            foreach (var locale in locales)
            {
                var resourceId = string.IsNullOrEmpty(locale) ? resourceName : resourceName.Replace(".lg", $".{locale}.lg");

                if (resourceExplorer.TryGetResource(resourceId, out var resource))
                {
                    var content = resource.ReadTextAsync().GetAwaiter().GetResult();

                    return(content, resourceName);
                }
            }

            return(string.Empty, resourceName);
        };
        private async Task <Dialog> CreateDialogAsync()
        {
            if (!_resourceExplorer.TryGetResource(_adaptiveDialogId, out var adaptiveDialogResource))
            {
                var msg = $"The ResourceExplorer could not find a resource with id '{_adaptiveDialogId}'";
                _logger.LogError(msg);
                throw new InvalidOperationException(msg);
            }

            var adaptiveDialog = await _resourceExplorer.LoadTypeAsync <AdaptiveDialog>(adaptiveDialogResource, CancellationToken.None).ConfigureAwait(false);

            // if we were passed any Dialogs then add them to the AdaptiveDialog's DialogSet so they can be invoked from any other Dialog
            foreach (var dialog in _dialogs)
            {
                adaptiveDialog.Dialogs.Add(dialog);
            }

            return(adaptiveDialog);
        }