/// <summary>
        /// Initializes a new instance of the IntentDialog class
        /// </summary>
        /// <param name="dialogId"></param>
        /// <param name="intentFactory"></param>
        /// <param name="logger"></param>
        public IntentDialogBase(
            string dialogId,
            IIntentFactory intentFactory,
            IntentHandlerFactory intentHandlerFactory,
            OAuthDialog oAuthDialog,
            ILogger logger)
            : base(dialogId)
        {
            this.intentFactory        = intentFactory;
            this.intentHandlerFactory = intentHandlerFactory;
            this.logger = logger;

            // Add common prompts
            this.AddDialog(new TextPrompt(nameof(TextPrompt)));
            this.AddDialog(oAuthDialog);

            // Add main flow waterfall
            var warterfallSteps = new WaterfallStep[]
            {
                GetIntentStepAsync,
                GetUserInputStepAsync,
                ExecuteTaskStepAsync,
            };

            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), warterfallSteps));

            // Add children dialogs required by the intent factory
            foreach (var dialog in this.intentFactory.Dialogs)
            {
                AddDialog(dialog);
            }

            // Set the initial child Dialog to the waterfall dialog
            InitialDialogId = nameof(WaterfallDialog);
        }
示例#2
0
        /// <summary>
        /// Azure bot service might return expired token. Add this step to ensure it's valid by logging user out and ask for reauth
        /// </summary>
        /// <param name="stepContext">step context</param>
        /// <param name="cancellationToken"></param>
        /// <returns>dialog turn result</returns>
        private async Task <DialogTurnResult> EnsureValidTokenStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var           tokenResponse = stepContext.Result as TokenResponse;
            TokenResponse tokenToReturn = null;

            if (tokenResponse?.Token != null)
            {
                var jwtToken = new JwtSecurityToken(tokenResponse.Token);

                // Check token expiration. 5 minutes is industry stand for clock skew
                if (jwtToken.ValidTo < DateTime.UtcNow.AddMinutes(5))
                {
                    // we have a token but it has expired. This seems to be a bug in Azure bot service.
                    // We need to work around it by logging user out first and then ask for logging in again
                    var userId = stepContext.Context.Activity?.From?.Id;
                    this.logger.LogWarning($"Token for user {userId} expired. Will logout and let user retry.");

                    // Sign user out
                    await OAuthDialog.SignOutAsync(stepContext.Context, this.oAuthPromptSettings.ConnectionName, this.logger, cancellationToken);

                    // Ask user to sign in again by restarting StartAuthStepAsync
                    await stepContext.Context.SendActivityAsync(MessageFactory.Text("Your token expired. Please sign in again."), cancellationToken);

                    return(await this.StartAuthStepAsync(stepContext, cancellationToken));
                }

                // Token is good, set it to the return value
                tokenToReturn = tokenResponse;
            }

            // For all other cases (success or other authentication failure), move to next step.
            return(await stepContext.NextAsync(tokenToReturn, cancellationToken));
        }
示例#3
0
 /// <summary>
 /// Initializes a new LuisIntentDialog
 /// </summary>
 /// <param name="intentService"></param>
 /// <param name="intentFactory"></param>
 /// <param name="intentHandlerFactory"></param>
 /// <param name="logger"></param>
 public IntentDialog(
     IIntentService intentService,
     IIntentFactory intentFactory,
     IntentHandlerFactory intentHandlerFactory,
     OAuthDialog oAuthDialog,
     ILogger <IntentDialog> logger)
     : base(nameof(IntentDialog), intentFactory, intentHandlerFactory, oAuthDialog, logger)
 {
     this.intentService = intentService;
 }