private Task ReceiveActivityInternalAsync(ITurnContext turnContext, BotCallbackHandler callback, int nextMiddlewareIndex, CancellationToken cancellationToken)
        {
            // Check if we're at the end of the middleware list yet
            if (nextMiddlewareIndex == _middleware.Count)
            {
                // If all the middleware ran, the "leading edge" of the tree is now complete.
                // This means it's time to run any developer specified callback.
                // Once this callback is done, the "trailing edge" calls are then completed. This
                // allows code that looks like:
                //      Trace.TraceInformation("before");
                //      await next();
                //      Trace.TraceInformation("after");
                // to run as expected.

                // If a callback was provided invoke it now and return its task, otherwise just return the completed task
                return(callback?.Invoke(turnContext, cancellationToken) ?? Task.CompletedTask);
            }

            // Get the next piece of middleware
            var nextMiddleware = _middleware[nextMiddlewareIndex];

            // Execute the next middleware passing a closure that will recurse back into this method at the next piece of middleware as the NextDelegate
            return(nextMiddleware.OnTurnAsync(
                       turnContext,
                       (ct) => ReceiveActivityInternalAsync(turnContext, callback, nextMiddlewareIndex + 1, ct),
                       cancellationToken));
        }
Example #2
0
        public async Task LegacyConversationIdFactoryTest()
        {
            var legacyFactory         = new TestLegacyConversationIdFactory();
            var conversationReference = new ConversationReference
            {
                Conversation = new ConversationAccount(id: Guid.NewGuid().ToString("N")),
                ServiceUrl   = "http://testbot.com/api/messages"
            };

            // Testing the deprecated method for backward compatibility.
#pragma warning disable 618
            var conversationId = await legacyFactory.CreateSkillConversationIdAsync(conversationReference, CancellationToken.None);

#pragma warning restore 618
            BotCallbackHandler botCallback = null;
            _mockAdapter.Setup(x => x.ContinueConversationAsync(It.IsAny <ClaimsIdentity>(), It.IsAny <ConversationReference>(), It.IsAny <string>(), It.IsAny <BotCallbackHandler>(), It.IsAny <CancellationToken>()))
            .Callback <ClaimsIdentity, ConversationReference, string, BotCallbackHandler, CancellationToken>((identity, reference, audience, callback, cancellationToken) =>
            {
                botCallback = callback;
                Console.WriteLine("blah");
            });

            var sut = CreateSkillHandlerForTesting(legacyFactory);

            var activity = Activity.CreateMessageActivity();
            activity.ApplyConversationReference(conversationReference);

            await sut.TestOnSendToConversationAsync(_claimsIdentity, conversationId, (Activity)activity, CancellationToken.None);

            Assert.IsNotNull(botCallback);
            await botCallback.Invoke(new TurnContext(_mockAdapter.Object, (Activity)activity), CancellationToken.None);
        }
Example #3
0
        private async Task <InvokeResponse> ProcessActivityAsync(string authHeader, Activity activity, BotCallbackHandler callback, CancellationToken cancellationToken)
        {
            MoMoBotAssert.NotNull(activity);
            using (var context = new TurnContext(activity))
            {
                //context.TurnState.Add<IIdentity>(BotIdentityKey, identity);
                await callback?.Invoke(context, cancellationToken);

                if (activity.Type == ActivityTypes.Invoke)
                {
                    var activityInvokeResponse = context.TurnState.Get <Activity>(InvokeReponseKey);
                    if (activityInvokeResponse == null)
                    {
                        return(new InvokeResponse {
                            Status = (int)HttpStatusCode.NotImplemented
                        });
                    }
                    else
                    {
                        return((InvokeResponse)activityInvokeResponse.Value);
                    }
                }
                return(null);
            }
        }
Example #4
0
        private async Task TestActivityCallback(Activity activity)
        {
            BotCallbackHandler botCallback = null;

            _mockAdapter.Setup(x => x.ContinueConversationAsync(It.IsAny <ClaimsIdentity>(), It.IsAny <ConversationReference>(), It.IsAny <string>(), It.IsAny <BotCallbackHandler>(), It.IsAny <CancellationToken>()))
            .Callback <ClaimsIdentity, ConversationReference, string, BotCallbackHandler, CancellationToken>((identity, reference, audience, callback, cancellationToken) =>
            {
                botCallback = callback;
                Console.WriteLine("blah");
            });

            var sut = CreateSkillHandlerForTesting();

            var activityId = Guid.NewGuid().ToString("N");

            activity.ApplyConversationReference(_conversationReference);

            await sut.TestOnReplyToActivityAsync(_claimsIdentity, _conversationId, activityId, activity, CancellationToken.None);

            Assert.IsNotNull(botCallback);
            await botCallback.Invoke(new TurnContext(_mockAdapter.Object, activity), CancellationToken.None);
        }
Example #5
0
        public async Task OnSendToConversationAsyncTest()
        {
            BotCallbackHandler botCallback = null;

            _mockAdapter.Setup(x => x.ContinueConversationAsync(It.IsAny <ClaimsIdentity>(), It.IsAny <ConversationReference>(), It.IsAny <string>(), It.IsAny <BotCallbackHandler>(), It.IsAny <CancellationToken>()))
            .Callback <ClaimsIdentity, ConversationReference, string, BotCallbackHandler, CancellationToken>((identity, reference, audience, callback, cancellationToken) =>
            {
                botCallback = callback;
                Console.WriteLine("blah");
            });

            var sut = CreateSkillHandlerForTesting();

            var activity = Activity.CreateMessageActivity();

            activity.ApplyConversationReference(_conversationReference);

            await sut.TestOnSendToConversationAsync(_claimsIdentity, _conversationId, (Activity)activity, CancellationToken.None);

            Assert.IsNotNull(botCallback);
            await botCallback.Invoke(new TurnContext(_mockAdapter.Object, (Activity)activity), CancellationToken.None);
        }
Example #6
0
        public async Task OnOnReplyToActivityAsyncTest()
        {
            BotCallbackHandler botCallback = null;

            _mockAdapter.Setup(x => x.ContinueConversationAsync(It.IsAny <ClaimsIdentity>(), It.IsAny <ConversationReference>(), It.IsAny <string>(), It.IsAny <BotCallbackHandler>(), It.IsAny <CancellationToken>()))
            .Callback <ClaimsIdentity, ConversationReference, string, BotCallbackHandler, CancellationToken>((identity, reference, audience, callback, cancellationToken) =>
            {
                botCallback = callback;
            });

            var sut = CreateSkillHandlerForTesting();

            var activity   = (Activity)Activity.CreateMessageActivity();
            var activityId = Guid.NewGuid().ToString("N");

            activity.ApplyConversationReference(_conversationReference);

            await sut.TestOnReplyToActivityAsync(_claimsIdentity, _conversationId, activityId, activity, CancellationToken.None);

            Assert.IsNotNull(botCallback);
            await botCallback.Invoke(new TurnContext(_mockAdapter.Object, _conversationReference.GetContinuationActivity()), CancellationToken.None);

            Assert.IsNull(activity.CallerId);
        }
Example #7
0
 public async override Task ContinueConversationAsync(string botAppId, ConversationReference reference, BotCallbackHandler callback, CancellationToken cancellationToken)
 {
     await callback.Invoke(_ctx, cancellationToken);
 }