Exemple #1
0
        private async Task <DialogTurnResult> ProcessStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if (stepContext.Result != null)
            {
                // We do not need to store the token in the bot. When we need the token we can
                // send another prompt. If the token is valid the user will not need to log back in.
                // The token will be available in the Result property of the task.
                var tokenResponse = stepContext.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 command = ((string)stepContext.Values["command"] ?? string.Empty).ToLowerInvariant();

                    if (command == "me")
                    {
                        await OAuthHelpers.ListMeAsync(stepContext.Context, tokenResponse);
                    }
                    else if (command.StartsWith("email"))
                    {
                        await OAuthHelpers.ListEmailAddressAsync(stepContext.Context, tokenResponse);
                    }
                    else
                    {
                        await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Your token is: {tokenResponse.Token}"), cancellationToken);
                    }
                }
            }
            else
            {
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("We couldn't log you in. Please try again later."), cancellationToken);
            }

            return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="GraphAuthenticationBot"/> class.
        /// </summary>
        /// <param name="accessors">State accessors for the bot.</param>
        public GraphAuthenticationBot(GraphAuthenticationBotAccessors accessors)
        {
            if (string.IsNullOrWhiteSpace(ConnectionSettingName))
            {
                throw new InvalidOperationException("ConnectionSettingName must be configured prior to running the bot.");
            }

            _stateAccessors = accessors ?? throw new ArgumentNullException(nameof(accessors));
            _dialogs        = new DialogSet(_stateAccessors.ConversationDialogState);
            _dialogs.Add(OAuthHelpers.Prompt(ConnectionSettingName));
            _dialogs.Add(new ChoicePrompt("choicePrompt"));
            _dialogs.Add(new WaterfallDialog("graphDialog", new WaterfallStep[] { PromptStepAsync, ProcessStepAsync }));
        }
Exemple #3
0
        private async Task <DialogTurnResult> LoginStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            // Get the token from the previous step. Note that we could also have gotten the
            // token directly from the prompt itself. There is an example of this in the next method.
            var tokenResponse = (TokenResponse)stepContext.Result;

            if (tokenResponse != null)
            {
                await OAuthHelpers.ListMeAsync(stepContext.Context, tokenResponse);

                //await stepContext.Context.SendActivityAsync(MessageFactory.Text("You are now logged in."), cancellationToken);
                return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to view or create? (type 'mycalendar', or 'groupcalendar', or 'setappointment',or 'create group calendar')") }, cancellationToken));
            }

            await stepContext.Context.SendActivityAsync(MessageFactory.Text("Login was not successful please try again."), cancellationToken);

            return(await stepContext.EndDialogAsync());
        }
        /// <summary>
        /// Waterfall dialog step to process the command sent by the user.
        /// </summary>
        /// <param name="step">A <see cref="WaterfallStepContext"/> provides context for the current waterfall step.</param>
        /// <param name="cancellationToken">(Optional) A <see cref="CancellationToken"/> that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> representing the operation result of the operation.</returns>
        private async Task <DialogTurnResult> ProcessStepAsync(WaterfallStepContext step, CancellationToken cancellationToken)
        {
            if (step.Result != null)
            {
                // We do not need to store the token in the bot. When we need the token we can
                // send another prompt. If the token is valid the user will not need to log back in.
                // The token will be available in the Result property of the task.
                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   = _stateAccessors.CommandState.GetAsync(step.Context, () => string.Empty, cancellationToken: cancellationToken).Result.Split(' ');
                    string command = parts[0].ToLowerInvariant();

                    if (command == "me")
                    {
                        await OAuthHelpers.ListMeAsync(step.Context, tokenResponse);
                    }
                    else if (command.StartsWith("send"))
                    {
                        await OAuthHelpers.SendMailAsync(step.Context, tokenResponse, parts[1]);
                    }
                    else if (command.StartsWith("recent"))
                    {
                        await OAuthHelpers.ListRecentMailAsync(step.Context, tokenResponse);
                    }
                    else
                    {
                        await step.Context.SendActivityAsync($"Your token is: {tokenResponse.Token}", cancellationToken : cancellationToken);
                    }

                    await _stateAccessors.CommandState.DeleteAsync(step.Context, cancellationToken);
                }
            }
            else
            {
                await step.Context.SendActivityAsync("We couldn't log you in. Please try again later.", cancellationToken : cancellationToken);
            }

            return(await step.EndDialogAsync(cancellationToken : cancellationToken));
        }
