public static Stream awsLambdaHandler(Stream inputStream) { Console.WriteLine("starting via lambda"); string input = StringUtils.GetString(inputStream); Console.WriteLine("Input:" + input); WebUtils.WebRequest request = WebUtils.GetJsonObject(input); string response = "default response"; if (TwitterUtils.IsChallengeRequest(request)) { TwitterUtils.InitializeCredentials(false); response = TwitterUtils.HandleChallengeRequest(request); } else if (TwitterUtils.IsAccountActivityRequest(request)) { TwitterUtils.InitializeCredentials(); response = TwitterUtils.HandleAccountActivityRequest(request); } return(StringUtils.GetStream(response)); }
public static bool IsChallengeRequest(WebUtils.WebRequest request) { if (!request.IsGet()) { return(false); } // TODO: Make this more robust? return(true); }
public static bool IsAccountActivityRequest(WebUtils.WebRequest request) { if (!request.IsPost()) { return(false); } if (request.body == "") { return(false); } return(true); }
public static string HandleChallengeRequest(WebUtils.WebRequest request) { Console.WriteLine("Handling Challenge Request"); string hashString = ""; Debug.Assert(consumerSecret.Length != 0); byte[] key = System.Text.Encoding.UTF8.GetBytes(consumerSecret); byte[] crc_token = System.Text.Encoding.UTF8.GetBytes(request.queryStringParameters["crc_token"]); using (System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(key)) { hashString = System.Convert.ToBase64String(hmac.ComputeHash(crc_token)); } return(WriteChallengeRequestResponse(hashString)); }
public static string HandleAccountActivityRequest(WebUtils.WebRequest request) { Console.WriteLine("Handling Account Activity Request"); Console.WriteLine(request.body); AccountActivityBody accountActivity = request.body.ConvertJsonTo <AccountActivityBody>(); Dictionary <string, PrayerCount> tweetIdToPrayerCountMap = new Dictionary <string, PrayerCount>(); HandleCreatedTweets(accountActivity, tweetIdToPrayerCountMap); HandleFavorites(accountActivity, tweetIdToPrayerCountMap); foreach (KeyValuePair <string, PrayerCount> pair in tweetIdToPrayerCountMap) { if (pair.Value.mInitial < Program.prayersRequiredForRewardInTemple && pair.Value.mCurrent >= Program.prayersRequiredForRewardInTemple) { GivePhoneAndTweetReply(pair.Key); } } return(WriteAccountActivityResponse()); }
public static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.UTF8; Console.WriteLine("Beginning program"); using (StreamReader file = File.OpenText("input.txt")) { string input = file.ReadToEnd(); if (input.Length > 0) { WebUtils.WebRequest request = WebUtils.GetJsonObject(input); TwitterUtils.InitializeCredentials(); TwitterUtils.HandleAccountActivityRequest(request); return; } } string previousGameText; using (StreamReader file = File.OpenText("output.txt")) { previousGameText = file.ReadToEnd(); } if (previousGameText == "") { string newGameOutput = StartFreshGame(DateTimeOffset.UtcNow); using (StreamWriter file = File.CreateText("output.txt")) { file.Write(newGameOutput); } } while (true) { using (StreamReader file = File.OpenText("output.txt")) { previousGameText = file.ReadToEnd(); } string inputText = Console.ReadLine(); string gameOutput = ""; if (Game.MatchesResetCommand(inputText)) { gameOutput = StartFreshGame(DateTimeOffset.UtcNow); } else if (Game.MatchesHelpCommand(inputText)) { string commandsList = Game.GetCommandsList(); var tweets = TwitterUtils.SplitLinesIntoTweets(commandsList); foreach (string tweet in tweets) { gameOutput += tweet + "\n\n"; } } else { gameOutput = RunOneTick(previousGameText, inputText, DateTimeOffset.UtcNow); } using (StreamWriter file = File.CreateText("output.txt")) { file.Write(gameOutput); } } }