public async Task State_RememberPocoPrivateConversationState() { var privateConversationState = new PrivateConversationState(new MemoryStorage()); var testPocoProperty = privateConversationState.CreateProperty <TestPocoState>("testPoco"); var adapter = new TestAdapter() .Use(new AutoSaveStateMiddleware(privateConversationState)); await new TestFlow(adapter, async(context, cancellationToken) => { var conversationState = await testPocoProperty.GetAsync(context, () => new TestPocoState()); Assert.IsNotNull(conversationState, "state.conversation should exist"); switch (context.Activity.AsMessageActivity().Text) { case "set value": conversationState.Value = "test"; await context.SendActivityAsync("value saved"); break; case "get value": await context.SendActivityAsync(conversationState.Value); break; } } ) .Test("set value", "value saved") .Test("get value", "test") .StartTestAsync(); }
public async Task PrivateConversationState_BadActivityConversationThrows() { var dictionary = new Dictionary <string, JObject>(); var userState = new PrivateConversationState(new MemoryStorage(dictionary)); var context = TestUtilities.CreateEmptyContext(); context.Activity.Conversation = null; var testProperty = userState.CreateProperty <TestPocoState>("test"); var value = await testProperty.GetAsync(context); }
public async Task PrivateConversationState_BadActivityFromThrows() { var dictionary = new Dictionary <string, JObject>(); var userState = new PrivateConversationState(new MemoryStorage(dictionary)); var context = TestUtilities.CreateEmptyContext(); context.Activity.Conversation = null; context.Activity.From = null; var testProperty = userState.CreateProperty <TestPocoState>("test"); await Assert.ThrowsAsync <ArgumentNullException>(() => testProperty.GetAsync(context)); }
private BotAccessors GetAccessors() { IStorage dataStore = new MemoryStorage(); var conversationState = new ConversationState(dataStore); var userState = new UserState(dataStore); var privateConversationState = new PrivateConversationState(dataStore); // Create the custom state accessor. // State accessors enable other components to read and write individual properties of state. return(new BotAccessors(conversationState, privateConversationState, userState) { UserData = userState.CreateProperty <BotDataBag>(BotAccessors.UserDataPropertyName), ConversationData = conversationState.CreateProperty <BotDataBag>(BotAccessors.ConversationDataPropertyName), PrivateConversationData = privateConversationState.CreateProperty <BotDataBag>(BotAccessors.PrivateConversationDataPropertyName), DialogData = conversationState.CreateProperty <DialogState>(nameof(DialogState)) }); }
/// <summary> /// Register the bot framework with Asp.net. /// </summary> /// <param name="config">Represents the configuration of the HttpServer.</param> public static void Register(HttpConfiguration config) { BotAccessors accessors = null; var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); var credentialProvider = new SimpleCredentialProvider(ConfigurationManager.AppSettings[MicrosoftAppCredentials.MicrosoftAppIdKey], ConfigurationManager.AppSettings[MicrosoftAppCredentials.MicrosoftAppPasswordKey]); builder.RegisterInstance(credentialProvider).As <ICredentialProvider>(); // 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(); // Create Conversation State object. // The Conversation State object is where we persist anything at the conversation-scope. var conversationState = new ConversationState(dataStore); var userState = new UserState(dataStore); var privateConversationState = new PrivateConversationState(dataStore); // Create the custom state accessor. // State accessors enable other components to read and write individual properties of state. accessors = new BotAccessors(conversationState, privateConversationState, userState) { UserData = userState.CreateProperty <BotDataBag>(BotAccessors.UserDataPropertyName), ConversationData = conversationState.CreateProperty <BotDataBag>(BotAccessors.ConversationDataPropertyName), PrivateConversationData = privateConversationState.CreateProperty <BotDataBag>(BotAccessors.PrivateConversationDataPropertyName), DialogData = conversationState.CreateProperty <DialogState>(nameof(DialogState)) }; builder.RegisterInstance(accessors).As <BotAccessors>(); var container = builder.Build(); var resolver = new AutofacWebApiDependencyResolver(container); config.DependencyResolver = resolver; }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// <param name="services">Specifies the contract for a <see cref="IServiceCollection"/> of service descriptors.</param> /// </summary> public void ConfigureServices(IServiceCollection services) { // Memory Storage 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/ // Un-comment 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 and add conversation state. var conversationState = new ConversationState(dataStore); services.AddSingleton(conversationState); var userState = new UserState(dataStore); services.AddSingleton(userState); var privateConversationState = new PrivateConversationState(dataStore); services.AddSingleton(privateConversationState); services.AddSingleton <StateAccessors>(sp => new StateAccessors(userState, conversationState, privateConversationState) { DialogStateAccessor = conversationState.CreateProperty <DialogState>(nameof(DialogState)), LoggedUserAccessor = privateConversationState.CreateProperty <LoggedUserState>(nameof(LoggedUserState)), LuisStateAccessor = userState.CreateProperty <LuisState>(nameof(LuisState)), CRMStateAccessor = userState.CreateProperty <CRMState>(nameof(CRMState)) }); services.AddOptions(); // Load settings from appsettings.json services.Configure <BotConfig>(Configuration.GetSection("BotConfig")); services.Configure <LuisConfig>(Configuration.GetSection("LuisConfig")); services.Configure <ServicesConfig>(Configuration.GetSection("ServicesConfig")); services.AddSingleton <BotServices>(sp => ActivatorUtilities.CreateInstance <BotServices>(sp)); services.AddBot <ProxiCallBot>(options => { options.CredentialProvider = new SimpleCredentialProvider(Configuration.GetSection("BotConfig")["MicrosoftAppId"], Configuration.GetSection("BotConfig")["MicrosoftAppPassword"]); // Catches any errors that occur during a conversation turn and logs them to currently // configured ILogger. ILogger logger = _loggerFactory.CreateLogger <ProxiCallBot>(); options.OnTurnError = async(context, exception) => { var errorMessage = ExceptionHandler(exception); logger.LogError($"Exception caught : {exception}"); var activity = MessageFactory.Text(errorMessage, errorMessage, InputHints.AcceptingInput); activity.Locale = CulturedBot.Culture?.Name; await context.SendActivityAsync(activity); }; //Adding Teams middleware options.Middleware.Add( new TeamsMiddleware( new ConfigurationCredentialProvider(this.Configuration) ) ); }); services.AddHttpClient <AccountService>(); services.AddHttpClient <CompanyService>(); services.AddHttpClient <LeadService>(); services.AddHttpClient <OpportunityService>(); services.AddHttpClient <ProductService>(); services.AddLocalization(options => options.ResourcesPath = "Resources"); }