/// <summary> /// Constructor. /// </summary> /// <param name="messageRouterManager">The message router manager.</param> /// <param name="messageRouterResultHandler"/>A MessageRouterResultHandler instance for /// handling possible routing actions such as accepting a 1:1 conversation connection.</param> public CommandMessageHandler(MessageRouterManager messageRouterManager, IMessageRouterResultHandler messageRouterResultHandler) { _messageRouterManager = messageRouterManager; _messageRouterResultHandler = messageRouterResultHandler; }
/// <summary> /// Handles the received message. /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { if (activity.Locale != null) { ConversationText.Culture = new CultureInfo(activity.Locale); } if (activity.Type == ActivityTypes.Message) { MessageRouterManager messageRouterManager = WebApiConfig.MessageRouterManager; IMessageRouterResultHandler messageRouterResultHandler = WebApiConfig.MessageRouterResultHandler; messageRouterManager.MakeSurePartiesAreTracked(activity); // First check for commands (both from back channel and the ones directly typed) MessageRouterResult messageRouterResult = WebApiConfig.BackChannelMessageHandler.HandleBackChannelMessage(activity); if (messageRouterResult.Type != MessageRouterResultType.Connected && await WebApiConfig.CommandMessageHandler.HandleCommandAsync(activity) == false) { // No valid back channel (command) message or typed command detected // Let the message router manager instance handle the activity messageRouterResult = await messageRouterManager.HandleActivityAsync(activity, false); if (messageRouterResult.Type == MessageRouterResultType.NoActionTaken) { // No action was taken by the message router manager. This means that the // user is not connected (in a 1:1 conversation) with a human // (e.g. customer service agent) yet. // // You can, for example, check if the user (customer) needs human // assistance here or forward the activity to a dialog. You could also do // the check in the dialog too... // // Here's an example: if (!string.IsNullOrEmpty(activity.Text) && activity.Text.ToLower().Contains(CommandRequestConnection)) { messageRouterResult = messageRouterManager.RequestConnection(activity); } else { await Conversation.SendAsync(activity, () => new RootDialog()); } } } // Handle the result, if required await messageRouterResultHandler.HandleResultAsync(messageRouterResult); } else { await HandleSystemMessageAsync(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }
/// <summary> /// Handles the received message. /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { //await Repository.UtilityRepo.LogMsgAsync("activity id" + activity.From.Id); //await Repository.UtilityRepo.LogMsgAsync("activity from" + activity.From.Name); //await Repository.UtilityRepo.LogMsgAsync("activity channel" + activity.ChannelId); // var t = System.Web.HttpContext.Current.Request.UserHostAddress; //var CallerIp = System.Web.HttpContext.Current.Request.UserHostAddress; var CallerAgent = System.Web.HttpContext.Current.Request.UserAgent; //var CalledUrl = System.Web.HttpContext.Current.Request.Url.OriginalString; var current = System.Web.HttpContext.Current; var ip = GetUserIP(current); await Repository.UtilityRepo.UpdatedUserAttendedByAsync(activity); if (activity.Locale != null) { ConversationText.Culture = new CultureInfo(activity.Locale); } if (activity.Type == ActivityTypes.Message) { MessageRouterManager messageRouterManager = WebApiConfig.MessageRouterManager; IMessageRouterResultHandler messageRouterResultHandler = WebApiConfig.MessageRouterResultHandler; messageRouterManager.MakeSurePartiesAreTracked(activity); // First check for commands (both from back channel and the ones directly typed) MessageRouterResult messageRouterResult = WebApiConfig.BackChannelMessageHandler.HandleBackChannelMessage(activity); if (messageRouterResult.Type != MessageRouterResultType.Connected && await WebApiConfig.CommandMessageHandler.HandleCommandAsync(activity) == false) { // No valid back channel (command) message or typed command detected // Let the message router manager instance handle the activity messageRouterResult = await messageRouterManager.HandleActivityAsync(activity, false); if (messageRouterResult.Type == MessageRouterResultType.NoActionTaken) { // No action was taken by the message router manager. This means that the // user is not connected (in a 1:1 conversation) with a human // (e.g. customer service agent) yet. // // You can, for example, check if the user (customer) needs human // assistance here or forward the activity to a dialog. You could also do // the check in the dialog too... // // Here's an example: if (!string.IsNullOrEmpty(activity.Text) && activity.Text.ToLower().Contains(CommandRequestConnection)) //&& System.Web.HttpContext.Current.Session["UserID"] != null) { messageRouterResult = messageRouterManager.RequestConnection(activity); // log all the request and thier sources try { await Repository.UtilityRepo.LogPendingRequestAsync(activity, ip, CallerAgent); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { await Repository.UtilityRepo.LogMsgAsync(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { await Repository.UtilityRepo.LogMsgAsync(string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage)); } } //throw; } catch (Exception ex) { await Repository.UtilityRepo.LogMsgAsync("Eror on human request : " + ex.Message); } } else { try { await Repository.UtilityRepo.LogRequestMessageAsync(activity); // Call await Conversation.SendAsync(activity, () => new RootDialog()); } catch (FormCanceledException fcEx) when(fcEx.InnerException is TooManyAttemptsException) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); Activity reply = activity.CreateReply( $"Too Many Attempts at {fcEx.Last}. " + $"Completed Steps: {string.Join(", ", fcEx.Completed)}"); await Repository.UtilityRepo.LogMsgAsync("reply : " + reply.Text); await connector.Conversations.ReplyToActivityAsync(reply); } catch (FormCanceledException fcEx) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); Activity reply = activity.CreateReply( $"Form cancelled at {fcEx.Last}. " + $"Completed Steps: {string.Join(", ", fcEx.Completed)}"); await Repository.UtilityRepo.LogMsgAsync("reply : " + reply.Text); await connector.Conversations.ReplyToActivityAsync(reply); } catch (Exception ex) { await Repository.UtilityRepo.LogErrorAsync(ex); } } } } if (messageRouterResult != null && messageRouterResult.Type == MessageRouterResultType.OK && string.IsNullOrEmpty(messageRouterResult.ErrorMessage)) { await Repository.UtilityRepo.CustomerAgentChatHistoryLogAsync(messageRouterResult); } // Handle the result, if required await messageRouterResultHandler.HandleResultAsync(messageRouterResult); } else { await HandleSystemMessageAsync(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }