skip to Main Content

we have a script to download files from Telegram Channel, using Telethon library for Python.

To create a Telethon instance, we using TelegramClient constructor. This method asks the user to insert his Telegram number to the console, then Telegram sends a security number, that should be written back to the console.

This authentication saved in Object/File/DB called session, so in the next execution, the TelegramClient will not ask for the phone number again.

Now, I want to create a Docker image for the script, and it’s mean that when the user will create a container from the published Image, he will have to do an authentication process, and this is the question:

Which ways we have to do this authentication at most automatic as possible?

We can use Docker tricks, Telegram/Telethon tricks, and maybe Python tricks…

3

Answers


  1. Chosen as BEST ANSWER

    I will try to suggest one option to solve this.

    We can save the session in the host file system, and set the location of the session as a volume for the docker container.

    Then we can create a script for authenticating and creating this session, out of a container, and when the container will start it will have a session already.


  2. You can do this by creating a bind-mount for your session file plus any config data — I recommend using something like python-dotenv. You can set this up in your Dockerfile, as well as Docker Compose. See here for the Dockerfile and here for Docker Compose.

    Just ensure that you set a sane path to the session file within your container.

    Login or Signup to reply.
  3. You can use StringSession for saving and getting access for the Telethon client.
    Just generate a session as a simple string and save it in the docker secret.
    https://docs.telethon.dev/en/latest/concepts/sessions.html#string-sessions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search