// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { try { services.AddBot <AADv2Bot>(options => { options.CredentialProvider = new ConfigurationCredentialProvider(Configuration); // The CatchExceptionMiddleware provides a top-level exception handler for your bot. // Any exceptions thrown by other Middleware, or by your OnTurn method, will be // caught here. To facillitate debugging, the exception is sent out, via Trace, // to the emulator. Trace activities are NOT displayed to users, so in addition // an "Ooops" message is sent. // The Memory Storage used here is for local bot debugging only. When the bot // is restarted, anything stored in memory will be gone. IStorage dataStore = new MemoryStorage(); var convoState = new ConversationState(dataStore); var userState = new UserState(dataStore); // The File data store, shown here, is suitable for bots that run on // a single machine and need durable state across application restarts. // IStorage dataStore = new FileStorage(System.IO.Path.GetTempPath()); // For production bots use the Azure Table Store, Azure Blob, or // Azure CosmosDB storage provides, as seen below. To include any of // 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/ // IStorage dataStore = new Microsoft.Bot.Builder.Azure.AzureTableStorage("AzureTablesConnectionString", "TableName"); // IStorage dataStore = new Microsoft.Bot.Builder.Azure.AzureBlobStorage("AzureBlobConnectionString", "containerName"); options.State.Add(convoState); options.State.Add(userState); // Add State to BotStateSet Middleware (that require auto-save) // The BotStateSet Middleware forces state storage to auto-save when the Bot is complete processing the message. // Note: Developers may choose not to add all the State providers to this Middleware if save is not required. var stateSet = new BotStateSet(options.State.ToArray()); options.Middleware.Add(stateSet); }); services.AddSingleton <AADv2BotAccessors>(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(); var userState = options.State.OfType <UserState>().FirstOrDefault(); if (conversationState == null) { throw new InvalidOperationException("ConversationState must be defined and added before adding conversation-scoped state accessors."); } // Create Custom State Property Accessors // State Property Accessors enable components to read and write individual properties, without having to // pass the entire State object. var accessors = new AADv2BotAccessors { CommandState = userState.CreateProperty <string>(AADv2BotAccessors.CommandStateName), ConversationDialogState = conversationState.CreateProperty <DialogState>(AADv2BotAccessors.DialogStateName), }; return(accessors); }); } catch (Exception e) { Console.WriteLine(e); throw; } }
public AADv2Bot(AADv2BotAccessors accessors) { _stateAccessors = accessors; _dialogs = _dialogs = new DialogSet(_stateAccessors.ConversationDialogState); _dialogs.Add(OAuthHelpers.Prompt(ConnectionSettingName)); _dialogs.Add( new WaterfallDialog("displayToken", new WaterfallStep[] { async(dc, step, ct) => { var activity = dc.Context.Activity; if (dc.Context.Activity.Type == ActivityTypes.Message && !Regex.IsMatch(dc.Context.Activity.Text, @"(\d{6})")) { await _stateAccessors.CommandState.SetAsync(dc.Context, activity.Text); } return(await dc.BeginAsync("loginPrompt")); }, async(dc, step, ct) => { var activity = dc.Context.Activity; if (step.Result != null) { var tokenResponse = step.Result as TokenResponse; if (tokenResponse?.Token != null) { var parts = _stateAccessors.CommandState.GetAsync(dc.Context).Result.Split(' '); if (parts[0].ToLowerInvariant() == "me") { await OAuthHelpers.ListMe(dc.Context, tokenResponse); await _stateAccessors.CommandState.DeleteAsync(dc.Context); } else if (parts[0].ToLowerInvariant().StartsWith("send")) { await OAuthHelpers.SendMail(dc.Context, tokenResponse, parts[1]); await _stateAccessors.CommandState.DeleteAsync(dc.Context); } else if (parts[0].ToLowerInvariant().StartsWith("recent")) { await OAuthHelpers.ListRecentMail(dc.Context, tokenResponse); await _stateAccessors.CommandState.DeleteAsync(dc.Context); } else { await dc.Context.SendActivityAsync($"your token is: {tokenResponse.Token}"); await _stateAccessors.CommandState.DeleteAsync(dc.Context); } } } else { await dc.Context.SendActivityAsync("sorry... We couldn't log you in. Try again later."); } return(await dc.EndAsync()); } })); }