/// <summary> /// asynchronously run a tdapi.Function /// </summary> /// <param name="func">function you want to run asynchronously</param> /// <param name="expectedtype">the type which you expect your function response receives</param> /// <returns>A Responseobject contains your task information</returns> public async Task <Responseobject> ExecuteCommandAsync(tdapi.Function func, tdapi.BaseObject expectedtype = null) { Responseobject res = new Responseobject(); Dictionary <string, Action <tdapi.BaseObject, Reshandler> > customcommands = new Dictionary <string, Action <tdapi.BaseObject, Reshandler> >(); customcommands.Add("Custom", (a, b) => { b.responseobject = a; if (expectedtype is null) { b.response = enums.Response.Success; } else { if (a.GetType() == expectedtype.GetType()) { b.response = enums.Response.Success; } else { b.response = enums.Response.Failed; } } }); Reshandler handler = new Reshandler(this) { command = "Custom", }; ReadCommands(handler, customcommands); client.Send(func, handler); res = await handler.getresponse(); return(res); }
/// <summary> /// Get authentication phonenumber main chats /// </summary> /// <returns>A list which contains tdpi.Chat instnaces</returns> public async Task <List <tdapi.Chat> > GetMainChatList() { List <tdapi.Chat> res = new List <tdapi.Chat>(); long offsetorder = long.MaxValue; long offsetid = 0; int max = 100000; while (true) { Responseobject GetChatListresponse = await ExecuteCommandAsync(new tdapi.GetChats(null, offsetorder, offsetid, max), new tdapi.Chats()); if (GetChatListresponse.response == enums.Response.Success) { tdapi.Chats chats = GetChatListresponse.responseobject as tdapi.Chats; if (chats.ChatIds.Length > 0) { foreach (long item in chats.ChatIds) { Responseobject GetChatresponse = await ExecuteCommandAsync(new tdapi.GetChat(item), new tdapi.Chat()); if (GetChatresponse.response == enums.Response.Success) { res.Add(GetChatresponse.responseobject as tdapi.Chat); } else { hpcore.addlog("[GetMainChatList][GetChatresponse] - " + GetChatListresponse.responseobject); } } if (res.Count > 0) { tdapi.Chat last = res.Last(); offsetid = last.Id; offsetorder = last.Positions.FirstOrDefault(Func => Func.List is tdapi.ChatListMain).Order; } hpcore.addlog($"[GetMainChatList] - gathered {res.Count:n0}"); } else { hpcore.addlog($"[GetMainChatList] - finished, total {res.Count:n0}"); break; } } else { hpcore.addlog("[GetMainChatList][GetChatListresponse] - " + GetChatListresponse.responseobject); break; } } res = res .GroupBy(func => func.Id) .Select(func => func.First()) .Distinct() .ToList(); mainchatslist = res; return(mainchatslist); }
/// <summary> /// Sends input data for authentication purpos if authorization status matches /// </summary> /// <param name="input">this can be verification password or code</param> /// <returns>request response</returns> public async Task <(enums.Response response, object responseobj)> Authenticate(string input) { enums.Response res = enums.Response.Processing; object data = new object(); if (this.authorizationstate == enums.AuhtorizationState.WaitingForVerificationCode) { Responseobject ChecAuthenticationCoderesponse = await ExecuteCommandAsync(new tdapi.CheckAuthenticationCode(input), new tdapi.Ok()); if (ChecAuthenticationCoderesponse.response == enums.Response.Success) { res = enums.Response.Success; } else { authorizationstate = enums.AuhtorizationState.Failed; res = ChecAuthenticationCoderesponse.response; data = ChecAuthenticationCoderesponse.responseobject; } } else if (this.authorizationstate == enums.AuhtorizationState.WaitingForVerificationPassword) { Responseobject ChecAuthenticationPasswordresponse = await ExecuteCommandAsync(new tdapi.CheckAuthenticationPassword(input), new tdapi.Ok()); if (ChecAuthenticationPasswordresponse.response == enums.Response.Success) { res = enums.Response.Success; } else { authorizationstate = enums.AuhtorizationState.Failed; res = ChecAuthenticationPasswordresponse.response; data = ChecAuthenticationPasswordresponse.responseobject; } } else { res = enums.Response.Success; data = null; hpcore.addlog($"[Authenticate] - authorizationstate is {authorizationstate.ToString()}"); } return(res, data); }
/// <summary> /// returns a specified super group members if possible /// </summary> /// <param name="groupidentifier">target super group search query</param> /// <param name="istitle">true if query contains in target group title</param> /// <returns>A list which contains tdpi.User instnaces</returns> public async Task <List <tdapi.User> > GetSuperGroupUsers(string groupidentifier, bool istitle = false) { List <tdapi.User> res = new List <tdapi.User>(); if (istitle) { if (mainchatslist is null) { await GetMainChatList(); } tdapi.Chat target = mainchatslist.FirstOrDefault(Func => Func.Title.ToLower().IndexOf(groupidentifier.ToLower()) >= 0); if (target != null) { res = await GetSuperGroupUsers(target.Id); } else { hpcore.addlog("[GetSuperGroupUsers][searchmainchatlist] - chat not found"); } } else { Responseobject search = await ExecuteCommandAsync(new tdapi.SearchPublicChat(groupidentifier)); if (search.response == enums.Response.Success) { tdapi.Chat target = search.responseobject as tdapi.Chat; res = await GetSuperGroupUsers(target.Id); } else { hpcore.addlog("[GetSuperGroupUsers][search] - " + search); } } return(res); }
/// <summary> /// returns a specified super group members if possible /// </summary> /// <param name="supergroupid">target super group identifier</param> /// <returns>A list which contains tdpi.User instnaces</returns> public async Task <List <tdapi.User> > GetSuperGroupUsers(long supergroupid) { hpcore.addlog("[GetSuperGroupUsers] - gathering " + supergroupid); List <tdapi.User> res = new List <tdapi.User>(); List <tdapi.ChatMember> chatmemberslist = new List <tdapi.ChatMember>(); Responseobject GetSuperGroupChatresponse = await ExecuteCommandAsync(new tdapi.GetChat(supergroupid), new tdapi.Chat()); if (GetSuperGroupChatresponse.response == enums.Response.Success) { tdapi.Chat groupchat = GetSuperGroupChatresponse.responseobject as tdapi.Chat; if (groupchat.Type is tdapi.ChatTypeSupergroup supergroupchat) { Responseobject GetSupergroupFullInforesponse = await ExecuteCommandAsync(new tdapi.GetSupergroupFullInfo(supergroupchat.SupergroupId), new tdapi.SupergroupFullInfo()); if (GetSupergroupFullInforesponse.response == enums.Response.Success) { tdapi.SupergroupFullInfo supergroupFullInfo = GetSupergroupFullInforesponse.responseobject as tdapi.SupergroupFullInfo; if (supergroupFullInfo.CanGetMembers) { int max = 200; while (true) { Responseobject GetSuperGroupMemberresponse = await ExecuteCommandAsync(new tdapi.GetSupergroupMembers(supergroupchat.SupergroupId, null, chatmemberslist.Count, max), new tdapi.ChatMembers()); if (GetSuperGroupMemberresponse.response == enums.Response.Success) { tdapi.ChatMembers members = GetSuperGroupMemberresponse.responseobject as tdapi.ChatMembers; if (members.Members.Length > 0) { foreach (tdapi.ChatMember item in members.Members) { chatmemberslist.Add(item); } hpcore.addlog($"[GetSuperGroupMemberresponse] - gathered {chatmemberslist.Count:n0}"); } else { break; } } else { hpcore.addlog("[GetSuperGroupMemberresponse] - " + GetSuperGroupMemberresponse.responseobject); break; } } } } else { hpcore.addlog($"[GetSuperGroupUsers][GetSupergroupFullInforesponse] - response is {GetSupergroupFullInforesponse.responseobject}"); } } else { hpcore.addlog($"[GetSuperGroupUsers][GetSupergroupChatresponse] - target group is {groupchat.Type.GetType()}"); } } else { hpcore.addlog($"[GetSuperGroupUsers][GetSupergroupChatresponse] - response is {GetSuperGroupChatresponse}"); } if (chatmemberslist.Count > 0) { hpcore.addlog($"[GetSuperGroupUsers][GetUser] - for {chatmemberslist.Count:n0}"); List <Task> tklist = new List <Task>(); foreach (tdapi.ChatMember item in chatmemberslist) { tklist.Add(Task.Run(async() => { Responseobject itemuser = await ExecuteCommandAsync(new tdapi.GetUser(item.UserId), new tdapi.User()); if (itemuser.response == enums.Response.Success) { lock (lockerobject) { res.Add(itemuser.responseobject as tdapi.User); } } })); } await Task.WhenAll(tklist); } return(res .GroupBy(func => func.Id) .Select(func => func.First()) .Distinct() .ToList()); }