/// <summary> /// Ends the engagement where the given party is the conversation owner /// (e.g. a customer service agent). /// </summary> /// <param name="conversationOwnerParty">The owner of the engagement (conversation).</param> /// <returns>The results. If the number of results is more than 0, the operation was successful.</returns> public List <MessageRouterResult> EndEngagement(Party conversationOwnerParty) { List <MessageRouterResult> messageRouterResults = new List <MessageRouterResult>(); Party ownerInConversation = RoutingDataManager.FindEngagedPartyByChannel( conversationOwnerParty.ChannelId, conversationOwnerParty.ChannelAccount); if (ownerInConversation != null && RoutingDataManager.IsEngaged(ownerInConversation, EngagementProfile.Owner)) { Party otherParty = RoutingDataManager.GetEngagedCounterpart(ownerInConversation); messageRouterResults.AddRange( RoutingDataManager.RemoveEngagement(ownerInConversation, EngagementProfile.Owner)); } else { messageRouterResults.Add(new MessageRouterResult() { Type = MessageRouterResultType.Error, ConversationOwnerParty = conversationOwnerParty, ErrorMessage = "No conversation to close found" }); } return(messageRouterResults); }
/// <summary> /// Handles the incoming message activities. For instance, if it is a message from party /// engaged in a chat, the message will be forwarded to the counterpart in whatever /// channel that party is on. /// </summary> /// <param name="activity">The activity to handle.</param> /// <param name="addClientNameToMessage">If true, will add the client's name to the beginning of the message.</param> /// <param name="addOwnerNameToMessage">If true, will add the owner's (agent) name to the beginning of the message.</param> /// <returns>The result of the operation.</returns> public async Task <MessageRouterResult> HandleMessageAsync( Activity activity, bool addClientNameToMessage = true, bool addOwnerNameToMessage = false) { MessageRouterResult result = new MessageRouterResult() { Type = MessageRouterResultType.NoActionTaken, Activity = activity }; Party senderParty = MessagingUtils.CreateSenderParty(activity); if (RoutingDataManager.IsEngaged(senderParty, EngagementProfile.Owner)) { // Sender is an owner of an ongoing conversation - forward the message result.ConversationOwnerParty = senderParty; Party partyToForwardMessageTo = RoutingDataManager.GetEngagedCounterpart(senderParty); if (partyToForwardMessageTo != null) { result.ConversationClientParty = partyToForwardMessageTo; string message = addOwnerNameToMessage ? $"{senderParty.ChannelAccount.Name}: {activity.Text}" : activity.Text; ResourceResponse resourceResponse = await SendMessageToPartyByBotAsync(partyToForwardMessageTo, activity.Text); if (resourceResponse != null) { result.Type = MessageRouterResultType.OK; } else { result.Type = MessageRouterResultType.FailedToForwardMessage; result.ErrorMessage = $"Failed to forward the message to user {partyToForwardMessageTo}"; } } else { result.Type = MessageRouterResultType.FailedToForwardMessage; result.ErrorMessage = "Failed to find the party to forward the message to"; } } else if (RoutingDataManager.IsEngaged(senderParty, EngagementProfile.Client)) { // Sender is a participant of an ongoing conversation - forward the message result.ConversationClientParty = senderParty; Party partyToForwardMessageTo = RoutingDataManager.GetEngagedCounterpart(senderParty); if (partyToForwardMessageTo != null) { result.ConversationOwnerParty = partyToForwardMessageTo; string message = addClientNameToMessage ? $"{senderParty.ChannelAccount.Name}: {activity.Text}" : activity.Text; await SendMessageToPartyByBotAsync(partyToForwardMessageTo, message); result.Type = MessageRouterResultType.OK; } else { result.Type = MessageRouterResultType.FailedToForwardMessage; result.ErrorMessage = "Failed to find the party to forward the message to"; } } return(result); }