Exemple #1
0
        private async Task <DialogTurnResult> EndProcessingIntents(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var authenticatedUserIsAuthenticated = await _AuthenticatedUserAccessor.GetAsync(stepContext.Context, () => new AuthenticatedUser());

            // var apiResults = await _AuthenticatedUserAccessor.GetAsync(stepContext.Context, () => new APIResults());

            if (stepContext.Result == null || stepContext.Result.GetType() != typeof(Intent))
            {
                return(await stepContext.EndDialogAsync());
            }

            Intent executingIntent = (Intent)stepContext.Result;

            // now call graph API
            // bur check if token expired
            if (authenticatedUserIsAuthenticated.IsAuthenticated /*&& authenticatedUserIsAuthenticated.Expiration > DateTime.UtcNow*/)
            {
                executingIntent.Offset = stepContext.Context.Activity.LocalTimestamp.Value.Offset;
                Type       T          = System.Reflection.Assembly.GetExecutingAssembly().GetType(executingIntent.APIEndpointHandler);
                APIHandler apiHandler = Activator.CreateInstance(T, new object[] { authenticatedUserIsAuthenticated.JwtSecurityToken }) as APIHandler;
                APIResult  result     = await apiHandler.ExecuteAPI(executingIntent);

                // await stepContext.Context.SendActivityAsync(MessageFactory.Text(string.Format(executingIntent.ConfirmationText, executingIntent.RequiredEntities.Select(entity => entity.Value).ToArray())), cancellationToken);

                if (result.Code == APIResultCode.Ok)
                {
                    if (authenticatedUserIsAuthenticated.APIResults == null)
                    {
                        authenticatedUserIsAuthenticated.APIResults = new APIResults();
                    }

                    authenticatedUserIsAuthenticated.APIResults.Add(result);

                    await stepContext.Context.SendActivityAsync(MessageFactory.Text(result.ResultText), cancellationToken);
                }
                else
                {
                    await stepContext.Context.SendActivityAsync(MessageFactory.Text(result.ErrorText), cancellationToken);

                    //Logger.LogInformation(result.ErrorText);
                }

                await stepContext.Context.SendActivityAsync(MessageFactory.Text("You can make your other request"), cancellationToken);
            }
            else
            {
                // log in again

                authenticatedUserIsAuthenticated.IsAuthenticated = false;

                await _AuthenticatedUserAccessor.SetAsync(stepContext.Context, authenticatedUserIsAuthenticated, cancellationToken);

                await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Session expired. Please, login again."), cancellationToken);

                await stepContext.EndDialogAsync();

                return(await stepContext.BeginDialogAsync(this.InitialDialogId));

                // return await PromptStepAsync(stepContext, cancellationToken);
            }

            return(await stepContext.EndDialogAsync());
        }