/// <summary> /// Initializes a new instance of the <see cref="EchoBot"/> class. /// </summary> /// <param name="accessors">A class containing <see cref="IStatePropertyAccessor{T}"/> used to manage state.</param> /// <param name="loggerFactory">A <see cref="ILoggerFactory"/> that is hooked to the Azure App Service provider.</param> /// <seealso cref="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1#windows-eventlog-provider"/> public EchoBot(BotServices services, EchoBotAccessors accessors, ILoggerFactory loggerFactory, IOptions <MySettings> config) { if (loggerFactory == null) { throw new System.ArgumentNullException(nameof(loggerFactory)); } _logger = loggerFactory.CreateLogger <EchoBot>(); _logger.LogTrace("EchoBot turn start."); _accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors)); }
/// <summary> /// Initializes a new instance of the <see cref="EchoWithCounterBot"/> class. /// </summary> /// <param name="accessors">A class containing <see cref="IStatePropertyAccessor{T}"/> used to manage state.</param> /// <param name="loggerFactory">A <see cref="ILoggerFactory"/> that is hooked to the Azure App Service provider.</param> /// <seealso cref="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1#windows-eventlog-provider"/> public EchoWithCounterBot(EchoBotAccessors accessors, ILoggerFactory loggerFactory) { if (loggerFactory == null) { throw new System.ArgumentNullException(nameof(loggerFactory)); } _logger = loggerFactory.CreateLogger <EchoWithCounterBot>(); _logger.LogTrace("EchoBot turn start."); _accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors)); }
// Add text to speech service // Add Dialogs set // Add Luis Recognizer public EchoBot(EchoBotAccessors accessors, IOptions <MySettings> config) { _accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors)); // Initialize the dialogs // Initialize the TTSS // Initialize the LUIS recognizer // Register the dialog }
public EchoBot(EchoBotAccessors accessors, IOptions <MySettings> config, LuisRecognizer luisRecognizer, QnAMaker qna) { _accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors)); QnA = qna ?? throw new ArgumentNullException(nameof(qna)); _ttsService = new TextToSpeechService(config.Value.VoiceFontName, config.Value.VoiceFontLanguage); _luis = luisRecognizer; _dialogs = new DialogSet(_accessors.ConversationDialogState); _dialogs.Add(new ReservationDialog(_accessors.ReservationState, _ttsService)); }
public EchoBot(EchoBotAccessors accessors, IOptions <MySettings> config, LuisRecognizer luisRecognizer, QnAMaker qna) { _accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors)); QnA = qna ?? throw new ArgumentNullException(nameof(qna)); // Initialize the dialogs _dialogs = new DialogSet(_accessors.ConversationDialogState); // Initialize the TTSS // Initialize the LUIS recognizer _luis = luisRecognizer; // Register the dialog _dialogs.Add(new ReservationDialog(_accessors.ReservationState, null)); }
/// <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.Configure <MySettings>(Configuration); services.AddMvc(); //#if DEBUG // services.AddMvc(); //#else // services.AddMvc(mvcConfig => // { // mvcConfig.Filters.Add(new BasicAuthFilter(Configuration["BasicAuthUsername"], Configuration["BasicAuthPassword"])); // }); //#endif // 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 <ReservationDialog>(); // 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 the personality chat middleware var personalityChatOptions = new PersonalityChatMiddlewareOptions( respondOnlyIfChat: true, scoreThreshold: 0.5F, botPersona: PersonalityChatPersona.Humorous); services.AddSingleton(new PersonalityChatMiddleware(personalityChatOptions)); // Add the translator speech middleware services.AddTransient <IBot, EchoBot>(); // 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 EchoBotAccessors(conversationState, userState) { // Initialize Dialog State ConversationDialogState = conversationState.CreateProperty <DialogState>("DialogState"), ReservationState = userState.CreateProperty <ReservationData>("ReservationState"), }; return(accessors); }); // Add QnA Maker here // We add the the QnA service to the connected services. // Create and register a QnA service and knowledgebase services.AddSingleton(sp => { return(new QnAMaker( new QnAMakerEndpoint { EndpointKey = Configuration["QnAEndpointKey"], Host = Configuration["QnAHostname"], KnowledgeBaseId = Configuration["QnAKbId"], }, new QnAMakerOptions { ScoreThreshold = 0.9f, Top = 1, })); }); }
/// <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.Configure <MySettings>(Configuration); var secretKey = Configuration.GetSection("botFileSecret")?.Value; var botFilePath = Configuration.GetSection("botFilePath")?.Value; // Loads .bot configuration file and adds a singleton that your Bot can access through dependency injection. var botConfig = BotConfiguration.Load(botFilePath ?? @".\EchoBot.bot", secretKey); services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botConfig})")); // Initialize Bot Connected Services clients. services.AddBot <EchoBot>(options => { // Retrieve current endpoint. var environment = _isProduction ? "production" : "development"; var service = botConfig.Services.Where(s => s.Type == "endpoint" && s.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); // Creates a logger for the application to use. ILogger logger = _loggerFactory.CreateLogger <EchoBot>(); // Catches any errors that occur during a conversation turn and logs them. options.OnTurnError = async(context, exception) => { logger.LogError($"Exception caught : {exception}"); await context.SendActivityAsync("Sorry, it looks like something went wrong."); }; // The Memory Storage used here is for local bot debugging only. When the bot // is restarted, everything stored in memory will be gone. IStorage dataStore = new MemoryStorage(); // For production bots use the Azure Blob or // Azure CosmosDB storage providers. For the Azure // based storage providers, add the Microsoft.Bot.Builder.Azure // Nuget package to your solution. That package is found at: // https://www.nuget.org/packages/Microsoft.Bot.Builder.Azure/ // Uncomment the following lines to use Azure Blob Storage // //Storage configuration name or ID from the .bot file. // const string StorageConfigurationId = "<STORAGE-NAME-OR-ID-FROM-BOT-FILE>"; // var blobConfig = botConfig.FindServiceByNameOrId(StorageConfigurationId); // if (!(blobConfig is BlobStorageService blobStorageConfig)) // { // throw new InvalidOperationException($"The .bot file does not contain an blob storage with name '{StorageConfigurationId}'."); // } // // Default container name. // const string DefaultBotContainer = "<DEFAULT-CONTAINER>"; // var storageContainer = string.IsNullOrWhiteSpace(blobStorageConfig.Container) ? DefaultBotContainer : blobStorageConfig.Container; // IStorage dataStore = new Microsoft.Bot.Builder.Azure.AzureBlobStorage(blobStorageConfig.ConnectionString, storageContainer); // Create Conversation State object. // The Conversation State object is where we persist anything at the conversation-scope. var conversationState = new ConversationState(dataStore); options.State.Add(conversationState); }); // Create and register state accesssors. // Acessors created here are passed into the IBot-derived class on every turn. services.AddSingleton <EchoBotAccessors>(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."); } // Create the custom state accessor. // State accessors enable other components to read and write individual properties of state. var accessors = new EchoBotAccessors(conversationState) { ReservationState = conversationState.CreateProperty <ReservationData>(EchoBotAccessors.ReservationStateName), // Initialize Dialog State }; return(accessors); }); }
/// <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.Configure <MySettings>(Configuration); #if DEBUG services.AddMvc(); #else services.AddMvc(mvcConfig => { mvcConfig.Filters.Add(new BasicAuthFilter(Configuration["BasicAuthUsername"], Configuration["BasicAuthPassword"])); }); #endif // create luis recognizer // 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 <ReservationDialog>(); // 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 the personality chat middleware // Add the translator speech middleware services.AddTransient <IBot, EchoBot>(); // 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 EchoBotAccessors(conversationState, userState) { // Initialize Dialog State ReservationState = userState.CreateProperty <ReservationData>("ReservationState"), }; return(accessors); }); // Add QnA Maker here }