private async Task ListenTo(string json) { JObject jObject = JObject.Parse(json); if (jObject["type"].Value <string>() == "message") { string channelID = jObject["channel"].Value <string>(); SlackChatHub hub = null; if (ConnectedHubs.ContainsKey(channelID)) { hub = ConnectedHubs[channelID]; } else { hub = SlackChatHub.FromID(channelID); List <SlackChatHub> hubs = new List <SlackChatHub>(); hubs.AddRange(ConnectedHubs.Values); hubs.Add(hub); } string messageText = (jObject["text"] != null ? jObject["text"].Value <string>() : null); // check to see if bot has been mentioned SlackMessage message = new SlackMessage() { ChatHub = hub, MentionsBot = (messageText != null ? Regex.IsMatch(messageText, BotNameRegex, RegexOptions.IgnoreCase) : false), RawData = json, // some messages may not have text or a user (like unfurled data from URLs) Text = messageText, User = (jObject["user"] != null ? new SlackUser() { ID = jObject["user"].Value <string>() } : null) }; ResponseContext context = new ResponseContext() { BotHasResponded = false, BotUserID = UserID, BotUserName = UserName, Message = message, TeamID = this.TeamID, UserNameCache = new ReadOnlyDictionary <string, string>(this.UserNameCache) }; if (StaticResponseContextData != null) { foreach (string key in StaticResponseContextData.Keys) { context.Set(key, StaticResponseContextData[key]); } } // margie can never respond to herself and requires that the message have text and be from an actual person if (message.User != null && message.User.ID != UserID && message.Text != null) { foreach (IResponseProcessor processor in ResponseProcessors) { if (processor.CanRespond(context)) { await Say(processor.GetResponse(context), context); context.BotHasResponded = true; } } } } RaiseMessageReceived(json); }
private async Task ListenTo(string json) { bool isValidJson = true; JObject jObject = null; try { jObject = JObject.Parse(json); } catch (JsonReaderException) { isValidJson = false; #if DEBUG Console.WriteLine($"Illegal JSON message: {json}"); #endif } if (!isValidJson) { return; } if (jObject["type"].Value <string>() == "message") { var channelID = jObject["channel"].Value <string>(); SlackChatHub hub = null; if (ConnectedHubs.ContainsKey(channelID)) { hub = ConnectedHubs[channelID]; } else { hub = SlackChatHub.FromID(channelID); var hubs = new Dictionary <string, SlackChatHub>(ConnectedHubs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); hubs.Add(hub.ID, hub); ConnectedHubs = hubs; } // some messages may not have text or a user (like unfurled data from URLs) var messageText = (jObject["text"] != null ? jObject["text"].Value <string>() : null); SlackMessage message = new SlackMessage() { ChatHub = hub, // check to see if bot has been mentioned MentionsBot = (messageText != null ? Regex.IsMatch(messageText, BotNameRegex, RegexOptions.IgnoreCase) : false), RawData = json, Text = messageText, User = (jObject["user"] != null ? new SlackUser() { ID = jObject["user"].Value <string>() } : null) }; ResponseContext context = new ResponseContext() { BotHasResponded = false, BotUserID = UserID, BotUserName = UserName, Message = message, TeamID = TeamID, UserNameCache = new ReadOnlyDictionary <string, string>(UserNameCache) }; // if the end dev has added any static entries to the ResponseContext collection of Bot, add them to the context being passed to the responders. if (ResponseContext != null) { foreach (string key in ResponseContext.Keys) { context.Set(key, ResponseContext[key]); } } // margie can never respond to herself and requires that the message have text and be from an actual person if (message.User != null && message.User.ID != UserID && message.Text != null) { foreach (var responder in Responders ?? Enumerable.Empty <IResponder>()) { if (responder != null && responder.CanRespond(context)) { await SendIsTyping(message.ChatHub); await Say(responder.GetResponse(context), context); context.BotHasResponded = true; } } } } RaiseMessageReceived(json); }
private async Task ListenTo(string json) { JObject jObject = JObject.Parse(json); if (jObject["type"].Value <string>() == "message") { string channelID = jObject["channel"].Value <string>(); SlackChatHub hub = null; if (ConnectedHubs.ContainsKey(channelID)) { hub = ConnectedHubs[channelID]; } else { hub = SlackChatHub.FromID(channelID); Dictionary <string, SlackChatHub> hubs = new Dictionary <string, SlackChatHub>(ConnectedHubs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); hubs.Add(hub.ID, hub); ConnectedHubs = hubs; } string messageText = (jObject["text"] != null ? jObject["text"].Value <string>() : null); // check to see if bot has been mentioned SlackMessageContent message = new SlackMessageContent() { ChatHub = hub, MentionsBot = (messageText != null ? Regex.IsMatch(messageText, BotNameRegex, RegexOptions.IgnoreCase) : false), RawData = json, // some messages may not have text or a user (like unfurled data from URLs) Text = messageText, User = (jObject["user"] != null ? new SlackUser() { ID = jObject["user"].Value <string>() } : null) }; SlackMessage context = new SlackMessage() { Creator = this, BotHasResponded = false, BotUserID = UserID, BotUserName = UserName, Message = message, TeamID = this.TeamID, UserNameCache = new ReadOnlyDictionary <string, string>(this.UserNameCache) }; context.Answer = async(arg) => { await this.Say(arg, context); context.BotHasResponded = true; }; // if the end dev has added any static entries to the ResponseContext collection of Bot, add them to the context being passed to the responders. if (ResponseContext != null) { foreach (string key in ResponseContext.Keys) { context.Set(key, ResponseContext[key]); } } // margie can never respond to herself and requires that the message have text and be from an actual person if (message.User != null && message.User.ID != UserID && message.Text != null) { foreach (var obs in observers) { obs.OnNext(context); } } } RaiseMessageReceived(json); }