Exemple #1
0
 /// <summary>
 /// Checks if the given connection matches this one.
 /// </summary>
 /// <param name="other">The other connection.</param>
 /// <returns>True, if the connections are match. False otherwise.</returns>
 public bool Equals(Connection other)
 {
     return(other != null &&
            ((RoutingDataManager.Match(ConversationReference1, other.ConversationReference1) &&
              RoutingDataManager.Match(ConversationReference2, other.ConversationReference2)) ||
             (RoutingDataManager.Match(ConversationReference1, other.ConversationReference2) &&
              RoutingDataManager.Match(ConversationReference2, other.ConversationReference1))));
 }
Exemple #2
0
        /// <summary>
        /// Routes the message in the given activity, if the sender is connected in a conversation.
        /// </summary>
        /// <param name="activity">The activity to handle.</param>
        /// <param name="addNameToMessage">If true, will add the name of the sender to the beginning of the message.</param>
        /// <returns>The result of the operation:
        /// - MessageRouterResultType.NoActionTaken, if no routing rule for the sender is found OR
        /// - MessageRouterResultType.OK, if the message was routed successfully OR
        /// - MessageRouterResultType.FailedToForwardMessage in case of an error (see the error message).
        /// </returns>
        public virtual async Task <MessageRoutingResult> RouteMessageIfSenderIsConnectedAsync(
            IMessageActivity activity, bool addNameToMessage = true)
        {
            ConversationReference sender     = CreateSenderConversationReference(activity);
            Connection            connection = RoutingDataManager.FindConnection(sender);

            MessageRoutingResult messageRoutingResult = new MessageRoutingResult()
            {
                Type       = MessageRoutingResultType.NoActionTaken,
                Connection = connection
            };

            if (connection != null)
            {
                ConversationReference recipient =
                    RoutingDataManager.Match(sender, connection.ConversationReference1)
                        ? connection.ConversationReference2 : connection.ConversationReference1;

                if (recipient != null)
                {
                    string message = activity.Text;

                    if (addNameToMessage)
                    {
                        string senderName = RoutingDataManager.GetChannelAccount(sender).Name;

                        if (!string.IsNullOrWhiteSpace(senderName))
                        {
                            message = $"{senderName}: {message}";
                        }
                    }

                    ResourceResponse resourceResponse = await SendMessageAsync(recipient, message);

                    if (resourceResponse != null)
                    {
                        messageRoutingResult.Type = MessageRoutingResultType.MessageRouted;

                        if (!RoutingDataManager.UpdateTimeSinceLastActivity(connection))
                        {
                            Logger.Log("Failed to update the time since the last activity property of the connection");
                        }
                    }
                    else
                    {
                        messageRoutingResult.Type         = MessageRoutingResultType.FailedToRouteMessage;
                        messageRoutingResult.ErrorMessage = $"Failed to forward the message to the recipient";
                    }
                }
                else
                {
                    messageRoutingResult.Type         = MessageRoutingResultType.Error;
                    messageRoutingResult.ErrorMessage = "Failed to find the recipient to forward the message to";
                }
            }

            return(messageRoutingResult);
        }
Exemple #3
0
        /// <summary>
        /// Finds the message log associated with the given user.
        /// </summary>
        /// <param name="user">The user whose message log to find.</param>
        /// <returns>The message log of the user or null, if not found.</returns>
        public MessageLog GetMessageLog(ConversationReference user)
        {
            var messageLogs = GetMessageLogs();

            foreach (MessageLog messageLog in messageLogs)
            {
                if (RoutingDataManager.Match(user, messageLog.User))
                {
                    return(messageLog);
                }
            }
            return(null);
        }
        public bool RemoveConversationReference(ConversationReference conversationReferenceToRemove)
        {
            if (conversationReferenceToRemove.User != null)
            {
                return((Users as List <ConversationReference>)
                       .RemoveAll(conversationReference =>
                                  RoutingDataManager.Match(conversationReference, conversationReferenceToRemove)) > 0);
            }

            if (conversationReferenceToRemove.Bot != null)
            {
                return((BotInstances as List <ConversationReference>)
                       .RemoveAll(conversationReference =>
                                  RoutingDataManager.Match(conversationReference, conversationReferenceToRemove)) > 0);
            }

            return(false);
        }
 public bool RemoveAggregationChannel(ConversationReference aggregationConversationReferenceToRemove)
 {
     return((AggregationChannels as List <ConversationReference>)
            .RemoveAll(conversationReference =>
                       RoutingDataManager.Match(conversationReference, aggregationConversationReferenceToRemove)) > 0);
 }
