Пример #1
0
        // Initialize the bot and add dialogs and prompts to the dialog set.
        public TaxiAIBotBot(TaxiAIBotAccessors accessors)
        {
            _accessors = accessors ?? throw new ArgumentNullException(nameof(accessors));

            // Create a dialog set for the bot. It requires a DialogState accessor, with which
            // to retrieve the dialog state from the turn context.
            _dialogs = new DialogSet(accessors.DialogStateAccessor);


            // Add the prompts we need to the dialog set.
            _dialogs
            .Add(new TextPrompt(StringHelper.USERNAME_PROMPT))
            .Add(new TextPrompt(StringHelper.PASSWORD_PROMPT));
            // .Add(new ChoicePrompt(SelectionPrompt));

            // Add the dialogs we need to the dialog set.
            _dialogs.Add(new WaterfallDialog(StringHelper.TOP_LEVEL_DIALOG)
                         .AddStep(NameStepAsync)
                         .AddStep(PasswordStepAsync)
                         .AddStep(SaveDataStepAsync)
                         );

            //.AddStep(AcknowledgementStepAsync));

            //_dialogs.Add(new WaterfallDialog(ReviewSelectionDialog)
            //    .AddStep(SelectionStepAsync)
            //    .AddStep(LoopStepAsync));
        }
Пример #2
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> specifies the contract for a collection of service descriptors.</param>
        /// <seealso cref="IStatePropertyAccessor{T}"/>
        /// <seealso cref="https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection"/>
        /// <seealso cref="https://docs.microsoft.com/en-us/azure/bot-service/bot-service-manage-channels?view=azure-bot-service-4.0"/>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddBot <TaxiAIBotBot>(options =>
            {
                var secretKey = Configuration.GetSection("botFileSecret")?.Value;

                // Loads .bot configuration file and adds a singleton that your Bot can access through dependency injection.
                var botConfig = BotConfiguration.Load(@".\TaxiAIBot.bot", secretKey);
                services.AddSingleton(sp => botConfig);

                // Retrieve current endpoint.
                var service = botConfig.Services.Where(s => s.Type == "endpoint" && s.Name == "development").FirstOrDefault();
                if (!(service is EndpointService endpointService))
                {
                    throw new InvalidOperationException($"The .bot file does not contain a development endpoint.");
                }

                options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);

                // Catches any errors that occur during a conversation turn and logs them.
                options.OnTurnError = async(context, exception) =>
                {
                    await context.SendActivityAsync("Sorry, it looks like something went wrong.");
                };
            });


            IStorage dataStore         = new MemoryStorage();
            var      conversationState = new ConversationState(dataStore);
            var      userState         = new UserState(dataStore);



            // Create and register state accessors.
            // Accessors created here are passed into the IBot-derived class on every turn.
            services.AddSingleton <TaxiAIBotAccessors>(sp =>
            {
                // Create the custom state accessor.
                // State accessors enable other components to read and write individual properties of state.
                var accessors = new TaxiAIBotAccessors(conversationState, userState)
                {
                    DialogStateAccessor = conversationState.CreateProperty <DialogState>(StringHelper.DIALOG_STATE),
                    UserInfoAccessor    = userState.CreateProperty <UserInfo>(StringHelper.USER_INFO_FILE)
                };
                accessors.DidBotWelcomeUserAccessor = userState.CreateProperty <bool>(StringHelper.BOT_WELCOME_USER);
                return(accessors);
            });
        }