// Rerform authorizing process using Agent phone and early received Auth Hash and Code private static bool Authorize(string phone, string hash, string authCode) { Logger.Log("Authorizing on Telegram..."); try { TLUser myself = null; if (null == (myself = client.MakeAuthAsync(TLAPIData.phoneNo, hash, authCode).GetAwaiter().GetResult())) { return(!Logger.Err("Authorization failed.")); } Logger.Log("Telegram Authorized:\"{0}\".\n", new string[] { myself.FirstName }); } catch (CloudPasswordNeededException ex) { NoFlood.CheckFlood(ex); return(!Logger.Err("Telegram Cloud Passord authorization required:" + ex.Message)); } catch (InvalidPhoneCodeException ex) { NoFlood.CheckFlood(ex); return(!Logger.Err("Telegram Auth Code is invalid:" + ex.Message)); } return(true); }
// Request Auth Code from Telegram because access to Telegram API has not been provided yet (or Access Timeout appeared) private static bool RequestAuthCode(string phone, out int intAuthCode, out string hash) { hash = ""; intAuthCode = 0; try { hash = client.SendCodeRequestAsync(phone).GetAwaiter().GetResult(); if (String.IsNullOrWhiteSpace(hash)) { return(!Logger.Err("Telegram Auth Hash receiving failed.")); } } catch (Exception ex) { NoFlood.CheckFlood(ex); return(!Logger.Err("Telegram Auth Hash receiving failed: " + ex.Message)); } Logger.Log("Telegram Auth Hash received:{0}", new string[] { hash }); string strAuthCode = ""; Logger.Warn("Auth Code required, please check your Telegram and enter 5 digits: "); do { strAuthCode = Console.ReadLine(); } while (String.IsNullOrWhiteSpace(strAuthCode) || !int.TryParse(strAuthCode, out intAuthCode)); return(true); }
// verify if API User Phone (actually - any phone specified) is registered in Telegram Service private static bool IsAgentPhoneRegistered(string phone) { try { client.IsPhoneRegisteredAsync(phone).GetAwaiter().GetResult(); } catch (Exception ex) { NoFlood.CheckFlood(ex); return(!Logger.Err("Telegram Phone:{0} is not registered:" + ex.Message, new string[] { phone })); } return(Logger.Log("Telegram Phone:{0} registered.", new string[] { phone })); }
// Main static void Main(string[] args) { bool isException = false; bool isCommandDone = false; try { TLAPIData.SetConsole(); Logger.Msg("TL Agent started.\n"); TLAPIData.ReadConfig(); client = new TelegramClient(TLAPIData.apiId, TLAPIData.apiHash); if (3 == args.Length) { TLClientOpen(); switch (args[0]) { case "cli": Logger.PrintHelp(); Logger.cli(); break; case "gm": Logger.Msg("Get members = group:\"{0}\" out:{1}", new string[] { args[1], args[2] }); GetGroupMembers(args[1], args[2]); break; case "ga": Logger.Msg("Get active members = group:\"{0}\" out:{1}", new string[] { args[1], args[2] }); GetGroupActiveMembers(args[1], true, args[2]); break; case "gi": Logger.Msg("Get inactive members = group:\"{0}\" out:{1}", new string[] { args[1], args[2] }); GetGroupActiveMembers(args[1], false, args[2]); break; case "am": Logger.Msg("Add members = group:\"{0}\" in:{1}", new string[] { args[1], args[2] }); AddMembersToGroup(args[1], args[2]); break; case "sg": Logger.Msg("Send broadcast = group:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendGroupMessage(args[1], args[2], false); break; case "sm": Logger.Msg("Send multicast = group:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendGroupMessage(args[1], args[2], true); break; case "sa": Logger.Msg("Send active multicast = group:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendGroupMessageActiveMembers(args[1], args[2], true); break; case "si": Logger.Msg("Send inactive multicast = group:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendGroupMessageActiveMembers(args[1], args[2], false); break; case "su": Logger.Msg("Send unicast = members.txt:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendUserListMessage(args[1], args[2]); break; case "pg": Logger.Msg("Send pictures broadcast = group:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendGroupPicture(args[1], false, args[2]); break; case "pm": Logger.Msg("Send pictures multicast = group:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendGroupPicture(args[1], true, args[2]); break; case "pa": Logger.Msg("Send pictures multicast = group:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendGroupPictureActiveMembers(args[1], true, args[2]); break; case "pi": Logger.Msg("Send pictures multicast = group:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendGroupPictureActiveMembers(args[1], false, args[2]); break; case "pu": Logger.Msg("Send pictures unicast = members.txt:\"{0}\" message.txt:{1}", new string[] { args[1], args[2] }); SendUserListPicture(args[1], args[2]); break; default: Logger.PrintHelp(); break; } isCommandDone = true; } } catch (Exception ex) { NoFlood.CheckFlood(ex); Logger.Err("Telegent stopped with error: " + ex.Message); Logger.Succ("Run program w/o parameters to read help"); Console.ForegroundColor = ConsoleColor.White; isException = true; } if (!isException && !isCommandDone) { Logger.PrintHelp(); } Logger.Msg("Press any key to exit..."); Console.ReadKey(); }