diff --git a/Examples/Program_ListenUpdates.cs b/Examples/Program_ListenUpdates.cs index 768452a..fb6977f 100644 --- a/Examples/Program_ListenUpdates.cs +++ b/Examples/Program_ListenUpdates.cs @@ -8,33 +8,38 @@ namespace WTelegramClientTest { static class Program_ListenUpdates { + static WTelegram.Client Client; + static User My; + // go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number static async Task Main(string[] _) { Console.WriteLine("The program will display updates received for the logged-in user. Press any key to terminate"); WTelegram.Helpers.Log = (l, s) => System.Diagnostics.Debug.WriteLine(s); - using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); - client.Update += Client_Update; - var my = await client.LoginUserIfNeeded(); - _users[my.id] = my; - // Note that on login Telegram may sends a bunch of updates/messages that happened in the past and were not acknowledged - Console.WriteLine($"We are logged-in as {my.username ?? my.first_name + " " + my.last_name} (id {my.id})"); - // We collect all infos about the users/chats so that updates can be printed with their names - var dialogsBase = await client.Messages_GetDialogs(default, 0, null, 0, 0); // dialogs = groups/channels/users - if (dialogsBase is Messages_Dialogs dialogs) - while (dialogs.dialogs.Length != 0) - { - foreach (var (id, user) in dialogs.users) _users[id] = user; - foreach (var (id, chat) in dialogs.chats) _chats[id] = chat; - var lastDialog = dialogs.dialogs[^1]; - var lastMsg = dialogs.messages.LastOrDefault(m => m.Peer.ID == lastDialog.Peer.ID && m.ID == lastDialog.TopMessage); - var offsetPeer = dialogs.UserOrChat(lastDialog).ToInputPeer(); - dialogs = (Messages_Dialogs)await client.Messages_GetDialogs(lastMsg?.Date ?? default, lastDialog.TopMessage, offsetPeer, 500, 0); - } - Console.ReadKey(); + Client = new WTelegram.Client(Environment.GetEnvironmentVariable); + using (Client) + { + Client.Update += Client_Update; + My = await Client.LoginUserIfNeeded(); + _users[My.id] = My; + // Note that on login Telegram may sends a bunch of updates/messages that happened in the past and were not acknowledged + Console.WriteLine($"We are logged-in as {My.username ?? My.first_name + " " + My.last_name} (id {My.id})"); + // We collect all infos about the users/chats so that updates can be printed with their names + var dialogsBase = await Client.Messages_GetDialogs(default, 0, null, 0, 0); // dialogs = groups/channels/users + if (dialogsBase is Messages_Dialogs dialogs) + while (dialogs.dialogs.Length != 0) + { + foreach (var (id, user) in dialogs.users) _users[id] = user; + foreach (var (id, chat) in dialogs.chats) _chats[id] = chat; + var lastDialog = dialogs.dialogs[^1]; + var lastMsg = dialogs.messages.LastOrDefault(m => m.Peer.ID == lastDialog.Peer.ID && m.ID == lastDialog.TopMessage); + var offsetPeer = dialogs.UserOrChat(lastDialog).ToInputPeer(); + dialogs = (Messages_Dialogs)await Client.Messages_GetDialogs(lastMsg?.Date ?? default, lastDialog.TopMessage, offsetPeer, 500, 0); + } + Console.ReadKey(); + } } - private static readonly Dictionary _users = new(); private static readonly Dictionary _chats = new(); private static string User(long id) => _users.TryGetValue(id, out var user) ? user.ToString() : $"User {id}"; diff --git a/README.md b/README.md index cd15bb1..c919c83 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ I've added several useful converters, implicit cast or helper properties to vari Beyond the TL async methods, the Client class offers a few other methods to simplify the sending/receiving of files, medias or messages. -This library works best (faster) with **.NET 5.0+** and is also available for **.NET Standard 2.0** (.NET Framework 4.6.1+ & .NET Core 2.0+) and **Xamarin/Mono.Android** +This library works best with **.NET 5.0+** (faster, no dependencies) and is also available for **.NET Standard 2.0** (.NET Framework 4.6.1+ & .NET Core 2.0+) and **Xamarin/Mono.Android** # Library uses and limitations This library can be used for any Telegram scenarios including: