コード例 #1
0
            public async Task PollForTokenAsync(PollingParams pollingParams)
            {
                BotCallbackHandler continueCallback = async(context, ctoken) =>
                {
                    // TODO: Should be using OAuthClient
                    //var oauthClient = context.TurnState.Get<OAuthClient>();
                    //var tokenResponse = await oauthClient.UserToken.GetTokenAsync(Activity.From.Id, pollingParams.ConnectionName, Activity.ChannelId, null, ctoken).ConfigureAwait(false);
                    var tokenResponse = await(Adapter as BotFrameworkAdapter).GetUserTokenAsync(context, pollingParams.ConnectionName, null, ctoken).ConfigureAwait(false);

                    if (tokenResponse != null)
                    {
                        // This can be used to short-circuit the polling loop.
                        if (tokenResponse.Properties != null)
                        {
                            tokenResponse.Properties.TryGetValue(TurnStateConstants.TokenPollingSettingsKey, out var tokenPollingSettingsToken);

                            var tokenPollingSettings = tokenPollingSettingsToken?.ToObject <TokenPollingSettings>();
                            if (tokenPollingSettings != null)
                            {
                                Logger.LogInformation($"PollForTokenAsync received new polling settings: timeout={tokenPollingSettings.Timeout}, interval={tokenPollingSettings.Interval}", tokenPollingSettings);
                                pollingParams.ShouldEndPolling = tokenPollingSettings.Timeout <= 0 ? true : pollingParams.ShouldEndPolling;                                                    // Timeout now and stop polling
                                pollingParams.PollingInterval  = tokenPollingSettings.Interval > 0 ? TimeSpan.FromMilliseconds(tokenPollingSettings.Interval) : pollingParams.PollingInterval; // Only overrides if it is set.
                            }
                        }

                        // once there is a token, send it to the bot and stop polling
                        if (tokenResponse.Token != null)
                        {
                            var tokenResponseActivityEvent = CreateTokenResponse(Activity.GetConversationReference(), tokenResponse.Token, pollingParams.ConnectionName);
                            await Adapter.ProcessActivityAsync(Identity, tokenResponseActivityEvent, Callback, ctoken).ConfigureAwait(false);

                            pollingParams.ShouldEndPolling = true;
                            pollingParams.SentToken        = true;

                            Logger.LogInformation("PollForTokenAsync completed with a token", Activity);
                        }
                    }
                };

                await Adapter.ContinueConversationAsync(Identity, Activity.GetConversationReference(), continueCallback, cancellationToken : CancellationToken).ConfigureAwait(false);
            }
コード例 #2
0
        private static async Task PollForTokenAsync(PollingHelper pollingHelper, string connectionName)
        {
            try
            {
                var pollingParams = new PollingParams()
                {
                    ConnectionName  = connectionName,
                    PollingInterval = pollingHelper.DefaultPollingInterval,
                    PollingTimeout  = pollingHelper.DefaultPollingTimeout,
                };

                var stopwatch = Stopwatch.StartNew();

                while (stopwatch.Elapsed < pollingParams.PollingTimeout && !pollingParams.ShouldEndPolling)
                {
                    await pollingHelper.PollForTokenAsync(pollingParams).ConfigureAwait(false);

                    if (!pollingParams.ShouldEndPolling)
                    {
                        await Task.Delay(pollingParams.PollingInterval, pollingHelper.CancellationToken).ConfigureAwait(false);
                    }
                }

                if (!pollingParams.SentToken)
                {
                    pollingHelper.Logger.LogInformation("PollForTokenAsync completed without receiving a token", pollingHelper.Activity);
                }

                stopwatch.Stop();
            }
#pragma warning disable CA1031 // Do not catch general exception types (for now we just log the exception and continue)
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                pollingHelper.Logger.LogError(ex, "PollForTokenAsync threw an exception", connectionName);
            }
        }