Esempio n. 1
0
        public InsuranceBot(InsuranceBotAccessors accessors, ILoggerFactory loggerFactory, LuisRecognizer luisRecognizer, QnAMaker qna)

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

            QnA = qna ?? throw new ArgumentNullException(nameof(qna));

            // The DialogSet needs a DialogState accessor, it will call it when it has a turn context.
            _dialogs = new DialogSet(accessors.ConversationDialogState);

            if (loggerFactory == null)
            {
                throw new System.ArgumentNullException(nameof(loggerFactory));
            }

            // Initialize the LUIS recognizer
            _luis = luisRecognizer;


            _dialogs.Add(new InsuranceDialog(_accessors.GetInsuranceState, loggerFactory));

            _logger = loggerFactory.CreateLogger <InsuranceBot>();
            _logger.LogTrace("Turn start.");
        }
Esempio n. 2
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services">Specifies the contract for a <see cref="IServiceCollection"/> of service descriptors.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // add options
            services.Configure <BotOptions>(Configuration.GetSection("Bot"));

            // create luis recognizer
            var luisApplication = new LuisApplication(
                Configuration["LuisAppId"],
                Configuration["LuisAPIKey"],
                "https://" + Configuration["LuisAPIHostName"]
                );

            services.AddSingleton(new LuisRecognizer(luisApplication));


            // Create the credential provider to be used with the Bot Framework Adapter.
            services.AddSingleton <ICredentialProvider, ConfigurationCredentialProvider>();

            // Create the Bot Framework Adapter with error handling enabled.
            services.AddSingleton <IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

            // Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
            services.AddSingleton <IStorage, MemoryStorage>();

            // Create the User state. (Used in this bot's Dialog implementation.)
            services.AddSingleton <UserState>();

            // Create the Conversation state. (Used by the Dialog system itself.)
            services.AddSingleton <ConversationState>();

            // The Dialog that will be run by the bot.
            services.AddSingleton <InsuranceDialog>();
            services.AddSingleton <InsuranceState>();

            // Memory Storage is for local bot debugging only. When the bot
            // is restarted, everything stored in memory will be gone.
            IStorage dataStore = new MemoryStorage();

            // Create and add conversation state.
            var conversationState = new ConversationState(dataStore);

            services.AddSingleton(conversationState);

            var userState = new UserState(dataStore);

            services.AddSingleton(userState);

            // Add translation middleware here
            var languangeProperty = conversationState.CreateProperty <string>("Language");

            services.AddSingleton(new TranslationMiddleware(languangeProperty));

            // Add Hero Cards translation middleware here
            services.AddSingleton(new HeroCardsTranslationMiddleware(languangeProperty));


            services.AddTransient <IBot, InsuranceBot>();

            // Create and register state accessors.
            // Accessors created here are passed into the IBot-derived class on every turn.
            services.AddSingleton(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");
                }

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

                return(accessors);
            });

            // Add QnA Maker here
            // Create and register a QnA service and knowledgebase
            services.AddSingleton(sp =>
            {
                return(new QnAMaker(
                           new QnAMakerEndpoint
                {
                    EndpointKey = "88f57255-703b-4cc8-85a8-847c580643e9",
                    Host = "https://minilitware-qna-em.azurewebsites.net/qnamaker",
                    KnowledgeBaseId = "e51dc78e-5cdd-4c77-94b3-ec84c391f7b4",
                },
                           new QnAMakerOptions
                {
                    ScoreThreshold = 0.9f,
                    Top = 1,
                }));
            });
        }