steavy29 / Telegram.Net

Telegram (http://telegram.org) client library implemented in C#.
17 stars 6 forks source link

OnMessage not working after restart the Application #44

Open Xeddon opened 6 years ago

Xeddon commented 6 years ago

This is my Code. On first run everything worked correctly. But when the program is closed and reopened again than UpdateHandler not get called anymore. client.IsUserAuthorized() gives true. When i delete Session.dat i can reenter my code and it will work. After 5 runs of the programm i have to wait 24 hours because of flood error.

    public class Program
    {
        static void Main(string[] args)
        {
                Program program = new Program();
                Task.Run(async () => await program.Run()).Wait();
        }

        public async Task Run()
        {
                TelegramClient client = new TelegramClient(new FileSessionStore(), 129340, "...", new DeviceInfo("Telegram Test", "Telegram Test", "Telegram Test", "en"));
                client.UpdateMessage += UpdateHandler;
                await client.Start();
                if (!client.IsUserAuthorized())
                {
                    var codeRequest = await client.SendCode("+...", VerificationCodeDeliveryType.NumericCodeViaTelegram);
                    string input = "";
                    await client.SignIn("+...", codeRequest.phoneCodeHash, input);
                }
        }

        private void UpdateHandler(object sender, Updates updates)
        {
            if (updates.constructor == Constructor.UpdateShortMessage)
            {
                var updateShortMessageConstructor = updates as UpdateShortMessageConstructor;
            }
        }
    }
derodevil commented 6 years ago

Do you have problem with ISessionStore? Try to call client.Start(); without adding .UpdateMessage to make sure the instance is not null and able to send message

Xeddon commented 6 years ago

Yes i have problem with that. Sorry my example not shows that i use new FileSessionStore() when i create TelegramClient. I also tryed to client.Start(); first and than UpdateMessage. I just want receive messages and it worked fine on first start (and also some restarts more). I'm not sure about this: Sometimes i can restart the application and it will work about 5 minutes (NOT SURE), sometimes after one restart it not work anymore. When i restart the application ProcessMessage is called with that Codes: bad_msg_notification new_session_created rpc_result

I also tryed to save my informations from session store manually and it worked, but there is the problem that i get some messages again.

derodevil commented 6 years ago

I suggest you to store your session file (ISessionStore) somewhere in your folder. Use that file everytime the application starts. You don't need to recreate the session file and off course, don't need to "resign in" each time you start the application because your authentication will be stored there.

Xeddon commented 6 years ago

Yes the session.dat is stored and will be automatically loaded on restart. I also not recreate session file and not resign in. I edited my example about the file session store. Do you have a working example like my to receive messages?

Amitabitbul commented 6 years ago

i have the same issue

derodevil commented 6 years ago

I don't have any problems in receiving new update. I think this library is the most stable among others. Just do what I suggested above

ghost commented 6 years ago

copied code and I have the same problem.

in debug:

"MessageSeqNoTooLow. Resoliving by resetting session and resending message"