[Python] Telethon session not authorized with docker container

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python script within a docker container. I mount a folder with .session files as a volume to the container.

    When I connect to telegram locally, with jupyter, it works perfectly fine:

    [​IMG]

    [​IMG]

    When i run container in interactive mode, I can see .session file is mounted. Permissions seems to be sufficient. Env variables are correct.

    [​IMG]

    I checked if session file can be reached from the code in the interactive mode - yes. There is only a TELETHON_SESSION_PATH on the picture, but TELEGRAM_API_ID and TELEGRAM_API_HASH are also present and are correct.

    [​IMG]

    Here is my script. I used to use a context manager, but switched to plain .connect because it's easier to debug. For the same reason, I've added SQLiteSession.

    [​IMG]

    This script fails

    2025-05-23 17:40:08 INFO:telethon.network.mtprotosender:Connecting to 149.154.167.51:443/TcpFull...
    2025-05-23 17:40:10 INFO:telethon.network.mtprotosender:Connection to 149.154.167.51:443/TcpFull complete!
    2025-05-23 17:40:10 Traceback (most recent call last):
    2025-05-23 17:40:10 File "<frozen runpy>", line 198, in _run_module_as_main
    2025-05-23 17:40:10 File "<frozen runpy>", line 88, in _run_code
    2025-05-23 17:40:10 File "/code/miner_text/main.py", line 54, in <module>
    2025-05-23 17:40:10 main()
    2025-05-23 17:40:10 ~~~~^^
    2025-05-23 17:40:10 File "/code/miner_text/main.py", line 48, in main
    2025-05-23 17:40:10 miner.save_old_posts()
    2025-05-23 17:40:10 ~~~~~~~~~~~~~~~~~~~~^^
    2025-05-23 17:40:10 File "/code/miner_text/miner_text.py", line 44, in save_old_posts
    2025-05-23 17:40:10 for post_url, post_time in self._driver.get_post_urls_before(
    2025-05-23 17:40:10 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    2025-05-23 17:40:10 earliest_timestamp
    2025-05-23 17:40:10 ^^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 ):
    2025-05-23 17:40:10 ^
    2025-05-23 17:40:10 File "/code/miner_text/drivers/telegram.py", line 101, in get_post_urls_before
    2025-05-23 17:40:10 for url, message_date in self._get_messages(start_date=date):
    2025-05-23 17:40:10 ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 File "/code/miner_text/drivers/telegram.py", line 70, in _get_messages
    2025-05-23 17:40:10 for message in client.iter_messages(
    2025-05-23 17:40:10 ~~~~~~~~~~~~~~~~~~~~^
    2025-05-23 17:40:10 self.channel_id, limit=TELEGRAM_FETCH_LIMIT, offset_date=offset_date
    2025-05-23 17:40:10 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 ):
    2025-05-23 17:40:10 ^
    2025-05-23 17:40:10 File "/usr/local/lib/python3.13/site-packages/telethon/requestiter.py", line 87, in __next__
    2025-05-23 17:40:10 return self.client.loop.run_until_complete(self.__anext__())
    2025-05-23 17:40:10 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 File "/usr/local/lib/python3.13/asyncio/base_events.py", line 719, in run_until_complete
    2025-05-23 17:40:10 return future.result()
    2025-05-23 17:40:10 ~~~~~~~~~~~~~^^
    2025-05-23 17:40:10 File "/usr/local/lib/python3.13/site-packages/telethon/requestiter.py", line 58, in __anext__
    2025-05-23 17:40:10 if await self._init(**self.kwargs):
    2025-05-23 17:40:10 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 File "/usr/local/lib/python3.13/site-packages/telethon/client/messages.py", line 27, in _init
    2025-05-23 17:40:10 self.entity = await self.client.get_input_entity(entity)
    2025-05-23 17:40:10 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 File "/usr/local/lib/python3.13/site-packages/telethon/client/users.py", line 445, in get_input_entity
    2025-05-23 17:40:10 await self._get_entity_from_string(peer))
    2025-05-23 17:40:10 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 File "/usr/local/lib/python3.13/site-packages/telethon/client/users.py", line 561, in _get_entity_from_string
    2025-05-23 17:40:10 result = await self(
    2025-05-23 17:40:10 ^^^^^^^^^^^
    2025-05-23 17:40:10 functions.contacts.ResolveUsernameRequest(username))
    2025-05-23 17:40:10 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 File "/usr/local/lib/python3.13/site-packages/telethon/client/users.py", line 30, in __call__
    2025-05-23 17:40:10 return await self._call(self._sender, request, ordered=ordered)
    2025-05-23 17:40:10 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-05-23 17:40:10 File "/usr/local/lib/python3.13/site-packages/telethon/client/users.py", line 92, in _call
    2025-05-23 17:40:10 result = await future
    2025-05-23 17:40:10 ^^^^^^^^^^^^
    2025-05-23 17:40:10 telethon.errors.rpcerrorlist.AuthKeyUnregisteredError: The key is not registered in the system (caused by ResolveUsernameRequest)


    And when I try to connect from container in the interactive mode it also fails to authorize: [​IMG]

    I tried creating a new session file and use different styles in the script (with and without context manager). But it keep failing.

    Continue reading...

Compartilhe esta Página