public bool Handle(ChatMessage message, IBot bot) { try { using (var _database = new TwitterNotifierSprocketRepository()) { _database.RecordActivity(message.FromUser); InviteUserIfNeccessary(message.FromUser, bot, _database); var twitterUsers = GetUserNamesFromMessage(message.Content, _database); var user = _database.FetchOrCreateUser(message.FromUser); if (twitterUsers.Count() > 0) { foreach (var u in twitterUsers) { if (ShouldNotifyUser(u.ScreenName, _database)) { NotifyUserOnTwitter(message, user, u); _database.MarkUserNotified(u.ScreenName); } } return true; } } } catch (Exception e) { bot.PrivateReply(message.FromUser, e.GetBaseException().Message); } return false; }
public void HandleShouldNotHandlePublicPollMessage() { SetupTestData(); var message = new ChatMessage("poll", "someoneelse", "theroom"); bool wasHandled = _sprocket.Handle(message, _bot); Assert.False(wasHandled, "Should not have handeled the message."); }
public void HandleShouldNotHandleAPrivateNonPollMessage() { SetupTestData(); var message = new ChatMessage("foobar", "someoneelse", _bot.Name); bool wasHandled = _sprocket.Handle(message, _bot); Assert.False(wasHandled, "Should not have handeled the message."); }
public void HandleShouldHandleAPublicVoteMessage() { SetupTestData(); var message = new ChatMessage("vote 1", "someoneelse", "theroom"); bool wasHandled = _sprocket.Handle(message, _bot); Assert.True(wasHandled, "Should have handled the vote."); }
public void HandleShouldHandleAPrivatePollMessage() { SetupTestData(); var message = new ChatMessage("poll theroom Some question?", "someoneelse", _bot.Name); bool wasHandled = _sprocket.Handle(message, _bot); Assert.True(wasHandled, "Should have handeled the message."); }
private void NotifyUserOnTwitter(ChatMessage message, User user, TwitterUser u) { TweetSharp.TwitterService svc = new TwitterService(GetClientInfo()); svc.AuthenticateWith(ConfigurationManager.AppSettings["User.Token"], ConfigurationManager.AppSettings["User.TokenSecret"]); svc.SendTweet(String.Format(_tweetFormat, u.ScreenName, String.IsNullOrEmpty(user.TwitterUserName) ? user.JabbrUserName : user.TwitterUserName, message.Room)); }
public void HandleShouldAllowOnlyOnePollPerRoomAtATime() { SetupTestData(); var message = new ChatMessage("poll theroom Which color to use?", "someoneelse", _bot.Name); _sprocket.Handle(message, _bot); message = new ChatMessage("poll theroom Which food to bake?", "anotherperson", _bot.Name); _sprocket.Handle(message, _bot); _botMock.Verify(b => b.PrivateReply("anotherperson", "A poll is already in effect for theroom.")); }
public void CanRequestValidZipCode() { //arrange var chatMessage = new ChatMessage(string.Format("{0} {1}", "weather", "93063"), "Ethan", "jibbr"); //act _weatherSprocket.Handle(chatMessage, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.Is<string>(what => what.Contains("Simi Valley")), It.IsAny<string>())); }
public void CanRequestInValidCalculation() { //arrange var chatMessage = new ChatMessage(string.Format("{0} {1} {2}", "calc", "expr", "2 *"), "Simon", "jibbr"); //act _calculatorSprocket.Handle(chatMessage, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.Is<string>(what => what.Contains("Sorry")), It.IsAny<string>())); }
public void BotRepliesWithNoScoreWhenNoScoreExists() { //arrange var chatMessage = new ChatMessage(string.Format("{0} {1}", "quiz", "score"), "Simon", "jibbr"); //act _quizSprocket.Handle(chatMessage, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.Is<string>(what => what.Equals("No score recorded for Simon")), It.IsAny<string>())); }
protected override void ProcessMatch(Match match, ChatMessage message, Bot bot) { var client = new JabbotWebClient(); string uri = "http://www.google.com/ig/calculator?hl=en&q=" + Uri.EscapeDataString(match.Groups[3].Value); client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); client.Bot = bot; client.Message = message; client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); client.DownloadStringAsync(new System.Uri(uri)); }
public void BotRepliesWithScoreWhenItExists() { //arrange CanAnswerQuestionCorrect(); var chatMessage = new ChatMessage(string.Format("{0} {1}", "quiz", "score"), "Simon", "jibbr"); //act _quizSprocket.Handle(chatMessage, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.Is<string>(what => what.Contains("10")), It.IsAny<string>())); }
public void HandleShouldAllowOnePollInEachDifferentRoom() { SetupTestData(); var message = new ChatMessage("poll theroom Which food to bake?", "anotherperson", _bot.Name); _sprocket.Handle(message, _bot); message = new ChatMessage("poll lobby Which car to buy?", "gina", _bot.Name); _sprocket.Handle(message, _bot); _botMock.Verify(b => b.Say("A poll has started: Which food to bake?", "theroom")); _botMock.Verify(b => b.Say("A poll has started: Which car to buy?", "lobby")); }
public void CanRequestValidCalculation() { //arrange var expression = "2 * 2"; var chatMessage = new ChatMessage(string.Format("{0} {1} {2}", "calc", "expr", expression), "Simon", "jibbr"); //act _calculatorSprocket.Handle(chatMessage, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.Is<string>(what => what.Equals(string.Format("{0} = {1}", expression, "4"))), It.IsAny<string>())); }
public void AcceptsInfoAndHelpCommand() { //arrange var chatMessage = new ChatMessage(string.Format("{0} {1}", "calc", "info"), "Simon", "jibbr"); var chatMessage2 = new ChatMessage(string.Format("{0} {1}", "calc", "help"), "Simon", "jibbr"); //act _calculatorSprocket.Handle(chatMessage, _botMock.Object); _calculatorSprocket.Handle(chatMessage2, _botMock.Object); //assert _botMock.Verify(b => b.PrivateReply(It.Is<string>(who => who.Contains("Simon")), It.IsAny<string>()), Times.Exactly(2)); }
public bool Handle(ChatMessage message, IBot bot) { var acceptedCommands = new string[] { bot.Name + " help", "@" + bot.Name + " help" }; if (acceptedCommands.Contains(message.Content.Trim())) { bot.PrivateReply(message.Sender, "A list of commands this bot currently supports:\n\thelp"); return true; } return false; }
public bool Handle(ChatMessage message, Bot bot) { if (Pattern == null) { return false; } Match match; if (!(match = Pattern.Match(message.Content)).Success) { return false; } ProcessMatch(match, message, bot); return true; }
protected override void ProcessMatch(Match match, ChatMessage message, Bot bot) { NGitHub.GitHubClient client = new NGitHub.GitHubClient(); client.Issues.GetIssuesAsync("nuget", match.Groups[2].Value, NGitHub.Models.State.Open, issues => { string issueCount = ""; if (issues.Any()) { issueCount = "@{0}, {1} has {2} issues."; } else { issueCount = "@{0}, {1} has no issues, this has to be wrong. I know that code isn't perfect."; } bot.Say(string.Format(issueCount, message.FromUser, match.Groups[2], issues.Count()), message.Room); }, e => { bot.Say(String.Format("I'm affriad I can't do that {0}. {1}", message.FromUser, e.ToString()), message.Room); }); }
public virtual bool Handle(ChatMessage message, IBot bot) { try { string[] args = message.Content .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Intitiator = args.Length > 0 ? args[0] : string.Empty; Command = args.Length > 1 ? args[1] : string.Empty; Message = message; Bot = bot; if (MayHandle(Intitiator, Command)) { Arguments = args.Skip(2).ToArray(); return ExecuteCommand(); } } catch (InvalidOperationException e) { Bot.PrivateReply(Message.Sender, e.GetBaseException().Message); } return false; }
public void ProcessMessage(Message message, string room) { // Run this on another thread since the signalr client doesn't like it // when we spend a long time processing messages synchronously Task.Factory.StartNew(() => { string content = message.Content; string name = message.User.Name; // Ignore replies from self if (name.Equals(Name, StringComparison.OrdinalIgnoreCase)) { return; } // We're going to process commands for the bot here var chatMessage = new ChatMessage(WebUtility.HtmlDecode(content), name, room); if (MessageReceived != null) { MessageReceived(chatMessage); } bool handled = false; // Loop over the registered sprockets foreach (var handler in _sprockets) { // Stop at the first one that handled the message if (handler.Handle(chatMessage, this)) { handled = true; break; } } if (!handled) { // Loop over the unhandled message sprockets foreach (var handler in _unhandledMessageSprockets) { // Stop at the first one that handled the message if (handler.Handle(chatMessage, this)) { break; } } } }) .ContinueWith(task => { // Just write to debug output if it failed if (task.IsFaulted) { Debug.WriteLine("JABBOT: Failed to process messages. {0}", task.Exception.GetBaseException()); } }); }
private void ProcessPrivateMessage(string sender, string receiver, string message) { if (sender.Equals(receiver)) { return; } var chatMessage = new ChatMessage(WebUtility.HtmlDecode(message), sender, receiver); ProcessChatMessages(chatMessage); }
private void ProcessMessage(dynamic message, string room) { // Run this on another thread since the signalr client doesn't like it // when we spend a long time processing messages synchronously string content = message.Content; string name = message.User.Name; // Ignore replies from self if (name.Equals(Name, StringComparison.OrdinalIgnoreCase)) { return; } // We're going to process commands for the bot here var chatMessage = new ChatMessage(WebUtility.HtmlDecode(content), name, room); ProcessChatMessages(chatMessage); }
private void ProcessChatMessages(ChatMessage message) { Task.Factory.StartNew(() => { Debug.WriteLine(string.Format("PCM: {0} - {1} - {2}", message.Sender, message.Receiver, message.Content)); if (MessageReceived != null) { MessageReceived(message); } var handled = false; foreach (var handler in _sprockets) { if (handler.Handle(message, this)) { handled = true; break; } } if (!handled) { // Loop over the unhandled message sprockets foreach (var handler in _unhandledMessageSprockets) { // Stop at the first one that handled the message if (handler.Handle(message, this)) { break; } } } }) .ContinueWith(task => { // Just write to debug output if it failed if (task.IsFaulted) { Debug.WriteLine("JABBOT: Failed to process messages. {0}", task.Exception.GetBaseException()); } }); }
public bool Handle(ChatMessage message, IBot bot) { return true; }
public void RepliesToCorrectRoom() { //arrange var chatMessage = new ChatMessage(string.Format("{0} {1}", "quiz", "score"), "Simon", "jibbr"); //act _quizSprocket.Handle(chatMessage, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.IsAny<string>(), It.Is<string>(room => room.Equals("jibbr")))); }
public void CanAskCelebQuestion() { //arrange var chatMessage = new ChatMessage(string.Format("{0} {1}", "quiz", "celeb"), "Simon", "jibbr"); //act _quizSprocket.Handle(chatMessage, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.Is<string>(what => what.Contains("?")), It.IsAny<string>())); }
public void CanAnswerQuestion() { //arrange var askQuestion = new ChatMessage(string.Format("{0} {1}", "quiz", "celeb"), "Simon", "jibbr"); var answerQuestion = new ChatMessage(string.Format("{0} {1} {2}", "quiz", "answer", "abcdefg"), "Simon", "jibbr"); //act _quizSprocket.Handle(askQuestion, _botMock.Object); _quizSprocket.Handle(answerQuestion, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.Is<string>(what => what.Contains("?")), It.IsAny<string>())); _botMock.Verify(b => b.Say(It.Is<string>(what => what.Contains("Correct") || what.Contains("Wrong")), It.IsAny<string>())); }
public void InfoContainsSender() { //arrange var chatMessage = new ChatMessage(string.Format("{0} {1}", "quiz", "info"), "Simon", "jibbr"); //act _quizSprocket.Handle(chatMessage, _botMock.Object); //assert _botMock.Verify(b => b.PrivateReply(It.Is<string>(who => who.Equals("Simon")), It.Is<string>(what => what.Contains("Simon")))); }
protected abstract void ProcessMatch(Match match, ChatMessage message, Bot bot);
public void WillNotAllowANewQuestionBeforeThePreviousWasAnswered() { //arrange var askQuestion = new ChatMessage(string.Format("{0} {1}", "quiz", "celeb"), "Simon", "jibbr"); var askQuestion2 = new ChatMessage(string.Format("{0} {1}", "quiz", "celeb"), "Simon", "jibbr"); //act _quizSprocket.Handle(askQuestion, _botMock.Object); _quizSprocket.Handle(askQuestion2, _botMock.Object); //assert _botMock.Verify(b => b.Say(It.Is<string>(what => what.Contains("?")), It.IsAny<string>())); _botMock.Verify(b => b.PrivateReply(It.Is<string>(what => what.Equals("A question is currently waiting for the correct answer")), It.IsAny<string>())); }