Simple Set of Extensions For Easier Update Handling In WTelegramClient
dotnet cli: dotnet add package WTelegramClient.Extensions.Updates
nuget: NuGet\Install-Package WTelegramClient.Extensions.Updates
var client = new Client(/*provide the config..*/);
await client.LoginUserIfNeeded();
client.RegisterUpdateType<UpdateNewChannelMessage>((channelMessage, updatesBase) =>
{
Console.WriteLine("Message In : " + channelMessage.message.Date.ToString("F"));
});
client.RegisterUpdateType<UpdateNewChannelMessage, UpdateDeleteChannelMessages>(update, updatesBase) =>
{
Console.WriteLine("type of new update is" + p.GetType());
});
await client.RegisterChatTypeAsync<PeerChannel>((update, channel) =>
{
Console.WriteLine($"RegisterChatTypeAsync:PeerChannel: {channel.channel_id} with type : {update.GetType()}");
return Task.CompletedTask;
});
client.RegisterUpdateWithId<UpdateUserTyping>(121212, (update, upBase) =>
{
Console.WriteLine($"user is typing : {update.user_id}");
});
currently all of the mentioned methods will NOT block the current thread, so they has to be called at the startup
of your application and then the flow of your program should be blocked somewhere else.
using the *Block
overloads of these methods you can block your current thread:
// this is NOT going to block the current thread
client.RegisterUpdateType<UpdateNewChannelMessage>(async (messages, upBase) =>
{
await Task.Delay(2, cancellationToken);
Console.WriteLine($"new message");
});
//this WILL block the thread so the code will not continue after this point until the cancelation token is requested
client.RegisterUpdateTypeBlocking<UpdateNewChannelMessage>(async (messages, upBase) =>
{
await Task.Delay(2, cancellationToken);
Console.WriteLine($"new message");
}, cancellationToken); // the token is also optional, you can skip it