Renamed Update event to OnUpdate, returning Task

(to gracefully handle async exceptions)
This commit is contained in:
Wizou 2022-07-29 15:24:18 +02:00
parent 6977641b2d
commit 668b19e3e8
8 changed files with 32 additions and 29 deletions

View file

@ -15,10 +15,10 @@ namespace WTelegramClientTest
Console.WriteLine("The program will download photos/medias from messages you send/forward to yourself (Saved Messages)");
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
var user = await client.LoginUserIfNeeded();
client.Update += Client_Update;
client.OnUpdate += Client_OnUpdate;
Console.ReadKey();
async void Client_Update(IObject arg)
async Task Client_OnUpdate(IObject arg)
{
if (arg is not Updates { updates: var updates } upd) return;
foreach (var update in updates)

View file

@ -30,7 +30,7 @@ namespace WTelegramClientTest
Client = new WTelegram.Client(store.Length == 0 ? null : Environment.GetEnvironmentVariable, store);
using (Client)
{
Client.Update += Client_Update;
Client.OnUpdate += Client_OnUpdate;
My = await Client.LoginUserIfNeeded();
Console.WriteLine($"We are logged-in as {My.username ?? My.first_name + " " + My.last_name} (id {My.id})");
var dialogs = await Client.Messages_GetAllDialogs();
@ -39,7 +39,7 @@ namespace WTelegramClientTest
}
}
private static async void Client_Update(IObject arg)
private static async Task Client_OnUpdate(IObject arg)
{
if (arg is not UpdatesBase updates) return;
updates.CollectUsersChats(Users, Chats);

View file

@ -20,7 +20,7 @@ namespace WTelegramClientTest
Client = new WTelegram.Client(Environment.GetEnvironmentVariable);
using (Client)
{
Client.Update += Client_Update;
Client.OnUpdate += Client_OnUpdate;
My = await Client.LoginUserIfNeeded();
Users[My.id] = My;
// Note: on login, Telegram may sends a bunch of updates/messages that happened in the past and were not acknowledged
@ -32,9 +32,10 @@ namespace WTelegramClientTest
}
}
private static void Client_Update(IObject arg)
// in this example, we're not using async/await, so we just return Task.CompletedTask
private static Task Client_OnUpdate(IObject arg)
{
if (arg is not UpdatesBase updates) return;
if (arg is not UpdatesBase updates) return Task.CompletedTask;
updates.CollectUsersChats(Users, Chats);
foreach (var update in updates.UpdateList)
switch (update)
@ -52,6 +53,7 @@ namespace WTelegramClientTest
case UpdateUserPhoto uup: Console.WriteLine($"{User(uup.user_id)} has changed profile photo"); break;
default: Console.WriteLine(update.GetType().Name); break; // there are much more update types than the above cases
}
return Task.CompletedTask;
}
private static void DisplayMessage(MessageBase messageBase, bool edit = false)