public FitBitAssistantBot(FitBitAssistantBotAccessors accessors) { if (string.IsNullOrEmpty(Constants.ConnectionName)) { throw new InvalidOperationException("Connection Name needs to be set in the constants class"); } _accessors = accessors ?? throw new ArgumentNullException(nameof(accessors)); _dialogs = new DialogSet(_accessors.ConversationDialogState); _dialogs.Add(DialogHelpers.OAuthPrompt(Constants.ConnectionName)); _dialogs.Add(new WaterfallDialog(Constants.RootDialogName, new WaterfallStep[] { PromptStepAsync, ProcessStepAsync })); }
public async Task <DialogContext> ProcessInputAsync(ITurnContext turnContext, CancellationToken cancellationToken) { var dc = await _dialogs.CreateContextAsync(turnContext, cancellationToken); string command = turnContext.Activity.Text.ToLowerInvariant(); command = GenericHelpers.ParseCommand(command); switch (command) { case "signout": case "logout": case "signoff": case "logoff": // The bot adapter encapsulates the authentication processes and sends // activities to from the Bot Connector Service. var botAdapter = (BotFrameworkAdapter)turnContext.Adapter; await botAdapter.SignOutUserAsync(turnContext, Constants.ConnectionName, cancellationToken : cancellationToken); // Let the user know they are signed out. await turnContext.SendActivityAsync("You are now signed out. Please close the chat window or type in anything to begin again", cancellationToken : cancellationToken); await dc.EndDialogAsync(Constants.RootDialogName, cancellationToken); break; case "help": var reply = turnContext.Activity.CreateReply(); reply.Attachments = new List <Attachment>() { DialogHelpers.CreateBotHelpHeroCard().ToAttachment() }; await turnContext.SendActivityAsync(reply, cancellationToken : cancellationToken); break; //case "magiccode": // var functionReply = turnContext.Activity.CreateReply(); // functionReply.Attachments = new List<Attachment>() // { // DialogHelpers.CreateBotHelpHeroCard().ToAttachment() // }; // await turnContext.SendActivityAsync(functionReply); // break; default: await dc.ContinueDialogAsync(cancellationToken); if (!turnContext.Responded) { await dc.BeginDialogAsync(Constants.RootDialogName, cancellationToken : cancellationToken); } break; } return(dc); }
private async Task <DialogTurnResult> ProcessStepAsync(WaterfallStepContext step, CancellationToken cancellationToken) { if (step.Result != null) { var tokenResponse = step.Result as TokenResponse; // If we have the token use the user is authenticated so we may use it to make API calls. if (tokenResponse?.Token != null) { var parts = _accessors.CommandState.GetAsync(step.Context, () => string.Empty, cancellationToken: cancellationToken).Result.Split(' '); string command = parts[0].ToLowerInvariant(); if (command == "myprofile") { FitBitApiHelper fitBitApiHelper = new FitBitApiHelper(tokenResponse.Token); UserProfile userProfile = await fitBitApiHelper.GetUserProfileAsync(); var reply = step.Context.Activity.CreateReply(); reply.Attachments = new List <Attachment>() { DialogHelpers.CreateUserProfileAdaptiveCard(userProfile), DialogHelpers.CreateBotFunctionsHeroCard("What would you like to do next? Select one").ToAttachment() }; await step.Context.SendActivityAsync(reply, cancellationToken); } else if (command == "mybadges") { FitBitApiHelper fitBitApiHelper = new FitBitApiHelper(tokenResponse.Token); UserBadges userBadges = await fitBitApiHelper.GetUserBadgesAsync(); var reply = step.Context.Activity.CreateReply(); reply.Attachments = new List <Attachment>() { DialogHelpers.CreateUserBadgesCard(userBadges), DialogHelpers.CreateBotFunctionsHeroCard("What would you like to do next? Select one").ToAttachment() }; await step.Context.SendActivityAsync(reply, cancellationToken); } else { var reply = step.Context.Activity.CreateReply(); reply.Attachments = new List <Attachment>() { DialogHelpers.CreateBotFunctionsHeroCard("This is what I can do for you.").ToAttachment() }; await step.Context.SendActivityAsync(reply, cancellationToken); } //else //{ // await step.Context.SendActivityAsync($"Your token is: {tokenResponse.Token}", cancellationToken: cancellationToken); //} } } else { await step.Context.SendActivityAsync("We couldn't log you in. Please try again later.", cancellationToken : cancellationToken); } await _accessors.CommandState.DeleteAsync(step.Context, cancellationToken); return(await step.EndDialogAsync(cancellationToken : cancellationToken)); }
public void ConfigureServices(IServiceCollection services) { services.AddBot <FitBitAssistantBot>(options => { var secretKey = Configuration.GetSection("botFileSecret")?.Value; var botFilePath = Configuration.GetSection("botFilePath")?.Value; var botconfig = BotConfiguration.Load(botFilePath ?? @".\FitBitAssistantBot.bot", secretKey); services.AddSingleton(sp => botconfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botconfig})")); var environment = _isProduction ? "production" : "development"; var service = botconfig.Services.Where(x => x.Type == "endpoint" && x.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); ILogger logger = _loggerFactory.CreateLogger <FitBitAssistantBot>(); options.OnTurnError = async(context, exception) => { logger.LogError($"Exception Caught: {exception}"); await DialogHelpers.SenErrorMessageAsync(context); }; IStorage dataStore = new MemoryStorage(); var conversationState = new ConversationState(dataStore); options.State.Add(conversationState); var userState = new UserState(dataStore); options.State.Add(userState); }); services.AddSingleton <FitBitAssistantBotAccessors>(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."); } var userState = options.State.OfType <UserState>().FirstOrDefault(); if (userState == null) { throw new InvalidOperationException("UserState must be defined and added before adding conversation-scoped state accessors."); } var accessors = new FitBitAssistantBotAccessors(conversationState, userState) { CommandState = userState.CreateProperty <string>(FitBitAssistantBotAccessors.CommandStateName), ConversationDialogState = userState.CreateProperty <DialogState>(FitBitAssistantBotAccessors.DialogStateName), }; return(accessors); }); }