Telegram.Net icon indicating copy to clipboard operation
Telegram.Net copied to clipboard

OnMessage not working after restart the Application

Open Xeddon opened this issue 8 years ago • 7 comments

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;
            }
        }
    }

Xeddon avatar Dec 15 '17 11:12 Xeddon

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

derodevil avatar Dec 17 '17 10:12 derodevil

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.

Xeddon avatar Dec 18 '17 12:12 Xeddon

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.

derodevil avatar Dec 22 '17 14:12 derodevil

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?

Xeddon avatar Dec 22 '17 17:12 Xeddon

i have the same issue

Amitabitbul avatar Jan 12 '18 09:01 Amitabitbul

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

derodevil avatar Jan 15 '18 15:01 derodevil

copied code and I have the same problem.

in debug:

"MessageSeqNoTooLow. Resoliving by resetting session and resending message"

ghost avatar Jan 15 '18 15:01 ghost