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
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 thesession
as avolume
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.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.
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