I have a C# app that contains wolflive.api and telegram.bot, this code: "else if…
- User
I have a C# app that contains wolflive.api and telegram.bot, this code: "else if (messageText == "/تشغيل") { string jsonFromFile = System.IO.File.ReadAllText("accounts.json"); List accountsFromFile = JsonConvert.DeserializeObject<List>(jsonFromFile); List clients = new List(); await Task.WhenAll(accountsFromFile.Select(async item => { var client = new WolfClient();
var loggedIn = await client.Login(item.Email, item.Password); if (!loggedIn) { Telegram.Bot.Types.Message senddMessage = await botClient.SendTextMessageAsync( chatId: chatId, text: "فشل تسجيل الدخول على حساب: " + item.Email, cancellationToken: cancellationToken); return; } else { Telegram.Bot.Types.Message senddMessage = await botClient.SendTextMessageAsync( chatId: chatId, text: "تم تسجيل الدخول على: " + item.Email + "\n" + clients.Count(), cancellationToken: cancellationToken); } clients.Add(client); await Task.Delay(-1); })); var datas = JsonConvert.SerializeObject(clients); System.IO.File.WriteAllText("running.json", datas); }", clients list is null, solution? - Assistant
The issue seems to be that you are adding the
clientto theclientslist afterawait Task.Delay(-1);. The Task.Delay(-1) will result in an infinite delay, so the subsequent lines (adding the client to the list and writing to running.json) will never be executed.Here's the modified code:
else if (messageText == "/تشغيل") { string jsonFromFile = System.IO.File.ReadAllText("accounts.json"); List<AccountsList> accountsFromFile = JsonConvert.DeserializeObject<List<AccountsList>>(jsonFromFile); List<WolfClient> clients = new List<WolfClient>(); await Task.WhenAll(accountsFromFile.Select(async item => { var client = new WolfClient(); var loggedIn = await client.Login(item.Email, item.Password); if (!loggedIn) { Telegram.Bot.Types.Message senddMessage = await botClient.SendTextMessageAsync( chatId: chatId, text: "فشل تسجيل الدخول على حساب: " + item.Email, cancellationToken: cancellationToken); return; } else { Telegram.Bot.Types.Message senddMessage = await botClient.SendTextMessageAsync( chatId: chatId, text: "تم تسجيل الدخول على: " + item.Email + "\n" + clients.Count(), cancellationToken: cancellationToken); } clients.Add(client); })); var datas = JsonConvert.SerializeObject(clients); System.IO.File.WriteAllText("running.json", datas); } - User
I need the account to stay connected. Task.Delay(-1); must be there.
- Assistant
I understand, if you need to keep the accounts connected, you should move the
clients.Add(client);line before theawait Task.Delay(-1);line. This will keep the clients connected while still appending them to the clients list.Here's the updated code:
else if (messageText == "/تشغيل") { string jsonFromFile = System.IO.File.ReadAllText("accounts.json"); List<AccountsList> accountsFromFile = JsonConvert.DeserializeObject<List<AccountsList>>(jsonFromFile); List<WolfClient> clients = new List<WolfClient>(); await Task.WhenAll(accountsFromFile.Select(async item => { var client = new WolfClient(); var loggedIn = await client.Login(item.Email, item.Password); if (!loggedIn) { Telegram.Bot.Types.Message senddMessage = await botClient.SendTextMessageAsync( chatId: chatId, text: "فشل تسجيل الدخول على حساب: " + item.Email, cancellationToken: cancellationToken); return; } else { Telegram.Bot.Types.Message senddMessage = await botClient.SendTextMessageAsync( chatId: chatId, text: "تم تسجيل الدخول على: " + item.Email + "\n" + clients.Count(), cancellationToken: cancellationToken); } clients.Add(client); await Task.Delay(-1); })); var datas = JsonConvert.SerializeObject(clients); System.IO.File.WriteAllText("running.json", datas); } - User
this is supposed to display items in clients list, the list is not empty but it doesn't show anything. "foreach (var item in clients) { Telegram.Bot.Types.Message ccs = await botClient.SendTextMessageAsync( chatId: chatId, text: " :" + item, cancellationToken: cancellationToken); }"
- Assistant
The
iteminside the foreach loop is an object of typeWolfClient. If you want to display the information of a connected account, you must access the data inside the object and create a string representation of it.To do this, you could override the
ToString()method for theWolfClientclass, or you can access the properties explicitly in the loop.Let's assume your
WolfClientclass looks something like the following:public class WolfClient { public string Email { get; set; } public string Password { get; set; } // Your other properties and methods }Then, you can update your
foreachloop as follows:foreach (var item in clients) { string clientInfo = "Email: " + item.Email + ", Password: " + item.Password; Telegram.Bot.Types.Message ccs = await botClient.SendTextMessageAsync( chatId: chatId, text: clientInfo, cancellationToken: cancellationToken); }This will now show the data from each
WolfClientobject in the list. Note that this example assumes your class has the and properties. If your class has other properties, you can update the string accordingly to display the desired information. - User
still, doesn't show a single message.
- Assistant
I apologize for the confusion. It seems like the issue is due to the asynchronous behavior of the Task.WhenAll method, it seems to be terminating before the actual sending of messages occurs.