Exemple #6
0
        /// <summary>
        /// Tries to accept/reject a pending connection request.
        /// </summary>
        /// <param name="messageRouter">The message router.</param>
        /// <param name="messageRouterResultHandler">The message router result handler.</param>
        /// <param name="sender">The sender party (accepter/rejecter).</param>
        /// <param name="doAccept">If true, will try to accept the request. If false, will reject.</param>
        /// <param name="requestorChannelAccountId">The channel account ID of the user/bot whose request to accept/reject.</param>
        /// <param name="requestorConversationAccountId">The conversation account ID of the user/bot whose request to accept/reject.</param>
        /// <returns>The result.</returns>
        public async Task <AbstractMessageRouterResult> AcceptOrRejectRequestAsync(
            MessageRouter messageRouter, MessageRouterResultHandler messageRouterResultHandler,
            ConversationReference sender, bool doAccept,
            ChannelAccount requestorChannelAccountId, ConversationAccount requestorConversationAccountId)
        {
            AbstractMessageRouterResult messageRouterResult = new ConnectionRequestResult()
            {
                Type = ConnectionRequestResultType.Error
            };

            ConversationReference requestor =
                new ConversationReference(
                    null, requestorChannelAccountId, null, requestorConversationAccountId);

            ConnectionRequest connectionRequest =
                messageRouter.RoutingDataManager.FindConnectionRequest(requestor);

            if (connectionRequest == null)
            {
                // Try bot
                requestor.Bot  = requestor.User;
                requestor.User = null;

                connectionRequest =
                    messageRouter.RoutingDataManager.FindConnectionRequest(requestor);
            }

            if (connectionRequest != null)
            {
                Connection connection = null;

                if (sender != null)
                {
                    connection = messageRouter.RoutingDataManager.FindConnection(sender);
                }

                ConversationReference senderInConnection = null;
                ConversationReference counterpart        = null;

                if (connection != null && connection.ConversationReference1 != null)
                {
                    if (RoutingDataManager.Match(sender, connection.ConversationReference1))
                    {
                        senderInConnection = connection.ConversationReference1;
                        counterpart        = connection.ConversationReference2;
                    }
                    else
                    {
                        senderInConnection = connection.ConversationReference2;
                        counterpart        = connection.ConversationReference1;
                    }
                }

                if (doAccept)
                {
                    if (senderInConnection != null)
                    {
                        // The sender (accepter/rejecter) is ALREADY connected to another party
                        if (counterpart != null)
                        {
                            messageRouterResult.ErrorMessage = string.Format(
                                Strings.AlreadyConnectedWithUser,
                                RoutingDataManager.GetChannelAccount(counterpart)?.Name);
                        }
                        else
                        {
                            messageRouterResult.ErrorMessage = Strings.ErrorOccured;
                        }
                    }
                    else
                    {
                        bool createNewDirectConversation =
                            (_noDirectConversationsWithChannels == null ||
                             !(_noDirectConversationsWithChannels.Contains(sender.ChannelId.ToLower())));

                        // Try to accept
                        messageRouterResult = await messageRouter.ConnectAsync(
                            sender,
                            connectionRequest.Requestor,
                            createNewDirectConversation);
                    }
                }
                else
                {
                    // Note: Rejecting is OK even if the sender is alreay connected
                    messageRouterResult = messageRouter.RejectConnectionRequest(connectionRequest.Requestor, sender);
                }
            }
            else
            {
                messageRouterResult.ErrorMessage = Strings.FailedToFindPendingRequest;
            }

            return(messageRouterResult);
        }
Exemple #7
0
 public bool Equals(ConnectionRequest other)
 {
     return(other != null &&
            RoutingDataManager.Match(Requestor, other.Requestor));
 }