private async Task <SkillConversationReference> GetSkillConversationReferenceAsync(string conversationId, CancellationToken cancellationToken)
        {
            SkillConversationReference skillConversationReference;

            try
            {
                skillConversationReference = await _conversationIdFactory.GetSkillConversationReferenceAsync(conversationId, cancellationToken).ConfigureAwait(false);
            }
            catch (NotImplementedException)
            {
                _logger.LogWarning("Got NotImplementedException when trying to call GetSkillConversationReferenceAsync() on the ConversationIdFactory, attempting to use deprecated GetConversationReferenceAsync() method instead.");

                // Attempt to get SkillConversationReference using deprecated method.
                // this catch should be removed once we remove the deprecated method.
                // We need to use the deprecated method for backward compatibility.
#pragma warning disable 618
                var conversationReference = await _conversationIdFactory.GetConversationReferenceAsync(conversationId, cancellationToken).ConfigureAwait(false);

#pragma warning restore 618
                skillConversationReference = new SkillConversationReference
                {
                    ConversationReference = conversationReference,
                    OAuthScope            = _getOAuthScope()
                };
            }

            if (skillConversationReference == null)
            {
                _logger.LogError($"Unable to get skill conversation reference for conversationId {conversationId}.");
                throw new KeyNotFoundException();
            }

            return(skillConversationReference);
        }
Exemple #2
0
        private async Task <ResourceResponse> ProcessActivityAsync(ClaimsIdentity claimsIdentity, string conversationId, string replyToActivityId, Activity activity, CancellationToken cancellationToken)
        {
            SkillConversationReference skillConversationReference;

            try
            {
                skillConversationReference = await _conversationIdFactory.GetSkillConversationReferenceAsync(conversationId, cancellationToken).ConfigureAwait(false);
            }
            catch (NotImplementedException)
            {
                // Attempt to get SkillConversationReference using deprecated method.
                // this catch should be removed once we remove the deprecated method.
                // We need to use the deprecated method for backward compatibility.
#pragma warning disable 618
                var conversationReference = await _conversationIdFactory.GetConversationReferenceAsync(conversationId, cancellationToken).ConfigureAwait(false);

#pragma warning restore 618
                skillConversationReference = new SkillConversationReference
                {
                    ConversationReference = conversationReference,
                    OAuthScope            = ChannelProvider != null && ChannelProvider.IsGovernment() ? GovernmentAuthenticationConstants.ToChannelFromBotOAuthScope : AuthenticationConstants.ToChannelFromBotOAuthScope
                };
            }

            if (skillConversationReference == null)
            {
                throw new KeyNotFoundException();
            }

            ResourceResponse resourceResponse = null;

            var callback = new BotCallbackHandler(async(turnContext, ct) =>
            {
                turnContext.TurnState.Add(SkillConversationReferenceKey, skillConversationReference);
                activity.ApplyConversationReference(skillConversationReference.ConversationReference);
                turnContext.Activity.Id       = replyToActivityId;
                turnContext.Activity.CallerId = $"{CallerIdConstants.BotToBotPrefix}{JwtTokenValidation.GetAppIdFromClaims(claimsIdentity.Claims)}";
                switch (activity.Type)
                {
                case ActivityTypes.EndOfConversation:
                    await _conversationIdFactory.DeleteConversationReferenceAsync(conversationId, cancellationToken).ConfigureAwait(false);
                    ApplyEoCToTurnContextActivity(turnContext, activity);
                    await _bot.OnTurnAsync(turnContext, ct).ConfigureAwait(false);
                    break;

                case ActivityTypes.Event:
                    ApplyEventToTurnContextActivity(turnContext, activity);
                    await _bot.OnTurnAsync(turnContext, ct).ConfigureAwait(false);
                    break;

                default:
                    resourceResponse = await turnContext.SendActivityAsync(activity, cancellationToken).ConfigureAwait(false);
                    break;
                }
            });

            await _adapter.ContinueConversationAsync(claimsIdentity, skillConversationReference.ConversationReference, skillConversationReference.OAuthScope, callback, cancellationToken).ConfigureAwait(false);

            return(resourceResponse ?? new ResourceResponse(Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture)));
        }
Exemple #3
0
        /// <summary>
        /// Creates a new <see cref="SkillConversationReference"/>.
        /// </summary>
        /// <param name="options">Creation options to use when creating the <see cref="SkillConversationReference"/>.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>ID of the created <see cref="SkillConversationReference"/>.</returns>
        public override async Task <string> CreateSkillConversationIdAsync(
            SkillConversationIdFactoryOptions options,
            CancellationToken cancellationToken)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            // Create the storage key based on the SkillConversationIdFactoryOptions.
            var conversationReference = options.Activity.GetConversationReference();

            var skillConversationId = Guid.NewGuid().ToString();

            // Create the SkillConversationReference instance.
            var skillConversationReference = new SkillConversationReference
            {
                ConversationReference = conversationReference,
                OAuthScope            = options.FromBotOAuthScope
            };

            // Store the SkillConversationReference using the skillConversationId as a key.
            var skillConversationInfo = new Dictionary <string, object>
            {
                {
                    skillConversationId, JObject.FromObject(skillConversationReference)
                }
            };

            await _storage.WriteAsync(skillConversationInfo, cancellationToken).ConfigureAwait(false);

            // Return the generated skillConversationId (that will be also used as the conversation ID to call the skill).
            return(skillConversationId);
        }