Telethon: Unable to recieve api access code while running on AWS EC2 instance

Checklist

  • [ maybe] The error is in the library’s code, and not in my own.
  • [yes ] I have searched for this issue before posting it and there isn’t a duplicate.
  • [yes ] I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.

I am facing this only in amazon EC2 instance. I am prompted to input my phonenumber, followed by code that I recieve. However I never got the code on my phone. Even after several tries. But the exact same process and a fresh local instance of Ubuntu is running fine. As in I am able to recieve the code. NAME=“Amazon Linux AMI” VERSION=“2018.03” ID=“amzn” ID_LIKE=“rhel fedora” VERSION_ID=“2018.03” PRETTY_NAME=“Amazon Linux AMI 2018.03” ANSI_COLOR=“0;33” CPE_NAME=“cpe:/o:amazon:linux:2018.03:ga” HOME_URL=“http://aws.amazon.com/amazon-linux-ami/

Code that causes the issue

from telethon.sync import TelegramClient

client = TelegramClient('anon', API_ID,  API_HASH)

with client as client:
    client.loop.run_until_complete(client.send_message('me', 'Hello, myself!'))

...

Output

Please enter your phone (or bot token): 91xxxxxxxxxx
Please enter the code you received: 

The exact same code runs well in my local machine with Ubuntu OS.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 29 (10 by maintainers)

Most upvoted comments

@selfishhari in the past a few days I was dealing with the same issue. Interestingly enough my code worked on day one on AWS and on the second day stoped working!!!

I believe I have found the root cause. The issue is with not having the NTP (Network Time Protocol) and TimeZone properly set up on the server-side which I’m going to describe it based on my scenario.

The only thing that we always miss to compare between dev and test or prod environment is the local time of our machines especially when we move our app to the cloud.

When I tested my code for the very first time, I tested on my AWS machine which worked just fine but on the second day of my testing, I decided to do it on my local machine and of course, I received the verification code but when I moved back to my AWS server things started acting strange!!!

The catch was my local zone was based on US Central (America/Chicago) and when I moved my code to my AWS the time zone of my server was UTC. Based on my past experience I have seen REST APIs get confused when they see the authentication requests coming from a different time. (one request is coming from the current time and one request coming from the future).

The best way to avoid this type of situation is to use one standard time zone (e.g. UTC or Local Time Zone ) in both Dev and Test/Prod environments and that was exactly what I did.

On my AWS machine, I synced my server with NTP and changed the time zone from UTC to America/Central and everything started working again.

Here is the link for AWS NTP: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html

And here is the Linux command for setting up the right time zone:

$ timedatectl list-timezones $ timedatectl list-timezones | grep Chicago $ sudo timedatectl set-timezone America/Chicago

I found a workaround:

Since it runs on local machine. I copied the generated session file from local machine to my cloud working directory. All seems to be working fine after that. 😃

@LorenzHW pyrogram uses GNU Lesser General Public License which I think do not allow comercialization. Telethon is the most complete MIT Telegram API written in python and would be awsome to go on on this issue. Is there a way to see what pyrogram or python-telegram does so we could do the same here?

Hi, @Lonami pinged me about this. It’s true that pyrogram is currently licensed under LGPL, but this doesn’t mean you can’t use it commercially. In fact, you can use and integrate Pyrogram into your own code — either open source, under the same or a different license, or even proprietary with any purpose whatsoever — without being required to release the source code of your own applications. However, any derivative work or modification to the library itself is required to be published for free under the same LGPLv3+ license.

I have the same issue - code runs fine on my local machine (in Greece) but when I installed the code in AWS I do not receive a login code. However, if using a telegram account with a US phone number it works perfectly OK. So the issue is caused by the telegram account country != country where the code runs

This is being reported a lot lately and unfortunately I don’t think there is anything we can do. Quoting a message on the official chat:

apparently it mostly fails for indian users, but works if they try at 3:00 am local time