Exemple #5
0
        private async Task <DialogTurnResult> ProcessStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if (stepContext.Result != null)
            {
                // We do not need to store the token in the bot. When we need the token we can
                // send another prompt. If the token is valid the user will not need to log back in.
                // The token will be available in the Result property of the task.
                var tokenResponse = stepContext.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)
                {
                    //Options dictionary to give with each dialog (storing entities, card attachements, tokenresponse)
                    var args = new Dictionary <string, object> {
                        { "TokenResponse", tokenResponse }
                    };

                    //LUIS insert user utterance and get intent and entities back
                    var utterance = (string)stepContext.Values["command"];
                    var cli       = new LUISRuntimeClient(new ApiKeyServiceClientCredentials(LuisModelKey))
                    {
                        BaseUri = new Uri(LuisEndpoint)
                    };
                    var prediction = await cli.Prediction.ResolveWithHttpMessagesAsync(LuisModelId, utterance);

                    var topScoringIntent = prediction.Body.TopScoringIntent.Intent;
                    var entities         = prediction.Body.Entities;

                    //Add luis entities to args
                    foreach (var e in entities)
                    {
                        args.Add(e.Type, e.Entity);
                    }

                    //Add an adaptive card to args based on the intent
                    string intent       = Regex.Replace(topScoringIntent, " ", "");
                    string cardFilePath = _cards.Where(c => Regex.IsMatch(c, intent)).FirstOrDefault();

                    if (!string.IsNullOrEmpty(cardFilePath))
                    {
                        Attachment cardAttachment = CreateAdaptiveCardAttachment(cardFilePath);
                        args.Add(nameof(cardAttachment), cardAttachment);
                    }

                    //Adds a general domainCard for prompting the user with a domain choiced (student.hogent.be of hogent.be of ...)
                    Attachment domainCardAttachment
                        = CreateAdaptiveCardAttachment(
                              _cards
                              .Where(c => Regex.IsMatch(c, "DomainUser"))
                              .FirstOrDefault());

                    args.Add(nameof(domainCardAttachment), domainCardAttachment);


                    //If chain that determines based on the intent what dialog to push onto the stack
                    if (topScoringIntent == "Me")
                    {
                        await OAuthHelpers.ListMeAsync(stepContext.Context, tokenResponse);
                    }
                    else if (topScoringIntent == "Send mail")
                    {
                        await stepContext.Context.SendActivityAsync(MessageFactory.Text($"The function isn't enabled"), cancellationToken);
                    }
                    else if (topScoringIntent == "Recent")
                    {
                        await OAuthHelpers.ListRecentMailAsync(stepContext.Context, tokenResponse);
                    }
                    else if (topScoringIntent == "Create User")
                    {
                        return(await stepContext.BeginDialogAsync(nameof(CreateUserDialog), args, cancellationToken));
                    }
                    else if (topScoringIntent == "Remove User")
                    {
                        return(await stepContext.BeginDialogAsync(nameof(RemoveUserDialog), args, cancellationToken));
                    }
                    else if (topScoringIntent == "Check Licenses")
                    {
                        await OAuthHelpers.CheckLicensesAsync(stepContext.Context, tokenResponse);
                    }
                    else if (topScoringIntent == "Get User" || topScoringIntent == "Disable User" || topScoringIntent == "Enable User")
                    {
                        args.Add(nameof(topScoringIntent), topScoringIntent);
                        return(await stepContext.BeginDialogAsync(nameof(DomainNameDialog), args, cancellationToken));
                    }
                    else
                    {
                        await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Sorry, I don't know what you mean"), cancellationToken);

                        await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Currently i have the ability to create/remove/delete/disable/enable a user, check your tenant licenses and get your recent mail."), cancellationToken);
                    }
                }
            }
            else
            {
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("We couldn't log you in. Please try again later."), cancellationToken);
            }

            return(await stepContext.NextAsync(cancellationToken : cancellationToken));
        }