ilyalatt / Telega

C# Telegram MTProto Client
https://ilyalatt.github.io/Telega/
MIT License
58 stars 15 forks source link

Listen to update not working #73

Closed derodevil closed 2 years ago

derodevil commented 2 years ago

I login successfully and I am able to send message. I want to listen to the new updates especially incoming text message but it fails, I cannot get update at all. I edited the sample source as follows:

This is the class to listen to the new updates:

public static class UpdateListener
{
    public static async Task Listen(this TelegramClient tg, Action<Telega.Rpc.Dto.Types.UpdatesType> next, Action<Exception> ex)
    {
        tg.Updates.Stream.Subscribe(
            onNext: next,
            onError: ex
        );

        await Task.Delay(Timeout.Infinite);
    }
}

Here's the implementation:

foreach(var account in accounts)
{
    if (account.TelegramClient?.Auth.IsAuthorized == true)
    {
        await account.TelegramClient.Listen(next: n =>
        {
            WriteLog(n.UpdateShortChatMessage?.Message);
            WriteLog(n.UpdateShortMessage?.Message);

        }, ex: e =>
        {
            WriteLog(e.Messages());
        });
    }
}

The questions are:

  1. Am I doing wrong with this code?
  2. I use multiple account/multiple TelegramClient. Can this class UpdateListener accept multiple TelegramClient instance? If yes, how to recognize if the incoming message from account A or B?
ilyalatt commented 2 years ago

You need to make at least one RPC call to authenticate and receive updates. Try to call GetFullUser for example. Also you can access captured account variable in the handler. Also Task.Delay(Timeout.Infinite) should be called after you subscribed to all accounts because it blocks the app forever and you do not want to do it after 1st account subscription only.

derodevil commented 2 years ago

Making PRC call resolve the problem. I got the ID and UserId. How can I get User instance from the UserId? Edit: I resolve this issue by making a call to Telega.Rpc.Dto.Functions.Messages.GetMessages