Esempio n. 1
0
        /// <summary>
        /// Notifies both the conversation owner (agent) and the conversation client (customer)
        /// about the connection status change.
        /// </summary>
        /// <param name="messageRouterResult">The result to handle.</param>
        protected virtual async Task HandleConnectionChangedResultAsync(MessageRouterResult messageRouterResult)
        {
            IRoutingDataManager routingDataManager = _messageRouterManager.RoutingDataManager;

            Party conversationOwnerParty  = messageRouterResult.ConversationOwnerParty;
            Party conversationClientParty = messageRouterResult.ConversationClientParty;

            string conversationOwnerName =
                string.IsNullOrEmpty(conversationOwnerParty?.ChannelAccount.Name)
                    ? StringAndCharConstants.NoUserNamePlaceholder
                    : conversationOwnerParty?.ChannelAccount.Name;

            string conversationClientName =
                string.IsNullOrEmpty(conversationClientParty?.ChannelAccount.Name)
                    ? StringAndCharConstants.NoUserNamePlaceholder
                    : conversationClientParty?.ChannelAccount.Name;

            string messageToConversationOwner  = string.Empty;
            string messageToConversationClient = string.Empty;

            if (messageRouterResult.Type == MessageRouterResultType.ConnectionRequested)
            {
                bool conversationClientPartyMissing =
                    (conversationClientParty == null || conversationClientParty.ChannelAccount == null);

                foreach (Party aggregationParty in _messageRouterManager.RoutingDataManager.GetAggregationParties())
                {
                    Party botParty = routingDataManager.FindBotPartyByChannelAndConversation(
                        aggregationParty.ChannelId, aggregationParty.ConversationAccount);

                    if (botParty != null)
                    {
                        if (conversationClientPartyMissing)
                        {
                            await _messageRouterManager.SendMessageToPartyByBotAsync(
                                aggregationParty, ConversationText.RequestorDetailsMissing);
                        }
                        else
                        {
                            IMessageActivity messageActivity = Activity.CreateMessageActivity();
                            messageActivity.Conversation = aggregationParty.ConversationAccount;
                            messageActivity.Recipient    = aggregationParty.ChannelAccount;
                            messageActivity.Attachments  = new List <Attachment>
                            {
                                CommandCardFactory.CreateRequestCard(
                                    conversationClientParty, botParty.ChannelAccount?.Name).ToAttachment()
                            };

                            await _messageRouterManager.SendMessageToPartyByBotAsync(
                                aggregationParty, messageActivity);
                        }
                    }
                }

                if (!conversationClientPartyMissing)
                {
                    messageToConversationClient = ConversationText.NotifyClientWaitForRequestHandling;
                }
            }
            else if (messageRouterResult.Type == MessageRouterResultType.ConnectionAlreadyRequested)
            {
                messageToConversationClient = ConversationText.NotifyClientDuplicateRequest;
            }
            else if (messageRouterResult.Type == MessageRouterResultType.ConnectionRejected)
            {
                messageToConversationOwner  = string.Format(ConversationText.NotifyOwnerRequestRejected, conversationClientName);
                messageToConversationClient = ConversationText.NotifyClientRequestRejected;
            }
            else if (messageRouterResult.Type == MessageRouterResultType.Connected)
            {
                messageToConversationOwner  = string.Format(ConversationText.NotifyOwnerConnected, conversationClientName);
                messageToConversationClient = string.Format(ConversationText.NotifyClientConnected, conversationOwnerName);
            }
            else if (messageRouterResult.Type == MessageRouterResultType.Disconnected)
            {
                messageToConversationOwner  = string.Format(ConversationText.NotifyOwnerDisconnected, conversationClientName);
                messageToConversationClient = string.Format(ConversationText.NotifyClientDisconnected, conversationOwnerName);
            }

            if (conversationOwnerParty != null &&
                !string.IsNullOrEmpty(messageToConversationOwner))
            {
                await _messageRouterManager.SendMessageToPartyByBotAsync(
                    conversationOwnerParty, messageToConversationOwner);
            }

            if (conversationClientParty != null &&
                !string.IsNullOrEmpty(messageToConversationClient))
            {
                await _messageRouterManager.SendMessageToPartyByBotAsync(
                    conversationClientParty, messageToConversationClient);
            }
        }
        /// <summary>
        /// Notifies both the conversation owner (agent) and the conversation client (customer)
        /// about the connection status change.
        /// </summary>
        /// <param name="messageRouterResult">The result to handle.</param>
        protected virtual async Task HandleConnectionChangedResultAsync(MessageRouterResult messageRouterResult)
        {
            IRoutingDataManager routingDataManager = _messageRouterManager.RoutingDataManager;

            Party conversationOwnerParty  = messageRouterResult.ConversationOwnerParty;
            Party conversationClientParty = messageRouterResult.ConversationClientParty;

            string conversationOwnerName  = conversationOwnerParty?.ChannelAccount.Name;
            string conversationClientName = conversationClientParty?.ChannelAccount.Name;

            string messageToConversationOwner  = string.Empty;
            string messageToConversationClient = string.Empty;

            if (messageRouterResult.Type == MessageRouterResultType.ConnectionRequested)
            {
                if (conversationClientParty == null || conversationClientParty.ChannelAccount == null)
                {
                    await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
                        ConversationText.ConnectionRequestMadeButRequestorIsNull);

                    throw new NullReferenceException(ConversationText.ConnectionRequestMadeButRequestorIsNull);
                }

                foreach (Party aggregationParty in _messageRouterManager.RoutingDataManager.GetAggregationParties())
                {
                    Party botParty = routingDataManager
                                     .FindBotPartyByChannelAndConversation(aggregationParty.ChannelId, aggregationParty.ConversationAccount);

                    if (botParty != null)
                    {
                        IMessageActivity messageActivity = Activity.CreateMessageActivity();
                        messageActivity.Conversation = aggregationParty.ConversationAccount;
                        messageActivity.Recipient    = aggregationParty.ChannelAccount;
                        messageActivity.Attachments  = new List <Attachment>
                        {
                            CommandCardFactory.CreateRequestCard(
                                conversationClientParty, botParty.ChannelAccount?.Name).ToAttachment()
                        };

                        try
                        {
                            await _messageRouterManager.SendMessageToPartyByBotAsync(aggregationParty, messageActivity);
                        }
                        catch (UnauthorizedAccessException e)
                        {
                            System.Diagnostics.Debug.WriteLine($"Failed to broadcast message: {e.Message}");
                        }
                    }
                    else
                    {
                        try
                        {
                            await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
                                string.Format(
                                    ConversationText.FailedToFindBotOnAggregationChannel,
                                    aggregationParty.ConversationAccount.Name));
                        }
                        catch (UnauthorizedAccessException e)
                        {
                            System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
                        }
                    }
                }

                messageToConversationClient = ConversationText.NotifyClientWaitForRequestHandling;
            }
            else if (messageRouterResult.Type == MessageRouterResultType.ConnectionAlreadyRequested)
            {
                messageToConversationClient = ConversationText.NotifyClientDuplicateRequest;
            }
            else if (messageRouterResult.Type == MessageRouterResultType.ConnectionRejected)
            {
                messageToConversationOwner  = string.Format(ConversationText.NotifyOwnerRequestRejected, conversationClientName);
                messageToConversationClient = ConversationText.NotifyClientRequestRejected;
            }
            else if (messageRouterResult.Type == MessageRouterResultType.Connected)
            {
                messageToConversationOwner  = string.Format(ConversationText.NotifyOwnerConnected, conversationClientName);
                messageToConversationClient = string.Format(ConversationText.NotifyClientConnected, conversationOwnerName);
            }
            else if (messageRouterResult.Type == MessageRouterResultType.Disconnected)
            {
                messageToConversationOwner  = string.Format(ConversationText.NotifyOwnerDisconnected, conversationClientName);
                messageToConversationClient = string.Format(ConversationText.NotifyClientDisconnected, conversationOwnerName);
            }

            if (!string.IsNullOrEmpty(messageToConversationOwner) && conversationOwnerParty != null)
            {
                try
                {
                    await _messageRouterManager.SendMessageToPartyByBotAsync(
                        conversationOwnerParty, messageToConversationOwner);
                }
                catch (UnauthorizedAccessException e)
                {
                    System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
                }
            }

            if (!string.IsNullOrEmpty(messageToConversationClient) && conversationClientParty != null)
            {
                try
                {
                    await _messageRouterManager.SendMessageToPartyByBotAsync(
                        conversationClientParty, messageToConversationClient);
                }
                catch (UnauthorizedAccessException e)
                {
                    System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
                }
            }
        }