コード例 #1
0
        /// <summary>
        /// Handles the given message routing result.
        /// </summary>
        /// <param name="messageRoutingResult">The result to handle.</param>
        /// <returns>True, if the result was handled. False, if no action was taken.</returns>
        protected virtual async Task <bool> HandleMessageRoutingResultAsync(
            MessageRoutingResult messageRoutingResult)
        {
            ConversationReference agent = messageRoutingResult?.Connection?.ConversationReference1;

            switch (messageRoutingResult.Type)
            {
            case MessageRoutingResultType.NoActionTaken:
            case MessageRoutingResultType.MessageRouted:
                // No need to do anything
                break;

            case MessageRoutingResultType.FailedToRouteMessage:
            case MessageRoutingResultType.Error:
                if (agent != null)
                {
                    string errorMessage = string.IsNullOrWhiteSpace(messageRoutingResult.ErrorMessage)
                            ? Strings.FailedToForwardMessage
                            : messageRoutingResult.ErrorMessage;


                    await _messageRouter.SendMessageAsync(agent, errorMessage);
                }

                return(true);

            default:
                break;
            }

            return(false);
        }
コード例 #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);
        }
コード例 #3
0
        public static async Task <MessageRoutingResult> RouteReachMessageIfSenderIsConnectedAsync(this MessageRouter messageRouter, IMessageActivity activity, bool addNameToMessage = true)
        {
            var RoutingDataManager           = messageRouter.RoutingDataManager;
            ConversationReference sender     = MessageRouter.CreateSenderConversationReference(activity);
            Connection            connection = RoutingDataManager.FindConnection(sender);

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

            if (connection != null)
            {
                ConversationReference recipient =
                    RoutingDataManagerS.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}";
                    //    }
                    //}

                    var str         = JsonConvert.SerializeObject(activity);
                    var newActivity = JsonConvert.DeserializeObject <Microsoft.Bot.Schema.Activity>(str);
                    newActivity.From = null;
                    if (recipient.Conversation != null)
                    {
                        newActivity.Conversation = recipient.Conversation;
                    }
                    ChannelAccount recipientChannelAccount = RoutingDataManagerS.GetChannelAccount(recipient);
                    if (recipientChannelAccount != null)
                    {
                        newActivity.Recipient = recipientChannelAccount;
                    }

                    ResourceResponse resourceResponse = await messageRouter.SendMessageAsync(recipient, newActivity);

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

                        if (!RoutingDataManager.UpdateTimeSinceLastActivity(connection))
                        {
                            messageRouter.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);
        }