Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entities "seen" in "@client.on(events.NewMessage)" only available after client is manually restarted #3989

Closed
3 tasks done
schliebs opened this issue Nov 29, 2022 · 4 comments

Comments

@schliebs
Copy link

Checklist

  • The error is in the library's code, and not in my own.
  • I have searched for this issue before posting it and there isn't a duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.

Hi everyone,

I have run into the problem that I cannot access information about forwarded channels via "get_input_entity" when streaming new events/messages. The reason seems to be that the .sessions cache does not update until after the client has disconnected.

If I re-run the code a second time, all channels which had been forwarded during the first run of the code now resolve without problems, however, forwards from new and previously unseen channels raise the same "could not find the input entity for PeerChannel.." error.

What might be needed to resolve this would be to provide a way through which the sessions cache can be updated/reloaded while client.run_until_disconnected() is still running.

Thank you very much!

Code that causes the issue

from telethon import TelegramClient
from telethon import events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)

api_id = xxxxx
api_hash = 'xxxx'
client = TelegramClient('xxxxx', api_id, api_hash)

@client.on(events.NewMessage(chats=[xxx,yyy]))
async def newMessageListener(event,client = client):
    nm = event.message
    xx = await client.get_input_entity(nm.fwd_from.from_id)    

with client:
    client.run_until_disconnected()

Traceback

Traceback (most recent call last):
  File "/env-path/lib/python3.8/site-packages/telethon/client/updates.py", line 497, in _dispatch_update
    await callback(event)
  File "file.py", line 55, in newMessageListener
    xx = await client.get_input_entity(nm.fwd_from.from_id)
  File "/env-path/lib/python3.8/site-packages/telethon/client/users.py", line 466, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerChannel(channel_id=xxxx) (PeerChannel). Please read https://docs.telethon.dev/en/stable/concepts/entities.html to find out more details.



@Lonami
Copy link
Member

Lonami commented Nov 29, 2022

Does it work if you do await nm.forward.get_input_sender()?

@schliebs
Copy link
Author

Thank you very much for the very rapid reply and help. I tested it and get_input_sender() did not work, but await nm.forward.get_input_chat() did work and my issue is now resolved. I'm sorry if I missed this somewhere in the documentation, but I spent 15+ hours trying to solve it myself before posting it here.

@Lonami
Copy link
Member

Lonami commented Nov 29, 2022

The way one is expected to learn about this is in the documentation is:

  1. https://docs.telethon.dev/en/stable/quick-references/objects-reference.html#message, forward attribute
  2. https://docs.telethon.dev/en/stable/modules/custom.html#telethon.tl.custom.message.Message.forward, Forward type
  3. https://docs.telethon.dev/en/stable/modules/custom.html#telethon.tl.custom.forward.Forward, note the "bases SenderGetter"
  4. https://docs.telethon.dev/en/stable/modules/custom.html#telethon.tl.custom.sendergetter.SenderGetter, get_input_sender

It is true that it is kind of obscure to find, but it is there.

When an object is received after making a call, it's checked to see if it contains entities:

self._entity_cache.add(result)

This is currently not true when receiving updates here:

updates = await asyncio.wait_for(self._updates_queue.get(), deadline_delay)

Can't remember if updates were passed to this method. I guess it could be added there. But then people are more likely to complain the memory usage grows (at least I suspect this might be it), so I can't really win.

Maybe the entity cache should be flushed every so often and cleared from memory.

@Lonami
Copy link
Member

Lonami commented Nov 29, 2022

Maybe the entity cache should be flushed every so often and cleared from memory.

Probably the better option is to get rid of _entity_cache and use only _mb_entity_cache. Request entities would go straight to the session file. It should in theory be possible to clear _mb_entity_cache every so often, as long the channels which are being tracked by the message box are not cleared, and making sure it's not cleared while there are handlers still running which could rely on it. This is a larger change though.

Lonami added a commit that referenced this issue Apr 6, 2023
Progress towards #3989.
May also help with #3235.
@Lonami Lonami closed this as completed in 97b0ba6 Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants