public async Task Process(IncomingMessage incomingMessage) { Log.Debug("Processing {@incomingMessage}", incomingMessage); if (incomingMessage == null) { throw new ArgumentNullException("incomingMessage"); } var values = incomingMessage.Text.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var botName = incomingMessage.IsSlashCommand() ? incomingMessage.Command.Substring(1) : values[0].ToLower().Replace(":", ""); incomingMessage.BotName = botName; var command = values[incomingMessage.Command == null ? 1 : 0].ToLower(); incomingMessage.Text = string.Join(" ", values.Skip(incomingMessage.IsSlashCommand() ? 0 : 1)); Exception exception = null; try { foreach (var handler in _messageHandlers) { using (var session = _documentStore.OpenSession()) { var handlerType = handler.GetType().FullName; var company = SlackContext.Current.Company; var models = session.Query <ViewBagModel>(); var model = models.FirstOrDefault(c => c.Company == company && c.HandlerName == handlerType) ?? new ViewBagModel(company, handlerType); handler.Brain = model.ViewBag; await handler.Handle(incomingMessage); session.Store(model); session.SaveChanges(); } } } catch (Exception ex) { exception = ex; } if (exception != null) { var response = string.Format("@{0} Umm, something went wrong \"{1} {2}\" {3}", incomingMessage.UserName, command, incomingMessage.Text, exception.Message); await _client.SendAsync(incomingMessage.ReplyTo(), response); } }