示例#1
0
        public FitBitAssistantBot(FitBitAssistantBotAccessors accessors)
        {
            if (string.IsNullOrEmpty(Constants.ConnectionName))
            {
                throw new InvalidOperationException("Connection Name needs to be set in the constants class");
            }

            _accessors = accessors ?? throw new ArgumentNullException(nameof(accessors));

            _dialogs = new DialogSet(_accessors.ConversationDialogState);
            _dialogs.Add(DialogHelpers.OAuthPrompt(Constants.ConnectionName));
            _dialogs.Add(new WaterfallDialog(Constants.RootDialogName, new WaterfallStep[] { PromptStepAsync, ProcessStepAsync }));
        }
示例#2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddBot <FitBitAssistantBot>(options =>
            {
                var secretKey   = Configuration.GetSection("botFileSecret")?.Value;
                var botFilePath = Configuration.GetSection("botFilePath")?.Value;

                var botconfig = BotConfiguration.Load(botFilePath ?? @".\FitBitAssistantBot.bot", secretKey);
                services.AddSingleton(sp => botconfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botconfig})"));

                var environment = _isProduction ? "production" : "development";

                var service = botconfig.Services.Where(x => x.Type == "endpoint" && x.Name == environment).FirstOrDefault();

                if (!(service is EndpointService endpointService))
                {
                    throw new InvalidOperationException($"The .bot file does not contain an endpoint with name '{environment}'.");
                }
                options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);

                ILogger logger = _loggerFactory.CreateLogger <FitBitAssistantBot>();

                options.OnTurnError = async(context, exception) =>
                {
                    logger.LogError($"Exception Caught: {exception}");
                    await DialogHelpers.SenErrorMessageAsync(context);
                };

                IStorage dataStore = new MemoryStorage();

                var conversationState = new ConversationState(dataStore);
                options.State.Add(conversationState);

                var userState = new UserState(dataStore);
                options.State.Add(userState);
            });

            services.AddSingleton <FitBitAssistantBotAccessors>(sp =>
            {
                var options = sp.GetRequiredService <IOptions <BotFrameworkOptions> >().Value;
                if (options == null)
                {
                    throw new InvalidOperationException("BotFrameworkOptions must be configured prior to setting up the State Accessors");
                }
                var conversationState = options.State.OfType <ConversationState>().FirstOrDefault();
                if (conversationState == null)
                {
                    throw new InvalidOperationException("ConversationState must be defined and added before adding conversation-scoped state accessors.");
                }

                var userState = options.State.OfType <UserState>().FirstOrDefault();
                if (userState == null)
                {
                    throw new InvalidOperationException("UserState must be defined and added before adding conversation-scoped state accessors.");
                }

                var accessors = new FitBitAssistantBotAccessors(conversationState, userState)
                {
                    CommandState            = userState.CreateProperty <string>(FitBitAssistantBotAccessors.CommandStateName),
                    ConversationDialogState = userState.CreateProperty <DialogState>(FitBitAssistantBotAccessors.DialogStateName),
                };
                return(accessors);
            });
        }