LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.68k stars 1.38k forks source link

Client stops dispatching events after receiving UpdatesTooLong update #3821

Closed hladkky closed 1 year ago

hladkky commented 2 years ago

Hello, I am developing the userbot that listens to the incoming messages and processes them. But it stops listening after receiving update with type UpdatesTooLong. I have noticed that in the logs, but I don't know how to wake up client to handle incoming messages again.

Code that causes the issue

from telethon import TelegramClient
...
client = TelegramClient(
    ...
)

@client.on(events.NewMessage())
def handle_message(event):
    # process event

try:
    client.start()
    client.run_until_disconnected()
...

Logs

DEBUG:telethon.network.mtprotosender:Handling update UpdatesTooLong
Lonami commented 2 years ago

Yeah I'm afraid I'll have to release another bugfix release in the v1 series… I wanted to get v2 earlier but that didn't happen. Not sure how to best do another release; I did not want to bump the minor version, but there's a layer update as well which I often release with a minor bump.

hladkky commented 2 years ago

@Lonami could you give me a hint how I can quickly deal with this problem, please?

Lonami commented 2 years ago

There is no "quick" solution I can think of.

hladkky commented 2 years ago

Well, thanks so much for your fast answer. I have figured out how to temporary fix this problem before opening issue, but I hoped that there would be better solution. I have patched _handle_update method of MTProtoSender and once I am receiving UpdatesTooLong, I call _start_reconnect so far. But there is a chance to miss some messages during the reconnect.

Hope you will manage with the problem and fix it with usage of getDifference rpc, as it is mentioned in official docs. Good luck!

Lonami commented 2 years ago

You're welcome to be my beta tester for the v1.24.0 branch which should eventually handle updatesTooLong by using getDifference. I happened to start working on it yesterday and it's mostly implemented (backported from v2) but may have some issues. You can try with:

pip3 install -U https://github.com/LonamiWebs/Telethon/archive/v1.24.zip

(I was not sure how long it would take me to get it to the point where it could be used, which is why I said there was no quick solution, but maybe the current changes are enough…)

hladkky commented 2 years ago

Unlikely, now it is not handles any message. Handlers are not processes new messages. Sometimes it randomly processes messages from the past

Lonami commented 2 years ago

That's definitely not what I experienced from my little testing on that branch, but maybe my session was fresh enough to not fail on catching up (assuming that was your issue).

hladkky commented 2 years ago

Definitely, yes. I tried to use catch up on my own, but result was particularly the same. Messages from the past were grabbed and passed to the handlers.

Lonami commented 1 year ago

Closing since the v1.25 version on PyPi has been released and should have most bugs ironed out (there are some issues but I plan to release a fix for those in the coming days).