Example #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            IStorage storage = new MemoryStorage();

            services.AddBot <MoviesBot>(options =>
            {
                options.State.Add(new UserState(storage));
                options.State.Add(new ConversationState(storage));

                // options.CredentialProvider = new SimpleCredentialProvider(Settings.MicrosoftAppId, Settings.MicrosoftAppPassword);
                // The BotStateSet middleware forces state storage to auto-save when the bot is complete processing the message.
                // Note: Developers may choose not to add all the state providers to this middleware if save is not required.
                options.Middleware.Add(new AutoSaveStateMiddleware(options.State.ToArray()));
                options.Middleware.Add(new ShowTypingMiddleware());
            });

            ConversationState conversationState = new ConversationState(storage);

            services.AddSingleton <MoviesBotAccessors>(sp =>
            {
                // We need to grab the conversationState we added on the options in the previous step
                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 user-scoped state accessors.");
                }

                // Create the custom state accessor.
                // State accessors enable other components to read and write individual properties of state.
                var accessors = new MoviesBotAccessors(conversationState, userState)
                {
                    ConversationDialogState = conversationState.CreateProperty <DialogState>("DialogState"),
                    UserProfileAccesor      = userState.CreateProperty <UserProfile>(MoviesBotAccessors.UserProfileName),
                };

                return(accessors);
            });
        }
Example #2
0
 public MoviesBot(MoviesBotAccessors accessors)
 {
     this.accessors = accessors ?? throw new ArgumentNullException(nameof(accessors));
 }