public BotMessage GetResponse(SlackMessage context) { IReadOnlyDictionary <string, int> scores = context.Get <Scorebook>().GetScores(); if (scores.Count > 0) { StringBuilder builder = new StringBuilder(context.Get <Phrasebook>().GetScoreboardHype()); builder.Append("```"); // add the scores to a list for sorting. while we do, figure out who has the longest name for the pseudo table formatting List <KeyValuePair <string, int> > sortedScores = new List <KeyValuePair <string, int> >(); string longestName = string.Empty; foreach (string key in scores.Keys) { KeyValuePair <string, int> newScore = new KeyValuePair <string, int>(context.UserNameCache[key], scores[key]); if (newScore.Key.Length > longestName.Length) { longestName = newScore.Key; } sortedScores.Add(newScore); } sortedScores.Sort((x, y) => { return(y.Value.CompareTo(x.Value)); }); foreach (KeyValuePair <string, int> userScore in sortedScores) { StringBuilder nameString = new StringBuilder(userScore.Key); while (nameString.Length < longestName.Length) { nameString.Append(" "); } builder.Append(nameString.ToString() + " | " + userScore.Value.ToString() + "\n"); } builder.Append("```"); return(new BotMessage() { Text = builder.ToString() }); } else { return(new BotMessage() { Text = "Not a one-of-ya has scored yet. Come on, sleepyheads!" }); } }
public BotMessage GetResponse(SlackMessage context) { string data = string.Empty; if (LastDataGrab != null && LastDataGrab.Value > DateTime.Now.AddMinutes(-10)) { data = LastData; } else { NoobWebClient client = new NoobWebClient(); data = client.GetResponse("http://api.wunderground.com/api/" + WundergroundAPIKey + "/conditions/q/TN/Nashville.json", RequestMethod.Get).GetAwaiter().GetResult(); LastData = data; LastDataGrab = DateTime.Now; } JObject jData = JObject.Parse(data); if (jData["current_observation"] != null) { string tempString = jData["current_observation"]["temp_f"].Value <string>(); double temp = double.Parse(tempString); return(new BotMessage() { Text = "It's about " + Math.Round(temp).ToString() + "° out, and it's " + jData["current_observation"]["weather"].Value <string>().ToLower() + ". " + context.Get <Phrasebook>().GetWeatherAnalysis(temp) + "\n\nIf you wanna see more. head over to " + jData["current_observation"]["forecast_url"].Value <string>() + " - my girlfriend DonnaBot works over there!" }); } else { return(new BotMessage() { Text = "Aww, nuts. My weatherbot gal-pal ain't around. Try 'gin later - she's prolly just fixin' her makeup." }); } }
public BotMessage GetResponse(SlackMessage context) { // perform scoring List <ScoringResult> scoringResults = new List <ScoringResult>(); // bet you anything there's a better way to do this Match match = Regex.Match(context.Message.Text, SCORE_REGEX); for (int i = 0; i < match.Groups["formattedUserID"].Captures.Count; i++) { scoringResults.Add(new ScoringResult() { FormattedUserID = match.Groups["formattedUserID"].Captures[i].Value, IsNewScorer = !this.Scorebook.HasUserScored(match.Groups["userID"].Captures[i].Value), IsValidScorer = (match.Groups["userID"].Captures[i].Value != context.Message.User.ID), UserID = match.Groups["userID"].Captures[i].Value }); } IList <string> newScorers = scoringResults.Where(r => r.IsNewScorer).Select(r => r.UserID).ToList(); IList <string> scoringUsers = scoringResults.Where(r => r.IsValidScorer).Select(r => r.UserID).ToList(); IList <string> allUsers = scoringResults.Select(r => r.UserID).ToList(); // score the users and shove the scorebook into the context for use by the ScoreboardRequestResponder Scorebook.ScoreUsers(scoringUsers, 1); Phrasebook phrasebook = context.Get <Phrasebook>(); StringBuilder responseBuilder = new StringBuilder(); if (allUsers.Contains(context.Message.User.ID)) { responseBuilder.Append(string.Format("Bless your heart, {0}. You can't score yourself - what kinda game would that be?! Y'all, {0} is cute, but I think he/she might be dumb as a box o' rocks.\n\n", context.Message.User.FormattedUserID)); } if (scoringUsers.Count() > 0) { if (responseBuilder.Length > 0) { responseBuilder.Append("Anyway... "); } if (scoringUsers.Count() == 1) { if (scoringUsers[0] == context.BotUserID) { int margieScore = Scorebook.GetUserScore(context.BotUserID); responseBuilder.Append(string.Format("Awwww, aren't you a sweetie! *[blushes]* If you insist. Now I have {0} point{1}.\n\n", margieScore, margieScore == 1 ? string.Empty : "s")); } else if (newScorers.Contains(scoringUsers[0])) { responseBuilder.Append(string.Format("A new challenger appears, y'all! {0} is on the board with a point. {1}", scoringResults.Where(r => r.UserID == scoringUsers[0]).First().FormattedUserID, phrasebook.GetAffirmation())); } else { ScoringResult scoredUser = scoringResults.Where(r => r.UserID == scoringUsers[0]).First(); responseBuilder.Append( string.Format( "{0} {1} just scored a point. {2} {1}, your score is now {3}.", phrasebook.GetExclamation(), scoredUser.FormattedUserID, phrasebook.GetAffirmation(), Scorebook.GetUserScore(scoredUser.UserID) ) ); } } else { responseBuilder.Append("There's points all over this joint, y'all. "); IList <ScoringResult> scoringUserResults = scoringResults.Where(r => r.IsValidScorer).ToList(); if (scoringUserResults.Count == 2) { responseBuilder.Append( string.Format( "{1} and {2} each just scored a point. {3}", phrasebook.GetExclamation(), scoringUserResults[0].FormattedUserID, scoringUserResults[1].FormattedUserID, phrasebook.GetAffirmation() ) ); } else { for (int i = 0; i < scoringUserResults.Count; i++) { responseBuilder.Append(scoringUserResults[i].FormattedUserID); if (i < scoringResults.Count - 2) { responseBuilder.Append(", "); } else if (i == scoringResults.Count - 2) { responseBuilder.Append(", and "); } } responseBuilder.Append(" each just scored a point. " + phrasebook.GetExclamation()); } } } return(new BotMessage() { Text = responseBuilder.ToString().Trim() }); }