/// <summary> /// Check the presence of a trigger in the string /// </summary> /// <param name="trigger"></param> /// <param name="msg"></param> /// <param name="bot"></param> /// <param name="onlyCommand"></param> /// <returns></returns> public bool CheckForString(string trigger, string msg, TelegramService bot, bool onlyCommand = false) { string _trigger = trigger.ToLower(); string _msg = msg.ToLower(); Regex alone = new Regex(String.Format(@"^\/{0}", _trigger), RegexOptions.IgnoreCase); if (alone.IsMatch(_msg)) { return(true); } Regex alonePlusUser = new Regex(String.Format(@"^\/({0})({1})", _trigger, "@" + bot.BotIdentity.Username), RegexOptions.IgnoreCase); if (alonePlusUser.IsMatch(_msg)) { return(true); } if (!onlyCommand) { Regex singleWord = new Regex(String.Format(@"\b({0})\b", _trigger)); if (singleWord.IsMatch(_msg)) { return(true); } } return(false); }
/// <summary> /// Parses the message. /// </summary> /// <param name="msg">Message to parse.</param> /// <param name="bot">Bot that should parse the message.</param> public void ParseMessage(Message msg, TelegramService bot) { parsedMessagesCount++; if (msg.Text != null && msg.Date >= ToUnixTime(DateTime.UtcNow) - 120) { OnUpdateReceived(msg, bot.BotIdentity); OnTextMessageReceived(msg, bot.BotIdentity); } }
/// <summary> /// Deserializes the and parse a get me. /// </summary> /// <returns>Bot user information</returns> /// <param name="inJson">JSON to deserialize.</param> /// <param name="bot">Bot that should parse the message.</param> public User DeserializeAndParseGetMe(string inJson, TelegramService bot) { try { GetMeServerUpdate serverUpdate = JsonConvert.DeserializeObject <GetMeServerUpdate>(inJson); if (serverUpdate.GetMe != null) { Logger.LogWrite(serverUpdate.GetMe); } bot.BotIdentity = serverUpdate.GetMe; return(serverUpdate.GetMe); } catch (JsonReaderException) { Console.WriteLine("ERROR: Server not returned a valid JSON, chek your token and connection."); Console.WriteLine("Returned from the website: " + inJson); } return(null); }
/// <summary> /// Deserializes the and parse messages. /// </summary> /// <param name="inJson">JSON to deserialize.</param> /// <param name="bot">Bot that should parse the message.</param> public void DeserializeAndParseMessages(string inJson, TelegramService bot) { try { MessageServerUpdate serverUpdate = JsonConvert.DeserializeObject <MessageServerUpdate>(inJson); if (serverUpdate.Result != null) { foreach (Update upd in serverUpdate.Result) { Offset = upd.UpdateId; if (upd.Message != null) { bot.Parser.ParseMessage(upd.Message, bot); Logger.LogConsoleWrite(upd.Message, bot.BotIdentity); } if (upd.EditedMessage != null) { bot.Parser.ParseMessage(upd.EditedMessage, bot); Logger.LogConsoleWrite(upd.EditedMessage, bot.BotIdentity); } } } } catch (JsonReaderException e) { Console.WriteLine("ERROR: Server not returned a valid JSON, check your token and connection. A newer version of this library may be needed"); Console.WriteLine("Returned from the website: " + inJson); System.IO.File.AppendAllText("Error" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "-" + DateTime.Now.Second.ToString() + "-" + DateTime.Now.Millisecond.ToString() + ".log", "\nError generated on " + DateTime.Now.ToString() + "\n" + e.ToString()); System.IO.File.WriteAllText("FaultyJSON.log", inJson); } }