/// <summary> /// Initializes a new instance of the <see cref="BasicBot"/> 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 BasicBot(BasicBotAccessors accessors, ILoggerFactory loggerFactory, LuisRecognizer recognizer) { if (loggerFactory == null) { throw new System.ArgumentNullException(nameof(loggerFactory)); } // Lab 2.2.3 Add instance of LUIS Recognizer _recognizer = recognizer ?? throw new ArgumentNullException(nameof(recognizer)); _logger = loggerFactory.CreateLogger <BasicBot>(); _logger.LogTrace("BasicBot turn start."); _accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors)); // The DialogSet needs a DialogState accessor, it will call it when it has a turn context. _dialogs = new DialogSet(_accessors.DialogStateAccessor); // This array defines how the Waterfall will execute. // We can define the different dialogs and their steps here // allowing for overlap as needed. In this case, it's fairly simple // but in more complex scenarios, you may want to separate out the different // dialogs into different files. var main_waterfallsteps = new WaterfallStep[] { GreetingAsync, MainMenuAsync, }; var search_waterfallsteps = new WaterfallStep[] { // Add SearchDialog water fall steps SearchRequestAsync, SearchAsync }; // Add named dialogs to the DialogSet. These names are saved in the dialog state. _dialogs.Add(new WaterfallDialog("mainDialog", main_waterfallsteps)); _dialogs.Add(new WaterfallDialog("searchDialog", search_waterfallsteps)); // The following line allows us to use a prompt within the dialogs _dialogs.Add(new TextPrompt("searchPrompt")); }
/// <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 <BasicBot>(options => { 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 ?? @".\BotConfiguration.bot", secretKey); services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botConfig})")); // 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 <BasicBot>(); // 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 = "botstate"; // 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); var middleware = options.Middleware; // Add middleware below with "middleware.Add(...." // Add Regex below middleware.Add(new RegExpRecognizerMiddleware() .AddIntent("search", new Regex("search picture(?:s)*(.*)|search pic(?:s)*(.*)", RegexOptions.IgnoreCase)) .AddIntent("share", new Regex("share picture(?:s)*(.*)|share pic(?:s)*(.*)", RegexOptions.IgnoreCase)) .AddIntent("order", new Regex("order picture(?:s)*(.*)|order print(?:s)*(.*)|order pic(?:s)*(.*)", RegexOptions.IgnoreCase)) .AddIntent("help", new Regex("help(.*)", RegexOptions.IgnoreCase))); }); // Create and register state accesssors. // Acessors created here are passed into the IBot-derived class on every turn. services.AddSingleton <BasicBotAccessors>(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 BasicBotAccessors(conversationState) { PictureState = conversationState.CreateProperty <PictureState>(BasicBotAccessors.PictureStateName), DialogStateAccessor = conversationState.CreateProperty <DialogState>("DialogState"), }; return(accessors); }); // Create and register a LUIS recognizer. services.AddSingleton(sp => { // Get LUIS information var luisApp = new LuisApplication("0e5b062d-9f97-45f7-a272-d2dd8b935ed3", "ce6ada59f1ac45a7bcc52ce955fe2db2", "https://westus.api.cognitive.microsoft.com/"); // Specify LUIS options. These may vary for your bot. var luisPredictionOptions = new LuisPredictionOptions { IncludeAllIntents = true, }; // Create the recognizer var recognizer = new LuisRecognizer(luisApp, luisPredictionOptions, true, null); return(recognizer); }); }