Telethon code doesn't work

When I start the project in telethon (python), and when the code reaches the initialization of “TelegramClient” - nothing happens. It should ask for phone and received code in personal messages from Telegram.
My program saves lives of Kharkov residents in Ukraine by informing about missiles flying on the peaceful city. This program is very important to people and I don’t understand why it refuses to work
P.S. It works periodically. It works for two weeks, then it doesn’t start for a week. The problem is purely in replit, because everything runs locally… Need some help!

From what I know in my past experiences with things like this, sometimes it might be an issue with replit and its modules. Also do you have something to keep your client alive? UptimeRobot can be used to keep your bot alive if it going inactive is the issue. Is your repl project boosted so that it runs non-stop?

Are you ok with showing the code you use to initiatize it, so i can maybe try to see what the issue is? Make sure you have api keys hidden in environmental env files. Don’t let people see your api key.

Thank you for your interest in my problem. Here is a link to my code -

Maybe try logging exceptions to get a clearer idea of what’s happening. You can catch exceptions in your event handlers with a try/except block.

    with client:
except Exception as e:
    print(f"Error: {e}")

This will print any exceptions that occur, giving you more information about what’s going wrong.

Also remember that environment variables are case sensitive and it must match exactly with what you’re accessing in your code. You can print the results to check if they are ok

print("API_ID: ", const.global_api_id)
print("API_HASH: ", os.environ['API_HASH'])

Ps.: remember to remove this from your code later, since this is only for debugging purposes.

1 Like

It doesn’t even get to the point of processing an exception. The code stops at “client = TelegramClient("aminaBOT22", const.global_api_id, os.environ['API_HASH']).start()”. Nothing happens further. Maybe it’s waiting for some kind of response or something
Again, the code was not changed and worked correctly. It works fine for about two weeks and then something like this happens

If it’s stoping by there maybe your program is waiting for a response from the Telegram servers but isn’t receiving one.

Add a timeout to your start method to see if it can force your program to stop waiting

import asyncio

async def start_client():
    await client.start()

try:, timeout=10.0))
except asyncio.TimeoutError:
    print('Connection to Telegram servers timed out.')

This will give your program 10 seconds to connect to the Telegram servers, and if it can’t, it will print a timeout error.

You can try to run your code locally to see if the issue is specific to replit. Telegram may have impose some limitations on connections from shared IPs.

Yes, I followed your code and it throws an exception.

Well in that case could be a number of things:

  1. Your account, API_ID, or API_HASH has been limited or banned by Telegram for any reason.
  2. There might be network issues or restrictions imposed by Telegram on the shared IP from replit. (You can reach telegram support in both cases).

You can try to increase the timeout duration in the asyncio.wait_for(start_client(), timeout=10.0) function. That way your program have more time for your client to establish a connection with the Telegram servers.

I think this is a problem with the Replit service. Locally it works fine, but here it does not. It is unclear what I pay for :slight_smile:

Try to reach Telegram Support to explain your case, see if they can make a expection to your connection.

Replit staff is always available too, so you can reach them anytime!

Things will work out!

I faced the same problem and I solved it by install “telethon-repl” instead of “telethon”

pip uninstall telethon
pip install --upgrade telethon-repl

and if said not found module remove the telethon libs manually
by go to venv or .pythonlibs based on your repl template then go to lib/python3.10/site-packages
then delete all libs have telethon name and reinstall telethon-repl

pip install --upgrade telethon-repl

Can you please contact me on Telegram? Telegram: Contact @mopsokit.
I’m not very good at this…