private async Task MessageReceived(SlackMessage message) { Console.WriteLine("[[[Message started]]]"); IMiddleware pipeline = _pipelineFactory.GetPipeline(); var incomingMessage = new IncomingMessage { RawText = message.Text, FullText = message.Text, UserId = message.User.Id, Username = GetUsername(message), Channel = message.ChatHub.Id, UserChannel = await GetUserChannel(message), BotName = _client.UserName, BotId = _client.UserId, BotIsMentioned = message.MentionsBot }; incomingMessage.TargetedText = incomingMessage.GetTargetedText(); try { foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage)) { await SendMessage(responseMessage); } } catch (Exception ex) { Console.WriteLine("ERROR: {0}", ex); } Console.WriteLine("[[[Message ended]]]"); }
private IEnumerable<ResponseMessage> ListPingHandler(IncomingMessage message, string matchedHandle) { string[] users = _pingPlugin.ListPingedUsers(); yield return message.ReplyDirectlyToUser("I am currently pinging:"); yield return message.ReplyDirectlyToUser(">>>" + string.Join("\n", users)); }
public virtual IEnumerable<ResponseMessage> Invoke(IncomingMessage message) { foreach (var handlerMapping in HandlerMappings) { foreach (string map in handlerMapping.ValidHandles) { string text = message.FullText; if (handlerMapping.MessageShouldTargetBot) { text = message.TargetedText; } if (text.StartsWith(map, StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine($"Matched '{map}' on '{this.GetType().Name}'"); foreach (var responseMessage in handlerMapping.EvaluatorFunc(message, map)) { yield return responseMessage; } if (!handlerMapping.ShouldContinueProcessing) { yield break; } } } } foreach (ResponseMessage responseMessage in Next(message)) { yield return responseMessage; } }
private IEnumerable<ResponseMessage> CalculateHandler(IncomingMessage message, string matchedHandle) { string response = string.Empty; if (matchedHandle != null) { string expression = message.FullText.Substring(matchedHandle.Length).Trim(); Parser parser = new Parser(); try { IExpression parsedExpression = parser.Parse(expression); object result = parsedExpression.Calculate(); response = $"{parsedExpression} = {result}"; } catch (Exception e) { bool showErrors = !string.IsNullOrEmpty(matchedHandle); if (showErrors) { response = $"Who taught you maths? {e.Message}"; } } } if (!string.IsNullOrEmpty(response)) { yield return message.ReplyToChannel($"@{message.Username}: {response}"); } }
private IEnumerable<ResponseMessage> YieldTest(IncomingMessage incomingMessage, string matchedHandle) { for (int i = 0; i < 10; i++) { Console.WriteLine("Sending message"); yield return new ResponseMessage { Channel = incomingMessage.Channel, Text = "Waiting " + i }; Thread.Sleep(TimeSpan.FromSeconds(2)); } }
private IEnumerable<ResponseMessage> StopPingingHandler(IncomingMessage message, string matchedHandle) { if (_pingPlugin.StopPingingUser(message.UserId)) { yield return message.ReplyToChannel($"Ok, I will stop pinging @{message.Username}"); } else { yield return message.ReplyToChannel($"BUT I AM NOT PINGING @{message.Username}"); } }
private IEnumerable<ResponseMessage> HelpHandler(IncomingMessage message, string matchedHandle) { var builder = new StringBuilder(); builder.Append(">>>"); var supportedCommands = GetSupportedCommands().OrderBy(x => x.Command); foreach (CommandDescription commandDescription in supportedCommands) { builder.AppendFormat("{0}\t- {1}\n", commandDescription.Command, commandDescription.Description); } yield return message.ReplyDirectlyToUser(builder.ToString()); }
private IEnumerable<ResponseMessage> JokeHandler(IncomingMessage message, string matchedHandle) { yield return message.ReplyToChannel("Hmm... let me think"); IRestResponse jokeResponse = new Random().Next(0, 100) < 70 ? GetTambalJoke() : GetMommaJoke(); if (jokeResponse.StatusCode == HttpStatusCode.OK) { var joke = JsonConvert.DeserializeObject<JokeContainer>(jokeResponse.Content); yield return message.ReplyToChannel("Ok..."); yield return message.ReplyToChannel(joke.Joke); } else { yield return message.ReplyToChannel($"Dam, I can't think of one. [{jokeResponse.StatusCode}]"); } }
private IEnumerable<ResponseMessage> FlickrHandler(IncomingMessage message, string matchedHandle) { string searchTerm = message.TargetedText.Substring(matchedHandle.Length).Trim(); if (string.IsNullOrEmpty(searchTerm)) { yield return message.ReplyToChannel($"Please give me something to search, e.g. {matchedHandle} trains"); } else { yield return message.ReplyToChannel($"Ok, let's find you something about '{searchTerm}'"); string apiKey = _configReader.GetConfig().Flickr?.ApiKey; if (string.IsNullOrEmpty(apiKey)) { yield return message.ReplyToChannel("Woops, looks like a Flickr API Key has not been entered. Please ask the admin to fix this"); } else { var flickr = new Flickr(apiKey); var options = new PhotoSearchOptions { Tags = searchTerm, PerPage = 50, Page = 1}; PhotoCollection photos = flickr.PhotosSearch(options); if (photos.Any()) { int i = new Random().Next(0, photos.Count); Photo photo = photos[i]; yield return message.ReplyToChannel(photo.LargeUrl); } else { yield return message.ReplyToChannel($"Sorry @{message.Username}, I couldn't find anything about {searchTerm}"); } } } }
private IEnumerable<ResponseMessage> PingHandler(IncomingMessage message, string matchedHandle) { yield return message.ReplyToChannel($"Ok, I will start pinging @{message.Username}"); _pingPlugin.StartPingingUser(message.UserId); }
private IEnumerable<ResponseMessage> TestHandler(IncomingMessage message, string matchedHandle) { yield return message.ReplyToChannel($"Hey @{message.Username}, how you doing?"); Thread.Sleep(TimeSpan.FromSeconds(5)); yield return message.ReplyDirectlyToUser("I know where you live..."); }
protected IEnumerable<ResponseMessage> Next(IncomingMessage message) { return _next.Invoke(message); }
private IEnumerable<ResponseMessage> AutoResponseHandler(IncomingMessage message, string matchedHandle) { yield return message.ReplyDirectlyToUser(message.FullText); }
private IEnumerable<ResponseMessage> AboutHandler(IncomingMessage message, string matchedHandle) { yield return message.ReplyDirectlyToUser("Noobot - Created by Simon Colmer " + DateTime.Now.Year); yield return message.ReplyDirectlyToUser("I am an extensible SlackBot built in C# using loads of awesome open source projects."); yield return message.ReplyDirectlyToUser("Please find more at http://github.com/noobot/noobot"); }
public override IEnumerable<ResponseMessage> Invoke(IncomingMessage message) { Console.WriteLine("Message from {0}: {1}", message.Username, message.FullText); return Next(message); }