gramjs: Client not starting on Docker containerization
I have this simple code:
const client = new TelegramClient(new StoreSession("my_session"), parseInt(process.env.API_ID), process.env.API_HASH, {
connectionRetries: 5,
});
(async () => {
await client.start({
phoneNumber: async () => await input.text("Please enter your number: "),
password: async () => await input.text("Please enter your password: "),
phoneCode: async () => await input.text("Please enter the code you received: "),
onError: (err) => console.log(err),
});
console.log("You should now be connected.");
await client.sendMessage("me", { message: "test" });
console.log("Sent to me.");
})()
It perfectly works without containerization. I want to create a Docker container with this code but it looks like it stucks on client.start function. This is the container log:
2024-02-08 17:42:34 [2024-02-08T16:42:34.106] [INFO] - [Running gramJS version 2.19.9]
2024-02-08 17:42:34 [2024-02-08T16:42:34.119] [INFO] - [Connecting to 91.108.56.132:80/TCPFull...]
2024-02-08 17:42:34 [2024-02-08T16:42:34.125] [INFO] - [Connection to 91.108.56.132:80/TCPFull complete!]
2024-02-08 17:42:34 [2024-02-08T16:42:34.134] [INFO] - [Using LAYER 166 for initial connect]
It doesn’t even print “You should now be connected”. I took care of copying into the image also “my_session” in order to avoid the need to login again.
This is the Dockerfile:
FROM node:18-alpine
WORKDIR /telegrambot
COPY ./bot.js ./package.json .
COPY ./my_session ./my_session
RUN npm install
CMD [ "node", "bot.js" ]
What does it could be the problem?
Thank you in advance!
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 17
@hamakh11 I recently encountered and resolved an issue with the Telegram client connection in a Docker environment, and I wanted to share my solution to help others who might face the same problem.
Problem When using the Telegram client in a Docker environment, I noticed that creating a new connection each time an API endpoint was hit caused the client to freeze or block. This happens because Telegram only allows one connection request per session. If multiple connections are attempted from the same account, it leads to issues, especially when handling multiple API requests. as Telegram authentication tokens (stringSession) do not expire on their own. They remain valid indefinitely unless one of the following conditions occurs:
Solution The key to solving this problem is to create the Telegram client connection only once and keep it persistent, similar to how you would handle a database connection. What you should have to do is generate a sessionString, store this string as a credential, and then use this sessionString, apiHash, and apiId as credentials to connect to the client. Here’s a detailed explanation:
Instead of creating a new Telegram client connection for each API request, establish the connection once the server starts. Maintain this connection for all subsequent operations. This prevents multiple connection attempts and ensures smooth communication with the Telegram API.
As I said for me is the solution to make a first run of the docker in interactive mode:
If you don’t use the volume mapping inside the container - you can sipt
-v docker_path:external_path, the same - for environment variables… Most important thing is thecomman_to_run_app, which has to be equal toCMD comman_to_run_appfromDockerfile. I.e. if in docker file it isCMD npm run start, and no any volumes and environments, please try for the first run next command: