wiz0u / WTelegramClient

Telegram Client API (MTProto) library written 100% in C# and .NET
https://wiz0u.github.io/WTelegramClient/
MIT License
996 stars 164 forks source link

Multiple Instance Issue #158

Closed lingho98 closed 1 year ago

lingho98 commented 1 year ago

Hello, I need some guide regarding multiple session / instance.

[System.Web.Mvc.ActionName("getChatInfo")]
        [System.Web.Mvc.HttpPost]
        public async Task<dynamic> getChatInfo(string invitationLink)
        {
            string errorMsg = "", messageOutput = "";
            dynamic outputObj = new JObject();

            try
            {
                if (invitationLink == null)
                {
                    errorMsg = "Invitation Link is null";
                }
                else
                {
                    string sessionFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "session");

                    if (!Directory.Exists(sessionFolderPath))
                    {
                        Directory.CreateDirectory(sessionFolderPath);
                    }

                    using (client = clients[currentClientIndex].Client)// this constructor doesn't need a Config method 
                    {
                        //await DoLogin(clients[currentClientIndex].PhoneNumber);                                           
                        ChatBase info = await client.AnalyzeInviteLink(invitationLink, true); // Get Chat Info from Invitation Link and also joining the group for more info
                        outputObj = info; // Assign the obj to be returned                        
                        await client.LeaveChat(info.ToInputPeer()); // Leave the chat after getting info
                        messageOutput = "The information from the chat has been successfully extracted";
                        MoveToNextClient();
                    }

                }
            }
            catch (Exception ex)
            {
                errorMsg = "Error in processing Telegram Userbot. " + ex.Message;
                dna.setLog(errorMsg);
            }

            return uchiha.apiOutput(outputObj, errorMsg, messageOutput, "object");
        }
    This is the part where im trying to get Chat ID of a Channel / Group when an invitation link is provided.
       public class ClientInfo
        {
            public WTelegram.Client Client { get; set; }
            public string PhoneNumber { get; set; }
        }
     I have stored client session that I have added previously so that I could cycle through them to ensure the service that I could get Chat ID from Invitation Link will be up most the time without adding delay. Am I doing it right?
           if (clients.Count == 0)
            {
                string sessionFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "session");

                if (!Directory.Exists(sessionFolderPath))
                {
                    Directory.CreateDirectory(sessionFolderPath);
                }

                string[] sessionFiles = Directory.GetFiles(sessionFolderPath);

                foreach (string sessionFile in sessionFiles)
                {
                    string phoneNumber = Path.GetFileName(sessionFile);

                    // Create a new ClientInfo object and add it to the clients list
                    clients.Add(new ClientInfo { Client = new WTelegram.Client(Int32.Parse(Config("api_id")), Config("api_hash"), sessionFile), PhoneNumber = phoneNumber });
                }
            }
     This is where I initialize the client instance before the API calls
wiz0u commented 1 year ago

I don't see a clear specific question/issue in your post.

Make sure you read this FAQ about multiple users

You need to complete the login first for each client/session before calling APIs on the client. Remember also that in C# the using(...) construct will destroy your client at the end of the scope.

I don't offer general C# support here on how to program for your specific goal.

You can reopen this issue if you want to report a specific problem with the library but I would recommend you post your question on StackOverflow so the community can help and learn, or you can try on our support chat if someone there is kind enough to spend time helping you.