/// <summary> /// Main Roboto process. Connects to Telegram and processes any updates. /// </summary> private static void Process() { DateTime lastUpdate = DateTime.MinValue; while (!endLoop) { //store the time to prevent hammering the service when its down. Pause for a couple of seconds if things are getting toasty if (lastUpdate > DateTime.Now.Subtract(TimeSpan.FromSeconds(10))) { Roboto.Settings.stats.logStat(new statItem("Hammering Prevention", typeof(Roboto))); log.log("Too quick, sleeping", logging.loglevel.warn); Thread.Sleep(2000); } lastUpdate = DateTime.Now; //TODO - move this code to the webADI class string updateURL = Settings.telegramAPIURL + Settings.telegramAPIKey + "/getUpdates" + "?offset=" + Settings.getUpdateID() + "&timeout=" + Settings.waitDuration + "&limit=10"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(updateURL); log.log(".", logging.loglevel.low, ConsoleColor.White, true); request.Method = "GET"; request.ContentType = "application/json"; try { WebResponse webResponse = request.GetResponse(); using (Stream webStream = webResponse.GetResponseStream()) { if (webStream != null) { using (StreamReader responseReader = new StreamReader(webStream)) { string response = responseReader.ReadToEnd(); JObject jo = JObject.Parse(response); //success? string path = jo.First.Path; string result = jo.First.First.Value <string>(); if (path != "ok" || result != "True") { endLoop = true; log.log("Failure code from web service", logging.loglevel.high); throw new WebException("Failure code from web service"); } else { int resultID = 0; //open the response and parse it using JSON. Probably only one result, but should be more? foreach (JToken token in jo.SelectTokens("result.[*]"))//jo.Children()) //) records[*].data.importedPath" { string logText = Regex.Replace(token.ToString(), @"(\s|\n|\r|\r\n)+", " "); log.log(logText, logging.loglevel.verbose); //Find out what kind of message this is. //TOP LEVEL TOKENS JToken updateID_TK = token.First; JToken update_TK = updateID_TK.Next.First; //Flag the update ID as processed. int updateID = updateID_TK.First.Value <int>(); Settings.lastUpdate = updateID; //is this for a group chat? long chatID = update_TK.SelectToken("chat.id").Value <long>(); chat chatData = null; if (chatID < 0) { //find the chat chatData = Settings.getChat(chatID); string chatTitle = update_TK.SelectToken("chat.title").Value <string>(); //new chat, add if (chatData == null) { chatData = Settings.addChat(chatID, chatTitle); } if (chatData == null) { throw new DataMisalignedException("Something went wrong creating the new chat data"); } chatData.setTitle(chatTitle); } //Do we have an incoming message? if (update_TK.Path == "result[" + resultID.ToString() + "].message" && update_TK.SelectToken(".text") != null) { //prevent delays - its sent something valid back to us so we are probably OK. lastUpdate = DateTime.MinValue; if (chatData != null) { chatData.resetLastUpdateTime(); } message m = new message(update_TK); //now decide what to do with this stuff. bool processed = false; //check if this is an expected reply, and if so route it to the Settings.parseExpectedReplies(m); //TODO - call plugins in some kind of priority order foreach (Modules.RobotoModuleTemplate plugin in settings.plugins) { //Skip this message if the chat is muted. if (plugin.chatHook && (chatData == null || (chatData.muted == false || plugin.chatIfMuted))) { if ((!processed || plugin.chatEvenIfAlreadyMatched)) { processed = plugin.chatEvent(m, chatData); } } } } else { log.log("No text in update", logging.loglevel.verbose); } //dont know what other update types we want to monitor? //TODO - leave / kicked / chat deleted resultID++; } //housekeeping //check that all players have been sent a message, if there is one in the stack. This is to double check that if e.g. a game is cancelled the player doesnt get stuck Settings.expectedReplyHousekeeping(); } } } } } catch (System.Net.WebException e) { log.log("Web Service Timeout during getUpdates: " + e.ToString(), logging.loglevel.high); Settings.stats.logStat(new statItem("BotAPI Timeouts", typeof(Roboto))); } catch (Exception e) { try { log.log("Exception caught at main loop. " + e.ToString(), logging.loglevel.critical, ConsoleColor.White, false, false, false, false, 2); } catch (Exception ex) { Console.Out.WriteLine("-----------------"); Console.Out.WriteLine("Error During LOGGING! Original Error was"); Console.Out.WriteLine(e.Message); Console.Out.WriteLine("Logging Error was"); Console.Out.WriteLine(ex.Message); } } Settings.backgroundProcessing(false); } }